Reputation: 17
I am trying to build a lightboard of LEDs to act as a timer. The idea would be that an array of 200 LEDs attached to one Arduino pin (pin 3) going at one millisecond per LED runs in a loop, with a second array of 20 LEDs attached to a second pin (pin 10) runs at 1/10 of a second, so that up to two seconds of time can be counted through the board.
#include <FastLED.h>
#define NUM_LEDS_PER_STRIP 200
#define NUM_LEDS_COUNTER 20
CRGB leds[NUM_LEDS_PER_STRIP];
CRGB counts[NUM_LEDS_COUNTER];
void setup() {
FastLED.addLeds<WS2811, 3>(leds, NUM_LEDS_PER_STRIP);
FastLED.addLeds<WS2811, 10>(counts, NUM_LEDS_COUNTER);
}
void loop() {
for(int x = 0; x < NUM_LEDS_COUNTER; x++) {
for(int i = 0; i < NUM_LEDS_PER_STRIP; i++) {
leds[i] = CRGB::White;
FastLED.show();
leds[i] = CRGB::Black;
delay(1);
}
counts[x] = CRGB::White;
FastLED.show();
delay(10);
}
}
Right now, my code works, except for two issues: I can't figure out how to get my bottom loop of 20 LEDs to reset (i.e. have them all turn off and restart from the first one again), and there is a delay while the second array of LEDs turns on an LED, which leads to the timing of the entire board being off.
I think this has to do with my using the delay() method, but I can't figure out how to get millis() to work with the array of LEDs. Or maybe I'm missing something else?
Can anyone help me figure out how to fix these two issues in my code?
Thanks!
Upvotes: 0
Views: 357
Reputation: 1772
You can use millis(). For a better understanding check Blink Without Delay
#include <FastLED.h>
#define NUM_LEDS_PER_STRIP 200
#define NUM_LEDS_COUNTER 20
CRGB leds[NUM_LEDS_PER_STRIP];
CRGB counts[NUM_LEDS_COUNTER];
#define INTERVAL_1 1000
#define INTERVAL_10 10000
unsigned long time_1 = 0;
unsigned long time_10 = 0;
void setup() {
FastLED.addLeds<WS2811, 3>(leds, NUM_LEDS_PER_STRIP);
FastLED.addLeds<WS2811, 10>(counts, NUM_LEDS_COUNTER);
}
void loop() {
unsigned long currentMillis = millis();
for (int x = 0; x < NUM_LEDS_COUNTER; x++) {
if (currentMillis - time_10 >= INTERVAL_10) {
time_10 = millis();
for (int i = 0; i < NUM_LEDS_PER_STRIP; i++) {
if (currentMillis - time_1 >= INTERVAL_1) {
time_1 = millis();
leds[i] = CRGB::White;
FastLED.show();
leds[i] = CRGB::Black;
}
}
counts[x] = CRGB::White;
FastLED.show();
}
}
}
Upvotes: 0