From 9922abe2f1b5223ba38240c648d2e3af4b4e2d9f Mon Sep 17 00:00:00 2001 From: Sam Anthony Date: Thu, 21 Nov 2024 21:05:47 -0500 Subject: sensor station: button debounce --- SensorStation/SensorStation.ino | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/SensorStation/SensorStation.ino b/SensorStation/SensorStation.ino index 46f792d..75f0f39 100644 --- a/SensorStation/SensorStation.ino +++ b/SensorStation/SensorStation.ino @@ -12,7 +12,10 @@ enum pins { }; enum { SECOND = 1000, - PERIOD = 30*SECOND, + + PERIOD = 30*SECOND, // Humidity sample period. + + DEBOUNCE = 50 // Button debounce time. }; const char ssid[] = "Pixel_6504"; @@ -134,26 +137,33 @@ url(const char *domain, const char *path, const char *query) { bool upButton(void) { static bool state = LOW; - return buttonPressed(UP_BTN, &state); + static unsigned long lastEvent = 0; + return buttonPressed(UP_BTN, &state, &lastEvent); } // Return true if the DOWN button was pressed. bool downButton(void) { static bool state = LOW; - return buttonPressed(DOWN_BTN, &state); + static unsigned long lastEvent = 0; + return buttonPressed(DOWN_BTN, &state, &lastEvent); } // Return true if the button connected to the specified pin was pressed. bool -buttonPressed(int pin, bool *lastState) { +buttonPressed(int pin, bool *lastState, unsigned long *lastEvent) { + unsigned long now = millis(); if (digitalRead(pin)) { - if (*lastState == LOW) { // Rising event. + if (*lastState == LOW && now-*lastEvent > DEBOUNCE) { + // Rising event. *lastState = HIGH; + *lastEvent = now; return true; } - } else { // Falling event. + } else if (now-*lastEvent > DEBOUNCE) { + // Falling event. *lastState = LOW; + *lastEvent = now; } return false; } -- cgit v1.2.3