I like the function EVERY_N_MILLISECONDS, but is there something like "EVERY_N_MILLISECONDS_PLUS_M"?

I like the function EVERY_N_MILLISECONDS, but is there something like “EVERY_N_MILLISECONDS_PLUS_M”?

For example I want to execute A every 100ms.
And I want to execute B 50ms after A.

I know I could use delay(50) inside the EVERY_N_MILLISECONDS function.
Or I could define a helper variable and check milis() to program my own solution.

But maybe there is a smart way I don’t know yet. :slight_smile:

Otherwise I would suggest a feature requast for the next FastLED version.
Like an optional second parameter:
EVERY_N_MILLISECONDS(100, 50)

You could have the action A reset a timer (use elapsedMillis data type) to zero and also set a boolean flag to true, then check on a cycle (main loop or every 5ms or whatever works for you) if the timer is past 50ms and the flag is true, and if so set the flag to false and do B. So in code in the main loop:

EVERY_N_MILLISECONDS(100){
flag=true;
timer=0;
//do A here
}
EVERY_N_MILLISECONDS(5){
if(flag==true && timer>=50){
flag=false;
//do B here
}
}

Sorry for messy code and not using pastebin or similar, I’m doing this on a tiny mobile screen. Also may have missed something. Finally I want to say there are easier ways of doing this in the cases where the interval of A is a multiple of the delay of B, but this works for general cases.

EDIT: also define flag as a boolean and timer as elapsedMillis, both at the top of the code. ElapsedMillis counts up automatically so you don’t need to do that yourself.

Thanks.
I already programmed similar code now, but I didn’t know about elapsedMillis. :slight_smile:

EVERY_N has some other interesting stuff you can do with it. Here’s a rather random example file I made to test out some of it’s options.

And a second example that does something like what you asked about here, plus it has an example of using EVERY_N with a variable timer. To get “B” to execute at a specific time after “A” I am resetting a timer whenever “A” is run, and then checking if enough time has passed to before running “B”.

@marmil Wow, I guess that’s a more elegant solution.