Misc files
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

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