strandtest.ino 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. // A basic everyday NeoPixel strip test program.
  2. // NEOPIXEL BEST PRACTICES for most reliable operation:
  3. // - Add 1000 uF CAPACITOR between NeoPixel strip's + and - connections.
  4. // - MINIMIZE WIRING LENGTH between microcontroller board and first pixel.
  5. // - NeoPixel strip's DATA-IN should pass through a 300-500 OHM RESISTOR.
  6. // - AVOID connecting NeoPixels on a LIVE CIRCUIT. If you must, ALWAYS
  7. // connect GROUND (-) first, then +, then data.
  8. // - When using a 3.3V microcontroller with a 5V-powered NeoPixel strip,
  9. // a LOGIC-LEVEL CONVERTER on the data line is STRONGLY RECOMMENDED.
  10. // (Skipping these may work OK on your workbench but can fail in the field)
  11. #include <Adafruit_NeoPixel.h>
  12. #ifdef __AVR__
  13. #include <avr/power.h> // Required for 16 MHz Adafruit Trinket
  14. #endif
  15. // Which pin on the Arduino is connected to the NeoPixels?
  16. // On a Trinket or Gemma we suggest changing this to 1:
  17. #define LED_PIN 6
  18. // How many NeoPixels are attached to the Arduino?
  19. #define LED_COUNT 60
  20. // Declare our NeoPixel strip object:
  21. Adafruit_NeoPixel strip(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800);
  22. // Argument 1 = Number of pixels in NeoPixel strip
  23. // Argument 2 = Arduino pin number (most are valid)
  24. // Argument 3 = Pixel type flags, add together as needed:
  25. // NEO_KHZ800 800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
  26. // NEO_KHZ400 400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
  27. // NEO_GRB Pixels are wired for GRB bitstream (most NeoPixel products)
  28. // NEO_RGB Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
  29. // NEO_RGBW Pixels are wired for RGBW bitstream (NeoPixel RGBW products)
  30. // setup() function -- runs once at startup --------------------------------
  31. void setup() {
  32. // These lines are specifically to support the Adafruit Trinket 5V 16 MHz.
  33. // Any other board, you can remove this part (but no harm leaving it):
  34. #if defined(__AVR_ATtiny85__) && (F_CPU == 16000000)
  35. clock_prescale_set(clock_div_1);
  36. #endif
  37. // END of Trinket-specific code.
  38. strip.begin(); // INITIALIZE NeoPixel strip object (REQUIRED)
  39. strip.show(); // Turn OFF all pixels ASAP
  40. strip.setBrightness(50); // Set BRIGHTNESS to about 1/5 (max = 255)
  41. }
  42. // loop() function -- runs repeatedly as long as board is on ---------------
  43. void loop() {
  44. // Fill along the length of the strip in various colors...
  45. colorWipe(strip.Color(255, 0, 0), 50); // Red
  46. colorWipe(strip.Color( 0, 255, 0), 50); // Green
  47. colorWipe(strip.Color( 0, 0, 255), 50); // Blue
  48. // Do a theater marquee effect in various colors...
  49. theaterChase(strip.Color(127, 127, 127), 50); // White, half brightness
  50. theaterChase(strip.Color(127, 0, 0), 50); // Red, half brightness
  51. theaterChase(strip.Color( 0, 0, 127), 50); // Blue, half brightness
  52. rainbow(10); // Flowing rainbow cycle along the whole strip
  53. theaterChaseRainbow(50); // Rainbow-enhanced theaterChase variant
  54. }
  55. // Some functions of our own for creating animated effects -----------------
  56. // Fill strip pixels one after another with a color. Strip is NOT cleared
  57. // first; anything there will be covered pixel by pixel. Pass in color
  58. // (as a single 'packed' 32-bit value, which you can get by calling
  59. // strip.Color(red, green, blue) as shown in the loop() function above),
  60. // and a delay time (in milliseconds) between pixels.
  61. void colorWipe(uint32_t color, int wait) {
  62. for(int i=0; i<strip.numPixels(); i++) { // For each pixel in strip...
  63. strip.setPixelColor(i, color); // Set pixel's color (in RAM)
  64. strip.show(); // Update strip to match
  65. delay(wait); // Pause for a moment
  66. }
  67. }
  68. // Theater-marquee-style chasing lights. Pass in a color (32-bit value,
  69. // a la strip.Color(r,g,b) as mentioned above), and a delay time (in ms)
  70. // between frames.
  71. void theaterChase(uint32_t color, int wait) {
  72. for(int a=0; a<10; a++) { // Repeat 10 times...
  73. for(int b=0; b<3; b++) { // 'b' counts from 0 to 2...
  74. strip.clear(); // Set all pixels in RAM to 0 (off)
  75. // 'c' counts up from 'b' to end of strip in steps of 3...
  76. for(int c=b; c<strip.numPixels(); c += 3) {
  77. strip.setPixelColor(c, color); // Set pixel 'c' to value 'color'
  78. }
  79. strip.show(); // Update strip with new contents
  80. delay(wait); // Pause for a moment
  81. }
  82. }
  83. }
  84. // Rainbow cycle along whole strip. Pass delay time (in ms) between frames.
  85. void rainbow(int wait) {
  86. // Hue of first pixel runs 5 complete loops through the color wheel.
  87. // Color wheel has a range of 65536 but it's OK if we roll over, so
  88. // just count from 0 to 5*65536. Adding 256 to firstPixelHue each time
  89. // means we'll make 5*65536/256 = 1280 passes through this outer loop:
  90. for(long firstPixelHue = 0; firstPixelHue < 5*65536; firstPixelHue += 256) {
  91. for(int i=0; i<strip.numPixels(); i++) { // For each pixel in strip...
  92. // Offset pixel hue by an amount to make one full revolution of the
  93. // color wheel (range of 65536) along the length of the strip
  94. // (strip.numPixels() steps):
  95. int pixelHue = firstPixelHue + (i * 65536L / strip.numPixels());
  96. // strip.ColorHSV() can take 1 or 3 arguments: a hue (0 to 65535) or
  97. // optionally add saturation and value (brightness) (each 0 to 255).
  98. // Here we're using just the single-argument hue variant. The result
  99. // is passed through strip.gamma32() to provide 'truer' colors
  100. // before assigning to each pixel:
  101. strip.setPixelColor(i, strip.gamma32(strip.ColorHSV(pixelHue)));
  102. }
  103. strip.show(); // Update strip with new contents
  104. delay(wait); // Pause for a moment
  105. }
  106. }
  107. // Rainbow-enhanced theater marquee. Pass delay time (in ms) between frames.
  108. void theaterChaseRainbow(int wait) {
  109. int firstPixelHue = 0; // First pixel starts at red (hue 0)
  110. for(int a=0; a<30; a++) { // Repeat 30 times...
  111. for(int b=0; b<3; b++) { // 'b' counts from 0 to 2...
  112. strip.clear(); // Set all pixels in RAM to 0 (off)
  113. // 'c' counts up from 'b' to end of strip in increments of 3...
  114. for(int c=b; c<strip.numPixels(); c += 3) {
  115. // hue of pixel 'c' is offset by an amount to make one full
  116. // revolution of the color wheel (range 65536) along the length
  117. // of the strip (strip.numPixels() steps):
  118. int hue = firstPixelHue + c * 65536L / strip.numPixels();
  119. uint32_t color = strip.gamma32(strip.ColorHSV(hue)); // hue -> RGB
  120. strip.setPixelColor(c, color); // Set pixel 'c' to value 'color'
  121. }
  122. strip.show(); // Update strip with new contents
  123. delay(wait); // Pause for a moment
  124. firstPixelHue += 65536 / 90; // One cycle of color wheel over 90 frames
  125. }
  126. }
  127. }