Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
11 years, 8 months ago.
How to escape from a loop immediately through pressing of non latching button?
How would I escape from a continuing loop prematurely and immediately through the press of a button. Even if the process that is being carried out is even a very long wait function. Basically I would like to get out of the while loop early and for it to discard what it was doing beforehand.
Many Thanks
Kev
1 Answer
11 years, 8 months ago.
If it is busy with a wait function, there is not a nice way to 'kill' that function. One option you can look at is using an InterruptIn for that button, and in the interrupt handler do whatever needs to be done. But if that is an option depends on your program.
Another thing you can do, is make your wait function like this:
Timer timer; while(1) { //Big loop timer.reset(); timer.start(); while (timer.read() < 1) { //Wait one second if (button == 1) //Stop our wait function is button is pressed break; } if (button == 1) //Also exit the big loop break; }