-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreadInput.ino
32 lines (26 loc) · 938 Bytes
/
readInput.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include "config.h"
/**
This function reads the input button and debounces the reading.
It fires the handler function only once, when the button is pressed or released.
*/
void TheButton::readInput() {
// Read the button
boolean currentButtonState = readButtonState();
// If the state has changed, then reset the debounce time
if (currentButtonState != lastButtonState) {
debounceTime = millis();
}
// If input is stable, debounced and the state has truly changed, then ...
if (((millis() - debounceTime) > BUTTON_DEBOUNCE_MS) && (currentButtonState != debouncedButtonState)) {
// Set the current state as the debounded state
debouncedButtonState = currentButtonState;
// Fire the correct handler
if (debouncedButtonState == true) {
onPressed();
} else {
onReleased();
}
}
// The currentButtonState is the lastButtonState now
lastButtonState = currentButtonState;
}