Last active
September 1, 2022 10:32
-
-
Save megasaturnv/eb14faa2e0c9e23662e9aa2f9db79338 to your computer and use it in GitHub Desktop.
Arduino simple digitalRead with software debounce
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
//Megasaturnv 2017-07-05 | |
//Arduino simple digitalRead with software debounce | |
//Digital read an pin with software debounce. Pin should be set as: pinMode(pin, INPUT_PULLUP) in setup(). | |
//Arduino ATMEGA328P (and some other ATMEL chips) have internal pull-up resistors. Pin is active when connected to GND | |
//Projects where pins are active high (with pull down resistors) should modify lines 7 and 11 below, as indicated | |
//This function will return true if input is active for at least 50ms and then released. The function will return false if input is not active | |
bool digitalReadDebounce(int pin) { | |
if (digitalRead(pin) == LOW) { // <- Change to '== HIGH' if pin is active high | |
delay(50); | |
bool debounceRunning = true; | |
while (debounceRunning) { | |
if (digitalRead(pin) == HIGH) { // <- Change to '== LOW' if pin is active high | |
debounceRunning = false; | |
} | |
} | |
delay(50); | |
return true; | |
} | |
return false; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment