Can someone explain the use of EVERY_N_MILLISECONDS ?

Can someone explain the use of EVERY_N_MILLISECONDS ? Can it be used only once per program? I’m seeing very odd behavior with the use of this macro and am curious what the limitations are.

Use it as many times as you like in your program as long as it’s in functions that are called frequently, eg in the “loop()” function. I often have some 1-second timers and some 20-ms timers and some 60-second timers all at once.

What’s your code look like and what are you seeing?

It’s a compiled macro, right? So you can’t modify the amount?

By default, the value is established at compile time, yes.

However, there are variants, EVERY_N_MILLISECONDS_I, EVERY_N_SECONDS_I, etc. that let you have access to the ‘timing’ wrapper object, so that you can change the timing, etc.

E.g.: this code shows how to change the timing from inside the body itself. In this case, the body will execute once every 5-25 seconds (randomly selected).

EVERY_N_SECONDS_I( timingObj, 10) {
// defaults to every 10 seconds, but this command
// will change the period to a random number
// of seconds from 5 to 25:
timingObj.setPeriod( random8( 5, 25) );

// ... now do whatever...
// ...

}

Ah, that’s right, forgot about that, thanks!

Oh that’s cool, thanks @Mark_Kriegsman ​. What’s the need for a default value, and what would cause it to be used?

That’s for the first time through the loop, and because you might not (always) tinker with the period.

For some reason I ran into glitches when using that macro more than once in the main loop. so what I ended up doing was rewriting all my functions to accept an unsigned long static variable passed to it that would hold a micro time mark for timing purposes.

@Myche_Tician Do you have a code example that was giving you glitches? (Post it to pastebin.)

I’ve rewritten my core code a bunch of times. What I basically had was two instances of EVERY_N_MILLISECONDS() that appeared to glitch. When I just used one, it was ok. It may have been an issue in my code, but i guess the use of macros is kinda of frowned upon in any extensive way, so I decided to start modifying all my routines to accept a static variable that will be used as a timing mark. I’ll make a new post about my approach towards writing multi-tasking code and see what other people think - right now i want to hash out a few routines for a sign I’m working on that has to go up.

Were they nested or something?
I’ve had no problems with half a dozen all going at once.

It would help me improve the library and documentation if you could show what you were trying that didn’t work. Happy to help however I can.

Next time I run into anything i’ll save code examples.

Great, thank you!