buttoncycler.ino 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. // Simple demonstration on using an input device to trigger changes on your
  2. // NeoPixels. Wire a momentary push button to connect from ground to a
  3. // digital IO pin. When the button is pressed it will change to a new pixel
  4. // animation. Initial state has all pixels off -- press the button once to
  5. // start the first animation. As written, the button does not interrupt an
  6. // animation in-progress, it works only when idle.
  7. #include <Adafruit_NeoPixel.h>
  8. #ifdef __AVR__
  9. #include <avr/power.h> // Required for 16 MHz Adafruit Trinket
  10. #endif
  11. // Digital IO pin connected to the button. This will be driven with a
  12. // pull-up resistor so the switch pulls the pin to ground momentarily.
  13. // On a high -> low transition the button press logic will execute.
  14. #define BUTTON_PIN 2
  15. #define PIXEL_PIN 6 // Digital IO pin connected to the NeoPixels.
  16. #define PIXEL_COUNT 16 // Number of NeoPixels
  17. // Declare our NeoPixel strip object:
  18. Adafruit_NeoPixel strip(PIXEL_COUNT, PIXEL_PIN, NEO_GRB + NEO_KHZ800);
  19. // Argument 1 = Number of pixels in NeoPixel strip
  20. // Argument 2 = Arduino pin number (most are valid)
  21. // Argument 3 = Pixel type flags, add together as needed:
  22. // NEO_KHZ800 800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
  23. // NEO_KHZ400 400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
  24. // NEO_GRB Pixels are wired for GRB bitstream (most NeoPixel products)
  25. // NEO_RGB Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
  26. // NEO_RGBW Pixels are wired for RGBW bitstream (NeoPixel RGBW products)
  27. boolean oldState = HIGH;
  28. int mode = 0; // Currently-active animation mode, 0-9
  29. void setup() {
  30. pinMode(BUTTON_PIN, INPUT_PULLUP);
  31. strip.begin(); // Initialize NeoPixel strip object (REQUIRED)
  32. strip.show(); // Initialize all pixels to 'off'
  33. }
  34. void loop() {
  35. // Get current button state.
  36. boolean newState = digitalRead(BUTTON_PIN);
  37. // Check if state changed from high to low (button press).
  38. if((newState == LOW) && (oldState == HIGH)) {
  39. // Short delay to debounce button.
  40. delay(20);
  41. // Check if button is still low after debounce.
  42. newState = digitalRead(BUTTON_PIN);
  43. if(newState == LOW) { // Yes, still low
  44. if(++mode > 8) mode = 0; // Advance to next mode, wrap around after #8
  45. switch(mode) { // Start the new animation...
  46. case 0:
  47. colorWipe(strip.Color( 0, 0, 0), 50); // Black/off
  48. break;
  49. case 1:
  50. colorWipe(strip.Color(255, 0, 0), 50); // Red
  51. break;
  52. case 2:
  53. colorWipe(strip.Color( 0, 255, 0), 50); // Green
  54. break;
  55. case 3:
  56. colorWipe(strip.Color( 0, 0, 255), 50); // Blue
  57. break;
  58. case 4:
  59. theaterChase(strip.Color(127, 127, 127), 50); // White
  60. break;
  61. case 5:
  62. theaterChase(strip.Color(127, 0, 0), 50); // Red
  63. break;
  64. case 6:
  65. theaterChase(strip.Color( 0, 0, 127), 50); // Blue
  66. break;
  67. case 7:
  68. rainbow(10);
  69. break;
  70. case 8:
  71. theaterChaseRainbow(50);
  72. break;
  73. }
  74. }
  75. }
  76. // Set the last-read button state to the old state.
  77. oldState = newState;
  78. }
  79. // Fill strip pixels one after another with a color. Strip is NOT cleared
  80. // first; anything there will be covered pixel by pixel. Pass in color
  81. // (as a single 'packed' 32-bit value, which you can get by calling
  82. // strip.Color(red, green, blue) as shown in the loop() function above),
  83. // and a delay time (in milliseconds) between pixels.
  84. void colorWipe(uint32_t color, int wait) {
  85. for(int i=0; i<strip.numPixels(); i++) { // For each pixel in strip...
  86. strip.setPixelColor(i, color); // Set pixel's color (in RAM)
  87. strip.show(); // Update strip to match
  88. delay(wait); // Pause for a moment
  89. }
  90. }
  91. // Theater-marquee-style chasing lights. Pass in a color (32-bit value,
  92. // a la strip.Color(r,g,b) as mentioned above), and a delay time (in ms)
  93. // between frames.
  94. void theaterChase(uint32_t color, int wait) {
  95. for(int a=0; a<10; a++) { // Repeat 10 times...
  96. for(int b=0; b<3; b++) { // 'b' counts from 0 to 2...
  97. strip.clear(); // Set all pixels in RAM to 0 (off)
  98. // 'c' counts up from 'b' to end of strip in steps of 3...
  99. for(int c=b; c<strip.numPixels(); c += 3) {
  100. strip.setPixelColor(c, color); // Set pixel 'c' to value 'color'
  101. }
  102. strip.show(); // Update strip with new contents
  103. delay(wait); // Pause for a moment
  104. }
  105. }
  106. }
  107. // Rainbow cycle along whole strip. Pass delay time (in ms) between frames.
  108. void rainbow(int wait) {
  109. // Hue of first pixel runs 3 complete loops through the color wheel.
  110. // Color wheel has a range of 65536 but it's OK if we roll over, so
  111. // just count from 0 to 3*65536. Adding 256 to firstPixelHue each time
  112. // means we'll make 3*65536/256 = 768 passes through this outer loop:
  113. for(long firstPixelHue = 0; firstPixelHue < 3*65536; firstPixelHue += 256) {
  114. for(int i=0; i<strip.numPixels(); i++) { // For each pixel in strip...
  115. // Offset pixel hue by an amount to make one full revolution of the
  116. // color wheel (range of 65536) along the length of the strip
  117. // (strip.numPixels() steps):
  118. int pixelHue = firstPixelHue + (i * 65536L / strip.numPixels());
  119. // strip.ColorHSV() can take 1 or 3 arguments: a hue (0 to 65535) or
  120. // optionally add saturation and value (brightness) (each 0 to 255).
  121. // Here we're using just the single-argument hue variant. The result
  122. // is passed through strip.gamma32() to provide 'truer' colors
  123. // before assigning to each pixel:
  124. strip.setPixelColor(i, strip.gamma32(strip.ColorHSV(pixelHue)));
  125. }
  126. strip.show(); // Update strip with new contents
  127. delay(wait); // Pause for a moment
  128. }
  129. }
  130. // Rainbow-enhanced theater marquee. Pass delay time (in ms) between frames.
  131. void theaterChaseRainbow(int wait) {
  132. int firstPixelHue = 0; // First pixel starts at red (hue 0)
  133. for(int a=0; a<30; a++) { // Repeat 30 times...
  134. for(int b=0; b<3; b++) { // 'b' counts from 0 to 2...
  135. strip.clear(); // Set all pixels in RAM to 0 (off)
  136. // 'c' counts up from 'b' to end of strip in increments of 3...
  137. for(int c=b; c<strip.numPixels(); c += 3) {
  138. // hue of pixel 'c' is offset by an amount to make one full
  139. // revolution of the color wheel (range 65536) along the length
  140. // of the strip (strip.numPixels() steps):
  141. int hue = firstPixelHue + c * 65536L / strip.numPixels();
  142. uint32_t color = strip.gamma32(strip.ColorHSV(hue)); // hue -> RGB
  143. strip.setPixelColor(c, color); // Set pixel 'c' to value 'color'
  144. }
  145. strip.show(); // Update strip with new contents
  146. delay(wait); // Pause for a moment
  147. firstPixelHue += 65536 / 90; // One cycle of color wheel over 90 frames
  148. }
  149. }
  150. }