-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathButton.cpp
39 lines (31 loc) · 840 Bytes
/
Button.cpp
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
33
34
35
36
37
38
/**
* @file Button.cpp
* @author Paolo D'Apice <[email protected]>
*/
#include "Button.h"
Button::Button(byte p, long delay) :
pin(p), state(LOW), prevState(LOW),
debounceDelay(delay), lastDebounceTime(0L) {
pinMode(pin, INPUT);
}
boolean Button::isPressed() {
return digitalRead(pin);
}
boolean Button::isChanged() {
boolean ret = false;
boolean reading = digitalRead(pin);
if (reading != prevState) {
// switch changed
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
// reading holds for longer than debounce delay, so take it
if (reading != state) {
// button state changed
state = reading;
ret = (state == HIGH);
}
}
prevState = reading;
return ret;
}