Using interrupts to pause and restart sketch

I'm trying to implement an interrupt based 'press button to pause and press button again to continue' routine. The only way I could think of was to have the ISR toggle the status of a flag which would then be polled on every iteration of the main loop (or whichever routine needed to be paused in the sketch).

Once the status flag is detected then the pause is implemented by sending the sketch into an infinite loop while the flag is set.

My question is - is there a better way of doing this?

What is it you are wanting loop to not execute for a while?

A simple switch read and decision should be sufficient.

If your main loop is not doing anything else useful then there is no real reason to have an interrupt handle the switch presses. A simple loop operation is still faster then any human would notice.

Just have your main loop do the digitalRead on the switch (don't forget to do switch debouncing) and take appropriate action on change of state of the switch input.

The interrupt method is best used if the main loop had lots of other things being done and/or had lots of delay statements or other blocking commands that would make testing the switch a possible sluggish situation.

Lefty

Thanks retrolefty - from a timing point of view my sketch would not suffer by implementing a continuous poll of the 'pause' button.

PaulS - I just wanted the sketch (or loop) to stop indefinitely, while waiting for the button press to restart it from that point. Thanks also.

Having said that, would the method I described be the most efficient way of using an interrupt for this purpose? I couldn't find any forum posts dealing with this unfortunately.

I think that you need to read the responses, again. There is no reason to use an interrupt to do what you want.

If you insist on using interrupts anyway, read up:

http://arduino.cc/en/Reference/AttachInterrupt

Pseudo-code for an interrupt-less "stop program/ start program" button...

The function "buttonPressed", which returns a boolean, would be written by the user. It would return "true" if a button were pressed at the time it was called. In more detail: It would look at a button, and if it were pressed, would wait a short time (to let bounces pass, in case it looked JUST as the button were pressed, then linger in the function until the button was released, then linger a bit MORE to let the bounces occuring while the button is released pass. Whew! All that for one little routine! The bigger picture is....

(We go into the loop in a "run the program" state)

void loop ()
{
if buttonPressed then {
while buttonPressed==false
{//do noting.. just go 'roind and round this
// inner loop until button pressed again
}
}//end of if buttonPresseed then....
DoGoodStuff <- this (and as many following lines as
you require) is where the things go for making the
program do what you want it to do when it is "running".
}// end of main loop

Hope that helps? The Arduino is, of course, "running" ALL OF THE TIME... but part of the time we have it going around and around, not appearing to do anything.

This program will only work of the DoGoodStuff part takes only a very short time to happen once. If it takes, say, half a second, then users will find that the Arduino doesn't stop as soon as they press the button. But all they have to do is be a bit patient. Push the button down, wait, and in due course the program will "stop". Well, stop doing the DoGoodStuff part, and enter the inner loop.