BLYNK
HOME       📲 GETTING STARTED       📗 DOCS       ❓HELP CENTER       👉 SKETCH BUILDER

Using of Simple timer library

Hi, Blynkers.
I would want to consult about using the SimpleTimer (BlynkTimer) library.
Let is needed to call for several functions with the same period:
SympleTimer (or BlynkTimer) timer;

Setup ()
{ 
  //... 
  Timer.setInterval (20000L, syncRTC);
  Timer.setInterval (1000L, func_1);
  Timer.setInterval (1000L, func_2);
  Timer.setInterval (1000L, func_3);
}

In this case, the summary must be less than 1000ms.
Is it right?

no, because mcus usually can’t do multitasking. so they will be executed one after the another.

if you read carefully the simple timer docs, it wrotes that they can not guarantee the timed functions that exactely. they just guarantee it won’t happen faster:

“It uses polling, so no guarantee can be made about the exact time when a callback is fired. For example, if you setup the library so that it calls a function every 2ms, but this function requires 5ms to complete, then you’ll have an invocation every 5ms.”

practically, with simple timer the code will be executed in the best case right after the timeout, or if the mcu is doing something else, after that.

think fifo method.

but you can visualise this very well, if you put a Serial.println(millis()); as the last line of every simple timer function. you will see they are executed one after another, not in the same time.

Thank You, wanek.

Of course, I understand this. In my example abow I wanted to say that if is needed
to call with sertain different intervals a several function the minimum period of calling any function must be either equel or more then summary duration execution of all these function

ah, ok, i didn’t understand what are you asking. sorry.

the minimum calling period actually it does not matter: if you set it too low, than anyway will not be executed only after the other tasks are completed. it won’t break anything in the program flow, if you mind this.

But It’s not quite true. In my program there are for functions whiches are called with using timer.setinterval(xxxL, me_func). So for one of them, the call interval was setted 1000ms. This function worked not stable. But when I incresed call interval up to 2000ms function now works still good. Hence I think that this function sometime could not be ended correctly - its work mey be interrupted the calling of next function. How You think, it mey be cause?

hmm, very interesting what you observed. theoretically it should not cause the issue, as stated in the official docs. could you share that function here what produced this simptome?

“For example, if you setup the library so that it calls a function every 2ms, but this function requires 5ms to complete, then you’ll have an invocation every 5ms.”

As I have experienced, this will generally work, but may cause stuttering and/or missed action… depending on the functions each timer will call…

Each timer is called, one after another, in the order set, but as each additional function is added to the MCU’s “todo” list, additional delays in timing can occur as they are all “expected” to run at the same time…

Unless the timing is absolutely critical, particularly over longer time periods (as these adjustments are cumulative), I generally space my timers out by a couple of ms. It seems to help with a seemingly smoother operation when running multiple functions.

Timer.setInterval (995L, func_1);
Timer.setInterval (1000L, func_2);
Timer.setInterval (1005L, func_3);

For any action that requires some degree of precision, but doesn’t always need to run 24/7 in the background, then I have the timers as precise as required, but only enable and disable them as needed.

timerid = Timer.setInterval (1000L, func);

timer.enable(timerId);  // Enables the specified timer

timer.disable(timerId);  // Disables the specified timer
2 Likes

Unfortunately it is impossible becose my program is very big.

Perhaps it is exactly my case…:slight_smile:

Thank You all.