Every N Seconds …
For the extremely brave, some experimental new FastLED additions… Just added to the v3.1 branch on github: some helpers to perform tasks periodically, e.g., every ten seconds. The simplest way to use the new functionality is with the new “EVERY_N_SECONDS” macro, that acts a bit like an “if” statement:
void loop() {
EVERY_N_SECONDS(10) { chooseNewPalette(); }
…
}
This mechanism does not use interrupts, and has to be executed periodically to work, i.e. as shown in the “loop” function above. It uses 4 bytes of RAM for each independent ‘Every N Seconds’ timer; you can have as many different ones as you wish.
You can also use these periodic triggers like this, without the helper macros. You have to declare a static instance of the ‘Every N Seconds’ class, initialize it with the period you want, and then you can just test it with an “if” statement to see if the period has come around again:
void loop() {
static CEveryNSeconds newPatternTime(10);
…
if( newPatternTime ) {
changePattern();
}
}
The nice thing about that is that you can integrate it with other triggers, as in:
if( newPatternTime || buttonPushed ) {
changePattern();
}
There are a few variations, including EVERY_N_MILLISECONDS.
The underlying time base is the Arduino millis clock, which drifts a fair amount, so don’t use these timers for rocket science, but they work nicely for making periodic changes to an LED animation.
There’s no sample code yet (other than this, above), but it’ll come in the next few days, along with whatever bug fixes are needed.
Enjoy, and let us know what you find. So far from my testing, it looks like this is going to simplify things a bit.