brix5 / firmware / demo / FeatherBase-RGB_LED_Test / FeatherBase-RGB_LED_Test.ino @ d83f65fc
History | View | Annotate | Download (6.708 KB)
1 | 7393c00e | Jan | // 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 ESP32, A3 is not output capable; |
||
20 | 5ca1a326 | Jan | // quick fix is to use Pin 21 (e.g.) and hardware connect 21 to A3 |
21 | #define LED_PIN A3 |
||
22 | 7393c00e | Jan | |
23 | // How many NeoPixels are attached to the Arduino? |
||
24 | #define LED_COUNT 2 |
||
25 | |||
26 | // Declare our NeoPixel strip object: |
||
27 | Adafruit_NeoPixel strip(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800); |
||
28 | // Argument 1 = Number of pixels in NeoPixel strip |
||
29 | // Argument 2 = Arduino pin number (most are valid) |
||
30 | // Argument 3 = Pixel type flags, add together as needed: |
||
31 | // NEO_KHZ800 800 KHz bitstream (most NeoPixel products w/WS2812 LEDs) |
||
32 | // NEO_KHZ400 400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers) |
||
33 | // NEO_GRB Pixels are wired for GRB bitstream (most NeoPixel products) |
||
34 | // NEO_RGB Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2) |
||
35 | // NEO_RGBW Pixels are wired for RGBW bitstream (NeoPixel RGBW products) |
||
36 | |||
37 | |||
38 | // setup() function -- runs once at startup -------------------------------- |
||
39 | |||
40 | void setup() { |
||
41 | // These lines are specifically to support the Adafruit Trinket 5V 16 MHz. |
||
42 | // Any other board, you can remove this part (but no harm leaving it): |
||
43 | #if defined(__AVR_ATtiny85__) && (F_CPU == 16000000) |
||
44 | clock_prescale_set(clock_div_1); |
||
45 | #endif |
||
46 | // END of Trinket-specific code. |
||
47 | |||
48 | strip.begin(); // INITIALIZE NeoPixel strip object (REQUIRED) |
||
49 | strip.show(); // Turn OFF all pixels ASAP |
||
50 | strip.setBrightness(50); // Set BRIGHTNESS to about 1/5 (max = 255) |
||
51 | } |
||
52 | |||
53 | |||
54 | // loop() function -- runs repeatedly as long as board is on --------------- |
||
55 | |||
56 | void loop() { |
||
57 | // Fill along the length of the strip in various colors... |
||
58 | colorWipe(strip.Color(255, 0, 0), 50); // Red |
||
59 | colorWipe(strip.Color( 0, 255, 0), 50); // Green |
||
60 | colorWipe(strip.Color( 0, 0, 255), 50); // Blue |
||
61 | |||
62 | // Do a theater marquee effect in various colors... |
||
63 | theaterChase(strip.Color(127, 127, 127), 50); // White, half brightness |
||
64 | theaterChase(strip.Color(127, 0, 0), 50); // Red, half brightness |
||
65 | theaterChase(strip.Color( 0, 0, 127), 50); // Blue, half brightness |
||
66 | |||
67 | rainbow(10); // Flowing rainbow cycle along the whole strip |
||
68 | theaterChaseRainbow(50); // Rainbow-enhanced theaterChase variant |
||
69 | } |
||
70 | |||
71 | |||
72 | // Some functions of our own for creating animated effects ----------------- |
||
73 | |||
74 | // Fill strip pixels one after another with a color. Strip is NOT cleared |
||
75 | // first; anything there will be covered pixel by pixel. Pass in color |
||
76 | // (as a single 'packed' 32-bit value, which you can get by calling |
||
77 | // strip.Color(red, green, blue) as shown in the loop() function above), |
||
78 | // and a delay time (in milliseconds) between pixels. |
||
79 | void colorWipe(uint32_t color, int wait) { |
||
80 | for(int i=0; i<strip.numPixels(); i++) { // For each pixel in strip... |
||
81 | strip.setPixelColor(i, color); // Set pixel's color (in RAM) |
||
82 | strip.show(); // Update strip to match |
||
83 | delay(wait); // Pause for a moment |
||
84 | } |
||
85 | } |
||
86 | |||
87 | // Theater-marquee-style chasing lights. Pass in a color (32-bit value, |
||
88 | // a la strip.Color(r,g,b) as mentioned above), and a delay time (in ms) |
||
89 | // between frames. |
||
90 | void theaterChase(uint32_t color, int wait) { |
||
91 | for(int a=0; a<10; a++) { // Repeat 10 times... |
||
92 | for(int b=0; b<3; b++) { // 'b' counts from 0 to 2... |
||
93 | strip.clear(); // Set all pixels in RAM to 0 (off) |
||
94 | // 'c' counts up from 'b' to end of strip in steps of 3... |
||
95 | for(int c=b; c<strip.numPixels(); c += 3) { |
||
96 | strip.setPixelColor(c, color); // Set pixel 'c' to value 'color' |
||
97 | } |
||
98 | strip.show(); // Update strip with new contents |
||
99 | delay(wait); // Pause for a moment |
||
100 | } |
||
101 | } |
||
102 | } |
||
103 | |||
104 | // Rainbow cycle along whole strip. Pass delay time (in ms) between frames. |
||
105 | void rainbow(int wait) { |
||
106 | // Hue of first pixel runs 5 complete loops through the color wheel. |
||
107 | // Color wheel has a range of 65536 but it's OK if we roll over, so |
||
108 | // just count from 0 to 5*65536. Adding 256 to firstPixelHue each time |
||
109 | // means we'll make 5*65536/256 = 1280 passes through this outer loop: |
||
110 | for(long firstPixelHue = 0; firstPixelHue < 5*65536; firstPixelHue += 256) { |
||
111 | for(int i=0; i<strip.numPixels(); i++) { // For each pixel in strip... |
||
112 | // Offset pixel hue by an amount to make one full revolution of the |
||
113 | // color wheel (range of 65536) along the length of the strip |
||
114 | // (strip.numPixels() steps): |
||
115 | int pixelHue = firstPixelHue + (i * 65536L / strip.numPixels()); |
||
116 | // strip.ColorHSV() can take 1 or 3 arguments: a hue (0 to 65535) or |
||
117 | // optionally add saturation and value (brightness) (each 0 to 255). |
||
118 | // Here we're using just the single-argument hue variant. The result |
||
119 | // is passed through strip.gamma32() to provide 'truer' colors |
||
120 | // before assigning to each pixel: |
||
121 | strip.setPixelColor(i, strip.gamma32(strip.ColorHSV(pixelHue))); |
||
122 | } |
||
123 | strip.show(); // Update strip with new contents |
||
124 | delay(wait); // Pause for a moment |
||
125 | } |
||
126 | } |
||
127 | |||
128 | // Rainbow-enhanced theater marquee. Pass delay time (in ms) between frames. |
||
129 | void theaterChaseRainbow(int wait) { |
||
130 | int firstPixelHue = 0; // First pixel starts at red (hue 0) |
||
131 | for(int a=0; a<30; a++) { // Repeat 30 times... |
||
132 | for(int b=0; b<3; b++) { // 'b' counts from 0 to 2... |
||
133 | strip.clear(); // Set all pixels in RAM to 0 (off) |
||
134 | // 'c' counts up from 'b' to end of strip in increments of 3... |
||
135 | for(int c=b; c<strip.numPixels(); c += 3) { |
||
136 | // hue of pixel 'c' is offset by an amount to make one full |
||
137 | // revolution of the color wheel (range 65536) along the length |
||
138 | // of the strip (strip.numPixels() steps): |
||
139 | int hue = firstPixelHue + c * 65536L / strip.numPixels(); |
||
140 | uint32_t color = strip.gamma32(strip.ColorHSV(hue)); // hue -> RGB |
||
141 | strip.setPixelColor(c, color); // Set pixel 'c' to value 'color' |
||
142 | } |
||
143 | strip.show(); // Update strip with new contents |
||
144 | delay(wait); // Pause for a moment |
||
145 | firstPixelHue += 65536 / 90; // One cycle of color wheel over 90 frames |
||
146 | } |
||
147 | } |
||
148 | } |