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