FastLED 3.10.6
Loading...
Searching...
No Matches
MultiArrays.ino
Go to the documentation of this file.
1// @filter: (memory is large)
2
6
7// MultiArrays - see https://github.com/FastLED/FastLED/wiki/Multiple-Controller-Examples for more info on
8// using multiple controllers. In this example, we're going to set up three NEOPIXEL strips on three
9// different pins, each strip getting its own CRGB array to be played with.
10//
11// The strips deliberately have three DIFFERENT lengths. There is no requirement
12// that strips be the same size -- each addLeds() call carries its own array and
13// its own count, so mixing a 60-LED strip with a 12-LED ring works fine. Only
14// the "array of arrays" style (see ArrayOfLedArrays.ino) forces equal lengths,
15// and that is a C array limitation, not a FastLED one.
16
17/*
18 * SAFE PIN REFERENCE FOR MULTI-ARRAY PROJECTS
19 *
20 * When using multiple LED strips, choose pins carefully to avoid conflicts.
21 * Below are tested safe pins for common boards with sufficient memory:
22 *
23 * ESP32-DevKitC (esp32dev) - Original ESP32 WROOM-32:
24 * Safe pins: 2, 4, 5, 12-19, 21-23, 25-27, 32, 33
25 * Best for LED output: 12, 13, 14, 15, 16, 17, 18, 19, 21, 22, 23, 25, 26, 27
26 * Avoid: 0 (boot button), 1 (TX), 3 (RX), 6-11 (flash), 34-39 (input only)
27 * Notes: GPIO 2 can cause boot issues if pulled high. Avoid strapping pins (0, 2, 5, 12, 15) if possible.
28 *
29 * ESP32-C3 (esp32-c3-devkitm-1):
30 * Safe pins: 0-10 (most are safe after boot)
31 * Best for LED output: 0, 1, 2, 3, 4, 5, 6, 7, 8, 10
32 * Avoid: 9 (boot strapping), 18, 19 (USB if using native USB)
33 * Notes: Fewer pins than ESP32, but most are GPIO capable. Pin 9 is strapping pin (keep low during boot).
34 *
35 * ESP32-S3 (esp32-s3-devkitc-1):
36 * Safe pins: 1-18, 21, 38-48 (many options available)
37 * Best for LED output: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 21, 38, 39, 40, 41, 42
38 * Avoid: 0 (boot button), 19, 20 (USB JTAG by default), 26-32 (flash/PSRAM on some modules), 43, 44 (TX/RX)
39 * Notes: Very flexible GPIO. Avoid 19/20 for USB-JTAG. Pin 1 is safe (avoids GPIO19 USB conflict).
40 *
41 * ESP32-C6:
42 * Safe pins: 0-7, 15-23 (after boot)
43 * Best for LED output: 0, 1, 2, 3, 4, 5, 6, 7, 15, 16, 17, 18, 19, 20, 21, 22, 23
44 * Avoid: 8, 9 (strapping pins), 12, 13 (USB if using native USB)
45 * Notes: Similar to C3 but with more pins. Most GPIO are multi-function capable.
46 *
47 * ESP32-P4 (if using esp32p4-devkit):
48 * Safe pins: Varies by specific module - refer to your dev board documentation
49 * Notes: Industrial ESP32 variant. Check your specific module's datasheet for GPIO capabilities.
50 *
51 * RP2040 (Raspberry Pi Pico):
52 * Safe pins: GP0-GP22, GP26-GP28 (most GPIO pins)
53 * Best for LED output: GP2-GP22 (any of these work well)
54 * Avoid: GP23-GP25 (used for internal functions on Pico)
55 * Notes: Very flexible - nearly all GPIO can be used. GP25 is the onboard LED on Pico.
56 * No special strapping requirements. Consider leaving GP0/GP1 free for UART debugging.
57 *
58 * Teensy 4.0:
59 * Safe pins: 0-23, 24-27 (some constraints), 28-33
60 * Best for LED output: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23
61 * Avoid: Pin 13 (onboard LED - can use but may blink), 0 (RX), consider avoiding for debugging
62 * Notes: All digital pins are 3.3V. Very fast GPIO. Most pins work great for LEDs.
63 *
64 * Teensy 4.1:
65 * Safe pins: 0-41, 54-57 (extensive GPIO)
66 * Best for LED output: 1-12, 14-27, 28-39 (many options)
67 * Avoid: Pin 13 (onboard LED), 0 (RX) if using Serial, 40-41 (I2C if using Wire)
68 * Notes: Similar to 4.0 but with many more pins. Excellent for large multi-strip projects.
69 *
70 * Teensy 3.6:
71 * Safe pins: 0-39, 54-57 (many GPIO available)
72 * Best for LED output: 2-12, 14-23, 24-33
73 * Avoid: Pin 13 (onboard LED), 0 (RX), 1 (TX) if using Serial
74 * Notes: 3.3V GPIO. Good memory for multi-strip projects. Predecessor to Teensy 4.x.
75 *
76 * General Recommendations:
77 * - Always test your specific board configuration before deploying
78 * - Start with 2-3 strips to verify pin assignments work correctly
79 * - Check for pin conflicts with other peripherals (SPI, I2C, UART)
80 * - Consider power requirements separately from data pin selection
81 * - For production, avoid pins needed for programming/debugging (TX, RX, USB pins)
82 * - When in doubt, refer to your board's pinout diagram
83 * - Use level shifters if mixing 3.3V boards with 5V LED strips (recommended for reliability)
84 *
85 * NOTE: This example uses platform-specific pin definitions to automatically
86 * select safe pins for your board. Finding truly "universal" safe pins across
87 * all platforms is challenging due to different architectures, boot requirements,
88 * and hardware constraints (strapping pins, flash interfaces, USB, etc.).
89 * The platform detection below handles these differences automatically.
90 * If you need different pins, simply define PIN_RED, PIN_GREEN, and PIN_BLUE
91 * before the platform detection code.
92 */
93
94#include <FastLED.h>
95
96// Three strips, three different lengths -- e.g. a 60-LED strip, a 24-LED ring
97// and a 12-LED ring.
98#define NUM_LEDS_RED 60
99#define NUM_LEDS_GREEN 24
100#define NUM_LEDS_BLUE 12
101
102// The longest strip, so the animation below can walk past the end of the
103// shorter ones without indexing out of bounds.
104#define MAX_STRIP_LEN NUM_LEDS_RED
105
109
110// Platform-specific safe pin definitions
111// These pins are chosen to avoid strapping pins, flash pins, USB pins, etc.
112#if defined(ESP32)
113 // ESP32 variants - use universally safe pins that work across most ESP32 modules
114 #if defined(CONFIG_IDF_TARGET_ESP32C3)
115 // ESP32-C3: Avoid pin 9 (strapping), use safe GPIO
116 #define PIN_RED 2
117 #define PIN_GREEN 3
118 #define PIN_BLUE 4
119 #elif defined(CONFIG_IDF_TARGET_ESP32S3)
120 // ESP32-S3: Use safe pins that avoid USB-JTAG (19, 20) and flash/PSRAM (26-32)
121 #define PIN_RED 1
122 #define PIN_GREEN 2
123 #define PIN_BLUE 3
124 #elif defined(CONFIG_IDF_TARGET_ESP32C6)
125 // ESP32-C6: Avoid strapping pins 8, 9 and USB pins 12, 13
126 #define PIN_RED 2
127 #define PIN_GREEN 3
128 #define PIN_BLUE 4
129 #elif defined(CONFIG_IDF_TARGET_ESP32C5)
130 // ESP32-C5: Avoid strapping pins 2, 7, 25, 27, 28, USB pins 13, 14, and flash pins 16-22
131 #define PIN_RED 3
132 #define PIN_GREEN 4
133 #define PIN_BLUE 5
134 #elif defined(CONFIG_IDF_TARGET_ESP32H2)
135 // ESP32-H2: Avoid flash pins 15-21, strapping pins 2, 3, 8, 9, 25, USB-Serial-JTAG 26, 27
136 #define PIN_RED 0
137 #define PIN_GREEN 1
138 #define PIN_BLUE 4
139 #elif defined(CONFIG_IDF_TARGET_ESP32C2)
140 // ESP32-C2: Only GPIO 0-20 available. Avoid flash pins 11-17, strapping pins 8, 9
141 #define PIN_RED 0
142 #define PIN_GREEN 1
143 #define PIN_BLUE 2
144 #else
145 // ESP32 classic (WROOM-32, WROVER, etc): Avoid strapping and flash pins
146 #define PIN_RED 13
147 #define PIN_GREEN 14
148 #define PIN_BLUE 15
149 #endif
150#elif defined(ARDUINO_ARCH_RP2040)
151 // RP2040 (Raspberry Pi Pico): Most GPIO are safe, using mid-range pins
152 #define PIN_RED 10
153 #define PIN_GREEN 11
154 #define PIN_BLUE 12
155#elif defined(__IMXRT1062__)
156 // Teensy 4.0/4.1: Most pins are safe, using common GPIO
157 #define PIN_RED 2
158 #define PIN_GREEN 3
159 #define PIN_BLUE 4
160#elif defined(__MK66FX1M0__) || defined(__MK64FX512__)
161 // Teensy 3.6: Safe pins away from Serial
162 #define PIN_RED 2
163 #define PIN_GREEN 3
164 #define PIN_BLUE 4
165#else
166 // Universal fallback for other platforms (AVR, etc.)
167 // These are common digital pins on most Arduino boards
168 #define PIN_RED 6
169 #define PIN_GREEN 7
170 #define PIN_BLUE 8
171#endif
172
173// For mirroring strips, all the "special" stuff happens just in setup. We
174// just addLeds multiple times, once for each strip
175void setup() {
176 // Each addLeds() call gets its own array AND its own length -- they do not
177 // have to agree with each other.
178
179 // tell FastLED there's 60 NEOPIXEL leds on pin PIN_RED
180 FastLED.addLeds<NEOPIXEL, PIN_RED>(redLeds, NUM_LEDS_RED);
181
182 // tell FastLED there's 24 NEOPIXEL leds on pin PIN_GREEN
183 FastLED.addLeds<NEOPIXEL, PIN_GREEN>(greenLeds, NUM_LEDS_GREEN);
184
185 // tell FastLED there's 12 NEOPIXEL leds on pin PIN_BLUE
186 FastLED.addLeds<NEOPIXEL, PIN_BLUE>(blueLeds, NUM_LEDS_BLUE);
187
188}
189
190// Walk one dot along every strip at once. Because the strips are different
191// lengths, the shorter ones wrap around more often -- hence the modulo. That
192// is the whole trick to animating mixed-length strips: keep a separate index
193// per strip, never one shared index.
194static void moveDot(int step, const CRGB &color) {
195 int r = step % NUM_LEDS_RED;
196 int g = step % NUM_LEDS_GREEN;
197 int b = step % NUM_LEDS_BLUE;
198
199 redLeds[r] = color;
200 greenLeds[g] = color;
201 blueLeds[b] = color;
202 FastLED.show();
203
204 // clear our current dots before we move on
205 redLeds[r] = CRGB::Black;
208 delay(100);
209}
210
211void loop() {
212 for(int i = 0; i < MAX_STRIP_LEN; i++) {
213 moveDot(i, CRGB::Red);
214 }
215
216 for(int i = MAX_STRIP_LEN-1; i >= 0; i--) {
218 }
219}
void setup()
void loop()
FL_DISABLE_WARNING_PUSH FL_DISABLE_WARNING_GLOBAL_CONSTRUCTORS CFastLED FastLED
Global LED strip management instance.
CRGB redLeds[NUM_LEDS_RED]
#define PIN_GREEN
CRGB blueLeds[NUM_LEDS_BLUE]
#define NUM_LEDS_RED
#define PIN_BLUE
#define MAX_STRIP_LEN
#define NUM_LEDS_BLUE
CRGB greenLeds[NUM_LEDS_GREEN]
static void moveDot(int step, const CRGB &color)
#define PIN_RED
#define NUM_LEDS_GREEN
fl::CRGB CRGB
Definition crgb.h:25
@ Red
<div style='background:#FF0000;width:4em;height:4em;'></div>
Definition crgb.h:629
@ Blue
<div style='background:#0000FF;width:4em;height:4em;'></div>
Definition crgb.h:519
@ Black
<div style='background:#000000;width:4em;height:4em;'></div>
Definition crgb.h:517