A post was split to a new topic: I need to set timers for relay with esp-01
Are you asking about more details on how to stagger calls using a single timer, or something else?
For staggering the calls:
Looking at your last code posted, it appears that you want to do 3 actions, each of them once per second, and a fourth every 20 seconds.
You have 4 events to time with a granularity of 1 second so you would have a single timer time out 4 times per second. Then, you would use a global variable as an index to determine which one of the events to call on each timeout. For the 20 second event, you would have another global variable to count 20 seconds.
unsigned char eventNumber = 0;
unsigned char temperaturaCount = 0;
void setup() {
timer.setInterval(250L, eventTimer);
}
void eventTimer() {
switch (eventNumber) {
case 0:
led_control();
break;
case 1:
MeasureCm();
break;
case 2:
if (++temperaturaCount == 20) {
temperaturaCount = 0;
Temperatura();
}
break;
case 3:
EncendidoSumpLamp();
break;
}
if (++eventNumber > 3) {
eventNumber = 0;
}
}
WOW @MLXXXp doing this I only use 1 timer for 4 process and all Iāll occur in one sec. very impresive!!! so will never flood the server doing this in 250 mSecs? Iām understanding right
Neat, elegant, simple and wonderful way of staggering timer calls. Everyday brings some good learning here.
As long as the longest function called is completed before the 250ms is up⦠otherwise it is absolutely no different then improperly staggered timers (and no better than properly staggered ones )
@mohan_sundaram Iāll try it, @Gunner I understand what are you talking about, if I have a function that last mor than 250 ms I presume that I can increase that number until get it work right? Indeed everyday brings some good learning here from all of you guys. Itās been a nice learning.
It could be better or it could be worse, in terms of code execution efficiency. It depends if, overall, the code to scan, and possibly handle, 4 timers instead of 1, each time timer.run() is called in loop(), takes longer than the switch statement in eventTimer() takes to run only each time the single timer times out. Iād bet on the switch statement taking less time.
The single timer method will probably use less RAM as well, since (in my example) it only needs 2 bytes plus the RAM to allocate 1 timer, instead of the RAM to allocate 4 timers.
Also, the single timer method guarantees that the events will always execute in the same order, which could be important in some cases.
And, the effort to come up with, and possibly have to ātuneā, the time values to properly stagger 4 timers isnāt required with the single timer method.
Choice comes down to āSix of one, half dozen of the otherā
But I prefer timers for the flexibility⦠Interval timers (adjustable/countable loops or infinite) and/or Timeout timers, ability to enable/disable, trackable⦠And so on.
I have 11 constant interval timers, two more set to enable/disable as needed and I canāt remember how many timeout timers for various counting, servo, graphic effects etc⦠even one lambda function timer just so I can have it in the sketch for easy reference.
None of that could be easily done with switch case or for() loops. And all in a single sketch on my MEGA⦠all running perfectly and under 44K storage and 2.7K dynamic.
Now this is not saying anyone has to use timers Just that they work great, are part of Blynk and should be recommended to new users for continuity sake when learning Blynk.
Yes⦠same rule applies for most any method used and with any timing sensitive computation on a non-realtime OS⦠Wherever possible, finish whatever dedicated function you start before moving onto the next⦠otherwise the MCU will try to do both (probably interlacing between commands), but can cause strange results.