Time input, start/stop time to trigger a solid state relay

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;
  }
}
1 Like

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.

1 Like

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 :wink: )

1 Like

@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.

1 Like

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.

1 Like

Choice comes down to ā€œSix of one, half dozen of the otherā€ :wink:

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 :stuck_out_tongue: 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.