RGBWstrandtest.ino 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. // NeoPixel test program showing use of the WHITE channel for RGBW
  2. // pixels only (won't look correct on regular RGB NeoPixel strips).
  3. #include <Adafruit_NeoPixel.h>
  4. #ifdef __AVR__
  5. #include <avr/power.h> // Required for 16 MHz Adafruit Trinket
  6. #endif
  7. // Which pin on the Arduino is connected to the NeoPixels?
  8. // On a Trinket or Gemma we suggest changing this to 1:
  9. #define LED_PIN 6
  10. // How many NeoPixels are attached to the Arduino?
  11. #define LED_COUNT 60
  12. // NeoPixel brightness, 0 (min) to 255 (max)
  13. #define BRIGHTNESS 50 // Set BRIGHTNESS to about 1/5 (max = 255)
  14. // Declare our NeoPixel strip object:
  15. Adafruit_NeoPixel strip(LED_COUNT, LED_PIN, NEO_GRBW + NEO_KHZ800);
  16. // Argument 1 = Number of pixels in NeoPixel strip
  17. // Argument 2 = Arduino pin number (most are valid)
  18. // Argument 3 = Pixel type flags, add together as needed:
  19. // NEO_KHZ800 800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
  20. // NEO_KHZ400 400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
  21. // NEO_GRB Pixels are wired for GRB bitstream (most NeoPixel products)
  22. // NEO_RGB Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
  23. // NEO_RGBW Pixels are wired for RGBW bitstream (NeoPixel RGBW products)
  24. void setup() {
  25. // These lines are specifically to support the Adafruit Trinket 5V 16 MHz.
  26. // Any other board, you can remove this part (but no harm leaving it):
  27. #if defined(__AVR_ATtiny85__) && (F_CPU == 16000000)
  28. clock_prescale_set(clock_div_1);
  29. #endif
  30. // END of Trinket-specific code.
  31. strip.begin(); // INITIALIZE NeoPixel strip object (REQUIRED)
  32. strip.show(); // Turn OFF all pixels ASAP
  33. strip.setBrightness(BRIGHTNESS);
  34. }
  35. void loop() {
  36. // Fill along the length of the strip in various colors...
  37. colorWipe(strip.Color(255, 0, 0) , 50); // Red
  38. colorWipe(strip.Color( 0, 255, 0) , 50); // Green
  39. colorWipe(strip.Color( 0, 0, 255) , 50); // Blue
  40. colorWipe(strip.Color( 0, 0, 0, 255), 50); // True white (not RGB white)
  41. whiteOverRainbow(75, 5);
  42. pulseWhite(5);
  43. rainbowFade2White(3, 3, 1);
  44. }
  45. // Fill strip pixels one after another with a color. Strip is NOT cleared
  46. // first; anything there will be covered pixel by pixel. Pass in color
  47. // (as a single 'packed' 32-bit value, which you can get by calling
  48. // strip.Color(red, green, blue) as shown in the loop() function above),
  49. // and a delay time (in milliseconds) between pixels.
  50. void colorWipe(uint32_t color, int wait) {
  51. for(int i=0; i<strip.numPixels(); i++) { // For each pixel in strip...
  52. strip.setPixelColor(i, color); // Set pixel's color (in RAM)
  53. strip.show(); // Update strip to match
  54. delay(wait); // Pause for a moment
  55. }
  56. }
  57. void whiteOverRainbow(int whiteSpeed, int whiteLength) {
  58. if(whiteLength >= strip.numPixels()) whiteLength = strip.numPixels() - 1;
  59. int head = whiteLength - 1;
  60. int tail = 0;
  61. int loops = 3;
  62. int loopNum = 0;
  63. uint32_t lastTime = millis();
  64. uint32_t firstPixelHue = 0;
  65. for(;;) { // Repeat forever (or until a 'break' or 'return')
  66. for(int i=0; i<strip.numPixels(); i++) { // For each pixel in strip...
  67. if(((i >= tail) && (i <= head)) || // If between head & tail...
  68. ((tail > head) && ((i >= tail) || (i <= head)))) {
  69. strip.setPixelColor(i, strip.Color(0, 0, 0, 255)); // Set white
  70. } else { // else set rainbow
  71. int pixelHue = firstPixelHue + (i * 65536L / strip.numPixels());
  72. strip.setPixelColor(i, strip.gamma32(strip.ColorHSV(pixelHue)));
  73. }
  74. }
  75. strip.show(); // Update strip with new contents
  76. // There's no delay here, it just runs full-tilt until the timer and
  77. // counter combination below runs out.
  78. firstPixelHue += 40; // Advance just a little along the color wheel
  79. if((millis() - lastTime) > whiteSpeed) { // Time to update head/tail?
  80. if(++head >= strip.numPixels()) { // Advance head, wrap around
  81. head = 0;
  82. if(++loopNum >= loops) return;
  83. }
  84. if(++tail >= strip.numPixels()) { // Advance tail, wrap around
  85. tail = 0;
  86. }
  87. lastTime = millis(); // Save time of last movement
  88. }
  89. }
  90. }
  91. void pulseWhite(uint8_t wait) {
  92. for(int j=0; j<256; j++) { // Ramp up from 0 to 255
  93. // Fill entire strip with white at gamma-corrected brightness level 'j':
  94. strip.fill(strip.Color(0, 0, 0, strip.gamma8(j)));
  95. strip.show();
  96. delay(wait);
  97. }
  98. for(int j=255; j>=0; j--) { // Ramp down from 255 to 0
  99. strip.fill(strip.Color(0, 0, 0, strip.gamma8(j)));
  100. strip.show();
  101. delay(wait);
  102. }
  103. }
  104. void rainbowFade2White(int wait, int rainbowLoops, int whiteLoops) {
  105. int fadeVal=0, fadeMax=100;
  106. // Hue of first pixel runs 'rainbowLoops' complete loops through the color
  107. // wheel. Color wheel has a range of 65536 but it's OK if we roll over, so
  108. // just count from 0 to rainbowLoops*65536, using steps of 256 so we
  109. // advance around the wheel at a decent clip.
  110. for(uint32_t firstPixelHue = 0; firstPixelHue < rainbowLoops*65536;
  111. firstPixelHue += 256) {
  112. for(int i=0; i<strip.numPixels(); i++) { // For each pixel in strip...
  113. // Offset pixel hue by an amount to make one full revolution of the
  114. // color wheel (range of 65536) along the length of the strip
  115. // (strip.numPixels() steps):
  116. uint32_t pixelHue = firstPixelHue + (i * 65536L / strip.numPixels());
  117. // strip.ColorHSV() can take 1 or 3 arguments: a hue (0 to 65535) or
  118. // optionally add saturation and value (brightness) (each 0 to 255).
  119. // Here we're using just the three-argument variant, though the
  120. // second value (saturation) is a constant 255.
  121. strip.setPixelColor(i, strip.gamma32(strip.ColorHSV(pixelHue, 255,
  122. 255 * fadeVal / fadeMax)));
  123. }
  124. strip.show();
  125. delay(wait);
  126. if(firstPixelHue < 65536) { // First loop,
  127. if(fadeVal < fadeMax) fadeVal++; // fade in
  128. } else if(firstPixelHue >= ((rainbowLoops-1) * 65536)) { // Last loop,
  129. if(fadeVal > 0) fadeVal--; // fade out
  130. } else {
  131. fadeVal = fadeMax; // Interim loop, make sure fade is at max
  132. }
  133. }
  134. for(int k=0; k<whiteLoops; k++) {
  135. for(int j=0; j<256; j++) { // Ramp up 0 to 255
  136. // Fill entire strip with white at gamma-corrected brightness level 'j':
  137. strip.fill(strip.Color(0, 0, 0, strip.gamma8(j)));
  138. strip.show();
  139. }
  140. delay(1000); // Pause 1 second
  141. for(int j=255; j>=0; j--) { // Ramp down 255 to 0
  142. strip.fill(strip.Color(0, 0, 0, strip.gamma8(j)));
  143. strip.show();
  144. }
  145. }
  146. delay(500); // Pause 1/2 second
  147. }