I am trying to have a button which, once pushed and held still, immediately stops the board from what its doing and instead makes it do something else. The code below is the only method that I came up with. But the only problem i see is that the code below would behave too slowly. Is there some method out of this? I'm using a pull-up resistor, the same setup in here:
void loop() {
val = digitalRead(inPin);
if(val== HIGH){
//does something else
}else{
while(....){
//takes considerable time for this loop to finish approx 5 seconds
}
}
If you don't know about interrupts have a look at this http://arduino.cc/en/Reference/AttachInterrupt. You can code your interrupt to detect the button press (may need to account for button boucing though so it doesn't call the interrupt over and over while it's bouncing, and interrrupts don't like delays() FYI-so millis), and then in the body of your interrupt function (the curly braces part {}) you can tell it to do something else. A simple switch case statement has worked well for me in this type of need...so the Arduino is always "listening" for the interrupt and when you press the button it goes to your interrupt service routine (ISR), goes to the swicth case statement and selects the other function you want it to go to immediately.