Last active
August 18, 2022 09:12
-
-
Save rahuldottech/839cb13140f24debae8f72343115de4b to your computer and use it in GitHub Desktop.
[Arduino] buttonWait - wait for button press before further execution.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
buttonWait v0.1 by rahuldottech | |
--------------------------------- | |
(pause a script till a button is, y'know, *pressed*...) | |
This code is in the public domain. | |
Proper schematic at https://arduino.stackexchange.com/a/56805/50258 | |
-- | |
Circuit Diagram: | |
[pin]----|--------------[button] | |
| | | |
[10k resistor] | | |
| | | |
[ground] [vcc] | |
-- | |
Usage: | |
buttonWait(<pin_number>); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* the function */ | |
void buttonWait(int buttonPin){ | |
int buttonState = 0; | |
while(1){ | |
buttonState = digitalRead(buttonPin); | |
if (buttonState == HIGH) { | |
return; | |
} | |
} | |
} | |
void loop() { | |
// put your main code here, to run repeatedly: | |
buttonWait(2); // wait for button press on pin 2 | |
// do something | |
// ... | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* the function */ | |
void buttonWait(int buttonPin){ | |
int buttonState = 0; | |
while(1){ | |
buttonState = digitalRead(buttonPin); | |
if (buttonState == HIGH) { | |
return; | |
} | |
} | |
} | |
void setup() { | |
// put your setup code here, to run once: | |
pinMode(LED_BUILTIN, OUTPUT); // initialize digital pin LED_BUILTIN as an output. | |
} | |
void loop() { | |
// put your main code here, to run repeatedly: | |
buttonWait(2); // wait for button press on pin 2 | |
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on | |
delay(1000); //wait for a second | |
buttonWait(2); // wait for another button press on pin 2 | |
digitalWrite(LED_BUILTIN, LOW); // turn the LED off | |
delay(1000); //wait for a second | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
how did you solve it?