Many thanks Pommie. I already use a global variable to keep the code inside the while clause running unless the switch is pressed, and by checking the flag again I realised that part was correctly done, but a few lines later I could see that my error was that I entered the while clause without actually exiting the interrrupt handler function, so it could not be called again after the next press and release of the button. By letting the handler function finish and changing the place in code where the function is called now the while clause is stopped after pressing the button as expected.