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