I have a system running which controls a couple of motors. I can set speed and direction with no problem and, of course, I can stop/start or change the speed or direction at any time.
I can also ramp speed up or down at a given rate within a loop. However with a simple loop, there's nothing I can do to change the motor speed or direction (or stop it) while the simple loop ( for loop or while loop) is executing.
What would be the general principle (using an Arduino MEGA) behind having ramping functions which could run independently while the overall program was doing something else (looking at key presses, reading sensors etc.) and having the loop interupted if an event took place which required a change in speed or rotational direction of the motors?
Take the body of the for-loop and place it in a function; I called it ramp(). It will return a bool to indicate when one ramp was completed. Add a static variable to keep track of count.