Advertisement
jbodhorn

Failing_button_color_change

Nov 1st, 2021
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.25 KB | None | 0 0
  1. // This is a demonstration on how to use an input device to trigger changes on your neo pixels.
  2. // You should wire a momentary push button to connect from ground to a digital IO pin. When you
  3. // press the button it will change to a new pixel animation. Note that you need to press the
  4. // button once to start the first animation!
  5.  
  6. #include <Adafruit_NeoPixel.h>
  7.  
  8. #define BUTTON_PIN 2 // Digital IO pin connected to the button. This will be
  9. // driven with a pull-up resistor so the switch should
  10. // pull the pin to ground momentarily. On a high -> low
  11. // transition the button press logic will execute.
  12.  
  13. #define PIXEL_PIN 6 // Digital IO pin connected to the NeoPixels.
  14.  
  15. #define PIXEL_COUNT 8
  16.  
  17. // Parameter 1 = number of pixels in strip, neopixel stick has 8
  18. // Parameter 2 = pin number (most are valid)
  19. // Parameter 3 = pixel type flags, add together as needed:
  20. // NEO_RGB Pixels are wired for RGB bitstream
  21. // NEO_GRB Pixels are wired for GRB bitstream, correct for neopixel stick
  22. // NEO_KHZ400 400 KHz bitstream (e.g. FLORA pixels)
  23. // NEO_KHZ800 800 KHz bitstream (e.g. High Density LED strip), correct for neopixel stick
  24. Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, NEO_GRB + NEO_KHZ800);
  25.  
  26. bool oldState = HIGH;
  27. int showType = 0;
  28.  
  29. void setup() {
  30. pinMode(BUTTON_PIN, INPUT_PULLUP);
  31. strip.begin();
  32. strip.show(); // Initialize all pixels to 'off'
  33. }
  34.  
  35. void loop()
  36. {
  37. // Get current button state.
  38. bool newState = digitalRead(BUTTON_PIN);
  39.  
  40. // Check if state changed from high to low (button press).
  41. if (newState == LOW && oldState == HIGH)
  42. {
  43. // Short delay to debounce button.
  44. delay(10);
  45. // Check if button is still low after debounce.
  46. newState = digitalRead(BUTTON_PIN);
  47. if (newState == LOW)
  48. {
  49. showType++;
  50. if (showType > 10)
  51. showType=0;
  52. startShow(showType);
  53. }
  54. }
  55. startShow(showType);
  56.  
  57. // Set the last button state to the old state.
  58. oldState = newState;
  59. }
  60.  
  61. void startShow(int i) {
  62. switch(i){
  63. case 0: colorWipe(strip.Color(0, 0, 0), 50); // Black/off
  64. break;
  65. case 1: colorWipe(strip.Color(255, 0, 0), 50); // Red
  66. break;
  67. case 2: colorWipe(strip.Color(0, 255, 0), 50); // Green
  68. break;
  69. case 3: colorWipe(strip.Color(0, 0, 255), 50); // Blue
  70. break;
  71. case 4: colorWipe(strip.Color(255, 255, 255), 255); // white full
  72. break;
  73. case 5: theaterChase(strip.Color(127, 127, 127), 50); // white
  74. break;
  75. case 6: theaterChase(strip.Color(127, 0, 0), 50); // Red
  76. break;
  77. case 7: theaterChase(strip.Color( 0, 0, 127), 50); // Blue
  78. break;
  79. case 8: rainbow(255);
  80. break;
  81. case 9: rainbowCycle(255);
  82. break;
  83. case 10: theaterChaseRainbow(255);
  84. break;
  85. }
  86. }
  87.  
  88. // Fill the dots one after the other with a color
  89. void colorWipe(uint32_t c, uint8_t wait) {
  90. for(uint16_t i=0; i<strip.numPixels(); i++) {
  91. strip.setPixelColor(i, c);
  92. strip.show();
  93. delay(wait);
  94. }
  95. }
  96.  
  97. void rainbow(uint8_t wait) {
  98. uint16_t i, j;
  99.  
  100. for(j=0; j<256; j++) {
  101. for(i=0; i<strip.numPixels(); i++) {
  102. strip.setPixelColor(i, Wheel((i+j) & 255));
  103. }
  104. strip.show();
  105. delay(wait);
  106. }
  107. }
  108.  
  109. // Slightly different, this makes the rainbow equally distributed throughout
  110. void rainbowCycle(uint8_t wait) {
  111. uint16_t i, j;
  112.  
  113. for(j=0; j<256*5; j++) { // 5 cycles of all colors on wheel
  114. for(i=0; i< strip.numPixels(); i++) {
  115. strip.setPixelColor(i, Wheel(((i * 256 / strip.numPixels()) + j) & 255));
  116. }
  117. strip.show();
  118. delay(wait);
  119. }
  120. }
  121.  
  122. //Theatre-style crawling lights.
  123. void theaterChase(uint32_t c, uint8_t wait) {
  124. for (int j=0; j<10; j++) { //do 10 cycles of chasing
  125. for (int q=0; q < 3; q++) {
  126. for (int i=0; i < strip.numPixels(); i=i+3) {
  127. strip.setPixelColor(i+q, c); //turn every third pixel on
  128. }
  129. strip.show();
  130.  
  131. delay(wait);
  132.  
  133. for (int i=0; i < strip.numPixels(); i=i+3) {
  134. strip.setPixelColor(i+q, 0); //turn every third pixel off
  135. }
  136. }
  137. }
  138. }
  139.  
  140. //Theatre-style crawling lights with rainbow effect
  141. void theaterChaseRainbow(uint8_t wait) {
  142. for (int j=0; j < 256; j++) { // cycle all 256 colors in the wheel
  143. for (int q=0; q < 3; q++) {
  144. for (int i=0; i < strip.numPixels(); i=i+3) {
  145. strip.setPixelColor(i+q, Wheel( (i+j) % 255)); //turn every third pixel on
  146. }
  147. strip.show();
  148.  
  149. delay(wait);
  150.  
  151. for (int i=0; i < strip.numPixels(); i=i+3) {
  152. strip.setPixelColor(i+q, 0); //turn every third pixel off
  153. }
  154. }
  155. }
  156. }
  157.  
  158. // Input a value 0 to 255 to get a color value.
  159. // The colours are a transition r - g - b - back to r.
  160. uint32_t Wheel(byte WheelPos) {
  161. WheelPos = 255 - WheelPos;
  162. if(WheelPos < 85) {
  163. return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
  164. }
  165. if(WheelPos < 170) {
  166. WheelPos -= 85;
  167. return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
  168. }
  169. WheelPos -= 170;
  170. return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
  171. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement