-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathButton.cpp
More file actions
44 lines (41 loc) · 725 Bytes
/
Button.cpp
File metadata and controls
44 lines (41 loc) · 725 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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
//
//
//
#include "Button.h"
Button::Button(int pin)
: pin(pin), prevState(false), timePressed(0)
{
pinMode(pin, INPUT);
}
Button::buttonResult Button::input()
{
state = (digitalRead(pin) == HIGH);
if (state == prevState)
{
if (state) { return unchangedPressed; }
else { return unchangedNotPressed; }
}
else
{
prevState = state;
if (state) {
timePressed = millis();
return changedPressed;
}
else {
unsigned long timeReleased = millis();
if (timeReleased - timePressed > TIMELONGPRESS) //long press?
{
return longPressed;
}
else if (timeReleased - timePressed > TIMESHORTPRESS) //short press?
{
return shortPressed;
}
else
{
return debounce;
}
}
}
}