Advertisement
Guest User

Untitled

a guest
Apr 24th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.70 KB | None | 0 0
  1. #include <Bounce.h>
  2. #include <FastLED.h>
  3.  
  4. // How many leds in your strip?
  5. #define NUM_LEDS 8
  6.  
  7. // Data pin for the LEDs
  8. #define DATA_PIN 17
  9.  
  10. // Potentiometer pins
  11. #define POT_0_PIN A0
  12. #define POT_1_PIN A1
  13. #define POT_2_PIN A2
  14.  
  15. // Switch pin
  16. #define SWITCH_PIN 9
  17.  
  18. // Push button pin
  19. #define BUTTON_PIN 2
  20.  
  21. Bounce bounce = Bounce(BUTTON_PIN, 5);
  22.  
  23. // Define the array of leds
  24. CRGB leds[NUM_LEDS];
  25.  
  26. boolean waiting = false;
  27.  
  28. byte failedAttempts = 0;
  29. byte maxFailedAttempts = 3;
  30.  
  31. // the random target color the player will try to match
  32. CHSV targetColor;
  33.  
  34. // the current color the player has created
  35. CHSV currentColor;
  36.  
  37. // the maximum allowable difference in hue between the target and current colors
  38. const byte maxDifference = 8;
  39.  
  40. // setup is called once by Arduino automatically
  41. // all initial setup work should be done here
  42. void setup() {
  43. // start the serial port, so we can log data to the Arduino IDE
  44. Serial.begin(9600);
  45.  
  46. // start FastLED, tell it about our LEDs
  47. FastLED.addLeds<WS2811, DATA_PIN, GRB>(leds, NUM_LEDS);
  48.  
  49. // set the LED brightness
  50. // this is a global brightness, applied regardless of what color(s) are shown on the LEDs
  51. FastLED.setBrightness(16);
  52.  
  53. FastLED.clear();
  54.  
  55. // set the switch and button pins to input mode (as opposed to output), since we'll be reading from them
  56. pinMode(SWITCH_PIN, INPUT);
  57. pinMode(BUTTON_PIN, INPUT);
  58. }
  59.  
  60. void loop() {
  61. bounce.update();
  62.  
  63. random16_add_entropy(analogRead(A3));
  64. random16_add_entropy (random());
  65.  
  66. int value0 = analogRead(A0); // read the value from the first potentiometer
  67. int value1 = analogRead(A1); // read the value from the second potentiometer
  68. int value2 = analogRead(A2); // read the value from the third potentiometer
  69.  
  70. // read whether the button is pressed
  71. boolean buttonIsPressed = bounce.risingEdge();
  72.  
  73. // read whether the switch is on or off
  74. boolean switchIsOn = digitalRead(SWITCH_PIN) == HIGH;
  75.  
  76. // scale the potentiometer values from 10-bit (0-1023) to 8-bit (0-255)
  77. byte byte0 = map(value0, 0, 1023, 0, 255); // scale the value of first potentiometer
  78. byte byte1 = map(value1, 0, 1023, 0, 255); // scale the value of second potentiometer
  79. byte byte2 = map(value2, 0, 1023, 0, 255); // scale the value of third potentiometer
  80.  
  81. // log the scaled potentiomenter values to the Serial port
  82. // printValues(byte0, byte1, byte2);
  83.  
  84. if(!waiting) {
  85. byte newHue = random8();
  86. while(abs((int)newHue - (int)targetColor.h) < 64) {
  87. newHue = random8();
  88. }
  89. targetColor = CHSV(newHue, 255, 255);
  90. leds[1] = targetColor;
  91. waiting = true;
  92. }
  93.  
  94. currentColor = CHSV(byte0, byte1, byte2);
  95. leds[0] = currentColor;
  96.  
  97. // if the button is pressed, check
  98. if (buttonIsPressed && waiting) {
  99. checkColors();
  100. }
  101.  
  102. // use FastLED to show the color(s) we've set
  103. FastLED.show();
  104.  
  105. // wait for a fraction of a second (30ms, or about 1/30 of a second)
  106. // let FastLED show the color while we wait
  107. FastLED.delay(30);
  108. }
  109.  
  110. void checkColors() {
  111. Serial.print(currentColor.h);
  112. Serial.print(", "); Serial.print(currentColor.s);
  113. Serial.print(", "); Serial.print(currentColor.v);
  114.  
  115. int diff = abs((int)currentColor.h - (int)targetColor.h);
  116.  
  117. // if the colors match, shift them down the strip
  118. if (diff <= maxDifference) {
  119. Serial.print(" == ");
  120. for(byte i = NUM_LEDS - 1; i > 0; i--) {
  121. leds[i] = leds[i - 1];
  122. }
  123.  
  124. failedAttempts = 0;
  125.  
  126. waiting = false;
  127. }
  128. else {
  129. Serial.print(" != ");
  130.  
  131. failedAttempts += 1;
  132.  
  133. byte numberToFlash = 1;
  134.  
  135. if(failedAttempts > maxFailedAttempts) {
  136. numberToFlash = NUM_LEDS;
  137. }
  138.  
  139. for(byte i = 0; i <= failedAttempts; i++) {
  140. fill_solid(leds, numberToFlash, CRGB::Red);
  141. FastLED.delay(failedAttempts * 100);
  142. fill_solid(leds, numberToFlash, CRGB::Black);
  143. FastLED.delay(failedAttempts * 100);
  144. }
  145.  
  146. if(failedAttempts > maxFailedAttempts) {
  147. failedAttempts = 0;
  148. waiting = false;
  149. }
  150. }
  151.  
  152. Serial.print(targetColor.h);
  153. Serial.print(", "); Serial.print(targetColor.s);
  154. Serial.print(", "); Serial.print(targetColor.v);
  155. Serial.println("");
  156. }
  157.  
  158. void printValues(int value0, int value1, int value2) {
  159. // keep track of the former values, so we can log only when they change
  160. // since these are static, they'll keep their value between calls
  161. static int lastValue0 = 0;
  162. static int lastValue1 = 0;
  163. static int lastValue2 = 0;
  164.  
  165. // if any of the values have changed
  166. if (value0 != lastValue0 || value1 != lastValue1 || value2 != lastValue2) {
  167. // print the new values to the Serial port
  168. Serial.print(value0);
  169. Serial.print(", "); Serial.print(value1);
  170. Serial.print(", "); Serial.print(value2);
  171. Serial.println("");
  172.  
  173. // store the new values
  174. lastValue0 = value0;
  175. lastValue1 = value1;
  176. lastValue2 = value2;
  177. }
  178. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement