I finally completed my first Arduino sketch project with your help.
I do have a question though, to expand this first project.
As we already know the "Loop" section of a script acts like a timer, it takes a measured amount of time to complete the list of commands in the script, once it reaches the bottom of the script then "Delays" the commanded milliseconds, followed by returning to the top of the loop to start over.
I need to be able to have several commands inside the main loop execute every 6 minuets while the rest of the loop continues to loop at about every second.
Is it possible to have two loops operating at different delays like that? If so what prevents the outside main loop from resetting the inside loop that is running much slower?
Your phrasing needs to be a bit different, but yes it’s doable.
Example
// l o o p ( )
//================================================^================================================
//
void loop()
{
//======================================================================== T I M E R heartbeatLED
//condition returned: STILLtiming(0), EXPIRED(1) or TIMERdisabled(2)
//is it time to toggle the heartbeat LED ?
if (heartbeatTIMER.checkTIMER() == EXPIRED)
{
//toggle the heartbeat LED
digitalWrite(heartbeatLED, digitalRead(heartbeatLED) == HIGH ? LOW : HIGH);
}
//======================================================================== T I M E R switches
//condition returned: STILLtiming(0), EXPIRED(1) or TIMERdisabled(2)
//is it time to check our switches ?
if (switchesTIMER.checkTIMER() == EXPIRED)
{
checkSwitches();
}
//======================================================================== T I M E R machine
//condition returned: STILLtiming(0), EXPIRED(1) or TIMERdisabled(2)
//is it time to service our State Machine ?
if (machineTIMER.checkTIMER() == EXPIRED)
{
checkMachine();
}
//================================================
// Other non blocking code goes here
//================================================
} //END of loop()
Not really. Those commands do take a little time to complete. But it's very difficult, impossible for a beginner, to know exactly how long they will take. So it's not a great way to make a timer.
Only if you put a delay() at the bottom of loop()! You don't have to have one. Generally only beginner/example code will have that. As soon as your coding gets more advanced, you realise that having a delay() in loop() causes problems.
You can only have one loop() (on almost all types of Arduino). But it's not a problem, you can do what you wanted, and much more, with just the one loop().
You would not use delay() to make your 6 timers. Only one delay() can be running at any time. Instead, you would use a different function: millis().