Using a timer interrupt to exit out of a method


So I have a device that controls three stepper motors to dispense solution from syringes into centrifuge tubes and I am initiating the code to do so via an LCD assembly that acts as a serial monitor.

What I want to do is create a "Cancel" button on the LCD that will stop the execution of the method to dispense the solutions. The problem with making a global flag and checking for it is I use the step() method within my own, and I would not be able to check for this global flag until the step method returns, but I want to immediately suspend execution and return to the code in Loop() after I press my cancel button.

Currently, I am using a timer interrupt to check for any button presses every 150 ms. What I am struggling with is what to do once the button is pressed. How can I just quit execution and go back to my loop?

// Method called from Loop() void dispenseToAll(int soln, float vol) { Timer3.initialize(150000); Timer3.attachInterrupt(exitDispense);

Serial1.print("\f"); Serial1.print("Dispensing Solution"); moveToSoln(soln); moveToSample(1); dispense(vol); moveToSample(2); dispense(vol); moveToSample(3); dispense(vol); moveToSample(4); dispense(vol); moveToSample(5); dispense(vol); moveToSample(6); dispense(vol); findTop(); findSampHome(); findSolnHome(); }

void exitDispense() { if(Serial1.available()) { char input =; if(input == '\n') {

} } }

What I am struggling with is what to do once the button is pressed. How can I just quit execution and go back to my loop?

loop() should be the worker thread (loose use of term) for all your program. Calls should be made out to functions to perform tasks on behalf of the loop. If an interrupt sets a flag while you are in a function, then you can check the state flag in loop() after the function return and before the next function call. Use "return" inside the conditional test of the flag variable to shortcut processing in the loop().

Please see,148850.0.html. Please pay special attention to:

  • Item #7, "If you are posting code or error messages, use "code" tags," and
  • Item #11, "Tips for getting the most out of your post." In particular, please note:

Post a complete sketch (program code)! If you don't, you waste time while people ask you to do that. However, with coding problems, if possible post a "minimal" sketch that demonstrates the problem - not hundreds of lines of code. If the problem goes away in the minimal sketch, it wasn't where you thought it was.

I think that "step() method" refers to the Stepper library provided with the IDE. I interpret your question to be, "How do I make step() terminate early?" One way is to call step() with the smallest reasonable argument, keep track of the number of steps and the current position with your own code, and check for a termination flag between calls to step(). Your button-press won't get an instant response, but it'll be fast, and step()'s record-keeping will match the motor's state as well as it does with multiple steps.

Another way is to modify step() to internally check the termination flag, and return when it finds it set. You'd want to make sure that step() terminates in an orderly fashion, with its record-keeping in sync with the motor's state.