Those are typically the interval timers… they constantly run at set intervals throughout the entire scripts runtime… thus the setup is a logical place to put them.
They could technically go anywhere, but then they might reset each time the code reaches them… effectively messing up the timing.
Other timers like timeouts can be placed wherever… just be aware that they don’t stop and wait for something to happen, so you have to be cognizant of timeframe when processing multiple timers to do advanced things.
E.g. Here is a crude “Open latch then flash LED x times” routine… change one timer without accounting for the rest can mess everything up.
It uses both setTimeout
and setTimer
types:
//===== LATCH(Relay Sim) & LED PULSE - BLYNK Functions =====
BLYNK_WRITE(V40) // Virtual button on V40 to activate Relay_LED pulses
{
RLY_LED = param.asInt();
if (RLY_LED == 1 && Flag == 0){
Flag = 1; // Keeps from allowing button press more then once while relay activated
digitalWrite(30, HIGH); // Activate Relay
timer.setTimeout(2000L, Relay2OFF); // Deactivare Relay after 2 seconds
timer.setTimer(2000L, LedON, 5); // Pulse LED routine (LedON/LedOFF) 5 times
}
}
void Relay2OFF()
{
digitalWrite(30, LOW);
Flag = 0; // reset flag after relay disables
}
void LedON()
{
digitalWrite(31, HIGH); // Turn on LED
timer.setTimeout(1000L, LedOFF); // Run OFFLED routine once in 1 second
}
void LedOFF()
{
digitalWrite(31, LOW); // Turn off LED
}