-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDebounceSwitch.cpp
More file actions
29 lines (24 loc) · 845 Bytes
/
Copy pathDebounceSwitch.cpp
File metadata and controls
29 lines (24 loc) · 845 Bytes
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
#include "DebounceSwitch.h"
bool DebounceSwitch::initPin() {
reading = digitalRead(pin);
prevState = reading;
currState = reading;
timePrev = millis();
return reading; // return the current state
}
bool DebounceSwitch::readPin(int debounceDelay) {
reading = digitalRead(pin);
if ( reading != prevState )
timePrev = millis(); // start the timer
if ( labs(millis() - timePrev) > long(debounceDelay) ) { // allow to settle
if (reading != currState) {
currState = reading; // update the current state
if (currState)
hiCallback();
else
loCallback();
}
}
prevState = reading; // update the previous state
return currState; // return the current state (de-bounced)
}