Guest User

Untitled

a guest
Jul 20th, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.42 KB | None | 0 0
  1. /*
  2. This code will make every Blink switch between two colors. When a Blink is triple clicked, it will receive the 2 colors a neighboring Blink is using and switch between those colors.
  3. */
  4.  
  5. //Timer that controls when the blinks will switch colors
  6. Timer blinkTimer;
  7.  
  8. //When this becomes true/false, it changes which color value it needs
  9. bool switchColor;
  10.  
  11. //These are the values that will be associated with specific colors
  12. byte colorVal1;
  13. byte colorVal2;
  14.  
  15. //Depending on the colorVals, these colors will change in loop()
  16. Color color1;
  17. Color color2;
  18.  
  19. void setup() {
  20. //Set the blinkTimer and set the first and second colors to RED and Blue so we know it's switching colors at first
  21. blinkTimer.set(500);
  22. colorVal1 = 0;
  23. colorVal2 = 1;
  24. }
  25.  
  26. void loop() {
  27.  
  28. //If colorVal1 = x then the first color is *color*
  29. switch (colorVal1) {
  30. case 0: color1 = RED; break;
  31. case 1: color1 = BLUE; break;
  32. case 2: color1 = GREEN; break;
  33. }
  34.  
  35. //If colorVal2 = x then the second color is *color*
  36. switch (colorVal2) {
  37. case 0: color2 = RED; break;
  38. case 1: color2 = BLUE; break;
  39. case 2: color2 = GREEN; break;
  40. }
  41.  
  42. //When the button is pressed once, add 1 to colorVal1 and change the current color
  43. if (buttonSingleClicked()) {
  44. colorVal1 += 1;
  45. }
  46.  
  47. //When colorVal1 becomes greater than 2, go back to 0 so it loops between the colors
  48. if (colorVal1 > 2) {
  49. colorVal1 = 0;
  50. }
  51.  
  52. //When the button is double clicked, add 1 to colorval2 and change the current color
  53. if (buttonDoubleClicked()) {
  54. colorVal2 += 1;
  55. }
  56.  
  57. if (colorVal2 > 2) {
  58. colorVal2 = 0;
  59. }
  60.  
  61. //When the blinkTimer expires, see if switch color is true/false and make it the opposite to alternate between the first and second color.
  62. //Then set the blinkTimer to 500ms again.
  63. if (blinkTimer.isExpired()) {
  64. if (!switchColor) {
  65. switchColor = true;
  66. } else if (switchColor) {
  67. switchColor = false;
  68. }
  69. blinkTimer.set(500);
  70. }
  71.  
  72. //When the switchColor is not true, show the 1st color. If switchColor is false, show the second color.
  73. if (!switchColor) {
  74. setColor(color1);
  75. } else if (switchColor) {
  76. setColor(color2);
  77. }
  78.  
  79. /*
  80. This is where it gets a bit tricky
  81.  
  82. We will send both the first and second colors to other neighboring Blinks by making the two values just one.
  83.  
  84.  
  85. byte data = (colorVal1 * 10) + colorVal2;
  86.  
  87.  
  88. So in this code we will be making colorVal1 the tens digit and the colorVal2 the singles digit
  89.  
  90. EXAMPLE: Color1 = BLUE & Color2 = GREEN
  91.  
  92. colorVal1 = 1 for BLUE & colorVal2 = 2 for GREEN
  93.  
  94. (1 * 10) + 2 = 12 <-- This is the single value we will send to other Blinks
  95.  
  96. When another Blink receives this data, it needs to dissect this data back to the original values
  97.  
  98. colorVal2 = 12 % 10 = 2
  99.  
  100. colorVal1 = (12 - (12 % 10 = 2)) / 10 = 1
  101.  
  102. */
  103.  
  104.  
  105. FOREACH_FACE(f) {
  106. if (!isValueReceivedOnFaceExpired(f)) {
  107. //get the single color value from a neighboring Blink
  108. byte receivedData = getLastValueReceivedOnFace(f);
  109. if (buttonMultiClicked()) {
  110. //When the Blink is triple clicked, dissect the single value and assign those values to colorVal1 & colorVal2
  111. if (buttonClickCount() == 3) {
  112. colorVal2 = receivedData % 10;
  113. colorVal1 = (receivedData - (receivedData % 10)) / 10;
  114. }
  115. }
  116. }
  117. }
  118.  
  119. //Take your colorVals and make them a single value
  120. byte data = (colorVal1 * 10) + colorVal2;
  121.  
  122. //Send the color data
  123. setValueSentOnAllFaces(data);
  124. }
Add Comment
Please, Sign In to add comment