Advertisement
jbodhorn

Neopixel_button_color_change

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