summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSam Anthony <sam@samanthony.xyz>2024-11-21 21:05:47 -0500
committerSam Anthony <sam@samanthony.xyz>2024-11-21 21:05:47 -0500
commit9922abe2f1b5223ba38240c648d2e3af4b4e2d9f (patch)
tree7266f6fecebe8fd163d30d838b974701fd3a64a9
parent4686c1b4f60b870796b87f8df1ebf337d9079a9a (diff)
downloadsoen422-9922abe2f1b5223ba38240c648d2e3af4b4e2d9f.zip
sensor station: button debounce
-rw-r--r--SensorStation/SensorStation.ino22
1 files 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;
}