Mapping a Key on the Keyboard to Exit Loop in Arduino

Hello all,

I want to create a function on the Arduino and put it in all the loops in my program such that whenever during use, the user can press the "Esc" key on the keyboard to cancel what it is currently doing. A serial monitor is being used with my current code.

My entire code is very long which is why I don't have it posted, but if you can answer the situation i described above, I will be able to implement it. You can imagine a simple loop running forever that will exit anytime the "Esc" key is pressed.

Thank you

If you want to break out of the current loop, and were using a button connected to an arduino pin ( not the PC keyboard as you require) you could do it as follows:

Define a global boolean variable esc initially set to false.
Define an interrupt service routine which sets esc to true.
Associate the pin the button is connected to with the ISR above.
Define a boolean function isEsc() which returns the value of esc and resets esc to false, if not already false.
In each loop (apart from the loop() function itself) include the statement:
if ( isEsc() ) break ;

Maybe this helps, but you now have to solve the problem of using a button on the host keyboard.