Hello. I'm working on a display for a school performance this weekend. I thought this would be a very simple project. It's 5 LEDs that flicker.
The effect itself is perfect at first, but after a short while the LEDs stop changing. It freezes in this state for a minute, and then the effect starts playing at a much faster rate. After this point, it flips back and forth between freezing and flickering quickly each minute or so.
The code is attached. Why is the execution speed changing so much?
I'd really love to have the project working for the performance in a couple days, so all help is greatly appreciated.
const int ledPin[] = {3, 5, 6, 9, 10}; // all PWM pins
int ledState[5];
long randNumber;
int flickerTime = 0;
const int flickerDelay = 0;
void setup() {
for (int i=0; i<=4; i++) {
pinMode(ledPin[i], OUTPUT);
}
randomSeed(analogRead(0)); // seed via unconnected pin noise
}
void loop() {
if (flickerTime < millis()) {
for (int i=0; i<=4; i++) {
if (ledState[i] == 0) { // if off, maybe flip back on
randNumber = random(0, 255);
if (randNumber == 1) {
ledState[i] = 250;
}
} else {
ledState[i]--; // fade out
}
if (i == 2) {
ledState[i] = 255; // don't flicker center light
}
analogWrite(ledPin[i], ledState[i]);
}
}
flickerTime = millis() + flickerDelay; // adjust timing
}