jbodhorn

button_color_change

Nov 1st, 2021 (edited)
37
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.43 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(1);
  45. // Check if button is still low after debounce.
  46. newState = digitalRead(BUTTON_PIN);
  47. if (newState == LOW)
  48. {
  49. showType++;
  50. if (showType > 11 )
  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), 0); // Black/off
  64. break;
  65. case 1: colorWipe(strip.Color(255, 0, 0), 255); // Red
  66. break;
  67. case 2: colorWipe(strip.Color(0, 255, 0), 255); // Green
  68. break;
  69. case 3: colorWipe(strip.Color(0, 0, 255), 255); // 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), 75); // white
  74. break;
  75. case 6: theaterChase(strip.Color(127, 0, 0), 75); // Red
  76. break;
  77. case 7: theaterChase(strip.Color( 0, 0, 127), 75); // Blue
  78. break;
  79. case 8: theaterChase(strip.Color(0, 127, 0), 75); // Green
  80. break;
  81. case 9: theaterChase(strip.Color( 127, 0, 127), 75); // Purple
  82. break;
  83. case 10: theaterChase(strip.Color( 0, 127, 127), 75); // Teal
  84. break;
  85. case 11: theaterChase(strip.Color( 127, 127, 0), 75); // Yellow
  86. break;
  87. }
  88. }
  89.  
  90. // Fill the dots one after the other with a color
  91. void colorWipe(uint32_t c, uint8_t wait) {
  92. for(uint16_t i=0; i<strip.numPixels(); i++) {
  93. strip.setPixelColor(i, c);
  94. strip.show();
  95. delay(wait);
  96. }
  97. }
  98.  
  99. void rainbow(uint8_t wait) {
  100. uint16_t i, j;
  101.  
  102. for(j=0; j<256; j++) {
  103. for(i=0; i<strip.numPixels(); i++) {
  104. strip.setPixelColor(i, Wheel((i+j) & 255));
  105. }
  106. strip.show();
  107. delay(wait);
  108. }
  109. }
  110.  
  111. // Slightly different, this makes the rainbow equally distributed throughout
  112. void rainbowCycle(uint8_t wait) {
  113. uint16_t i, j;
  114.  
  115. for(j=0; j<256*5; j++) { // 5 cycles of all colors on wheel
  116. for(i=0; i< strip.numPixels(); i++) {
  117. strip.setPixelColor(i, Wheel(((i * 256 / strip.numPixels()) + j) & 255));
  118. }
  119. strip.show();
  120. delay(wait);
  121. }
  122. }
  123.  
  124. //Theatre-style crawling lights.
  125. void theaterChase(uint32_t c, uint8_t wait) {
  126. for (int j=0; j<10; j++) { //do 10 cycles of chasing
  127. for (int q=0; q < 3; q++) {
  128. for (int i=0; i < strip.numPixels(); i=i+3) {
  129. strip.setPixelColor(i+q, c); //turn every third pixel on
  130. }
  131. strip.show();
  132.  
  133. delay(wait);
  134.  
  135. for (int i=0; i < strip.numPixels(); i=i+3) {
  136. strip.setPixelColor(i+q, 0); //turn every third pixel off
  137. }
  138. }
  139. }
  140. }
  141.  
  142. //Theatre-style crawling lights with rainbow effect
  143. void theaterChaseRainbow(uint8_t wait) {
  144. for (int j=0; j < 256; j++) { // cycle all 256 colors in the wheel
  145. for (int q=0; q < 3; q++) {
  146. for (int i=0; i < strip.numPixels(); i=i+3) {
  147. strip.setPixelColor(i+q, Wheel( (i+j) % 255)); //turn every third pixel on
  148. }
  149. strip.show();
  150.  
  151. delay(wait);
  152.  
  153. for (int i=0; i < strip.numPixels(); i=i+3) {
  154. strip.setPixelColor(i+q, 0); //turn every third pixel off
  155. }
  156. }
  157. }
  158. }
  159.  
  160. // Input a value 0 to 255 to get a color value.
  161. // The colours are a transition r - g - b - back to r.
  162. uint32_t Wheel(byte WheelPos) {
  163. WheelPos = 255 - WheelPos;
  164. if(WheelPos < 85) {
  165. return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
  166. }
  167. if(WheelPos < 170) {
  168. WheelPos -= 85;
  169. return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
  170. }
  171. WheelPos -= 170;
  172. return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
  173. }
Add Comment
Please, Sign In to add comment