Hi, I'm looking for some advice. I've written the base program for a motor control sequence box. Its fairly basic with the functions to control the direction of a motor at a pre determined speed. The other function is to turn the motor several revs in one direction then change and repeat in the other direction. This is easily done using a sequence of commands, delays etc. The other option is to run it through a count loop with a second delay representing a timer. What I need to do is at some point through this function cancel and return to the main menu. If I use delays I find nothing can be done until the whole function is carried out. And if I use the count loop I'm struggling also to exit? What other ideas can I look at to cancel these loops and return to a main menu?
The important thing to keep in mind is that the loop() function is named that for a reason - because it loops. Let it handle all the looping. On any given pass through loop(), it may be necessary to step, or not. If it is, it may be time to step, or not. if it is, step and record when you last stepped.
No need to delay() and no need to have big for loops that seemingly never end.
Thanks I'll keep this in mind.
Using millis() for timing allows you to read inputs on each iteration of loop() with no blocking code to slow things down.
Another thought. if the motor is executing a series of commands then create a state machine with one state for each step. switch/case is useful for this as you can have "delay" states that use use millis() for timing and read inputs outside of the state machine in the loop() function.
Many Thanks that's given me more to consider, Excellent tutorial. Thanks