kersny

I need to put a button in my arduino that will pause all code until the button is pressed.I need to not let the pin in the loop turn on, until a switch is pressed. After that, the switch doesn't matter at all.Here is my code:

int swPin = 8; // set this to the switch pin pinMode(swPin, INPUT); digitalWrite(swPin, HIGH); // turn on pull-up resistor while(digitalRead(swPin) == HIGH) ; // do nothing while the switch is not pressed // code will get here when swPin is switched to ground

attachInterrupt(1, Dist, FALLING);}

Also, the delay of 100000 in the interrupt handler is not a good idea. Better to set a new variable to true when the interrupt occurs and do a digital read in loop() and delay if that variable is set to true.

kersny

This doesn't seem to be working for me. I apply power, wait ~ 10 sec. (I have an NG rev.c) and press the button, and nothing happens? All I added was a switch between pin 8 and ground. I didn't think I needed to pull it up since you used the internal pullups.

PS. I know the delay isn't ideal, but I will be turning the system off shortly after it stops, so I don't really worry about it.

int swPin = 8; pinMode(swPin, INPUT); digitalWrite(swPin, HIGH); // turn on pull-up resistor digitalWrite(ledPin,HIGH); // turn on the LED so you know the code is waiting for the switch while(digitalRead(swPin) == HIGH) ; // do nothing while the switch is not pressed // code will get will get here when swPin is switched to ground, so the LED will turn off digitalWrite(ledPin, LOW);