Love the new "EVERY_N_MILLISECONDS"! Is it possible to do something like this?:

Love the new “EVERY_N_MILLISECONDS”!
Is it possible to do something like this?:
EVERY_N_MILLISECONDS( a_dynamic_maybe_even_random_variable ) {
something();
}

Yes! I’ll post example code later tonight if no one else has by then, when I have better internet access.

Short version: use EVERY_N_MILLIS_I – note the “I” on then end. This takes TWO arguments, so you choose a ‘name’ for this timer, and set the initial value. Then you can adjust the period for the timer inside the body:

EVERY_N_MILLIS_I( thistimer, 100 ) { // initial period = 100ms
thistimer.setPeriod( random8( 10, 200) ); // random period 10…200ms
…do whatever…
}

Check out “CEveryNTimePeriods” in lib8tion.h for more info on what you can do when you have access to the timer object.

Also note that the native name is EVERY_N_MILLIS_I. I just added EVERY_N_MILLISECONDS_I as an alias in the github repository on the 3.1 branch.

Freaking awesome! Thanks again :wink:

@Mark_Kriegsman is there also something like EVERY_N_HOURS ? I’d like to use it on an aquarium installation…

Yep! Check lib8tion.h. I think there are millis, seconds, minutes, hours, and days.

Bear in mind that the onboard clock isn’t terribly accurate (off by minutes a day).

Thanks a lot Mark! Hope the fishie’s internal clock is even less accurate :wink:

If you want to provide an external time source (other than the on-board clock, which is pretty bad), you can, by doing this:
#define USE_GET_MILLISECOND_TIMER 1
then you can provide a function matching this signature
uint32_t get_millisecond_timer()
{
… read your real-time-clock, and convert to millis …
return clockmillis;
}

All the FastLED timer (and beat) functions will then use that as their time source. If I hadn’t hiked a 2,800-foot ascent up a mountain today, I’d post sample code… maybe if I recover, over the weekend…