-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmButton.cpp
More file actions
77 lines (65 loc) · 1.54 KB
/
Copy pathmButton.cpp
File metadata and controls
77 lines (65 loc) · 1.54 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/*
Put this file in your libraries folder along with MomentaryButton.h
Written by Mark Fickett
Modified by Julian Loiacono December '14
*/
#include "mButton.h"
// Switch S2 behaviour
//#define __S2_To_HIGH__
//#define __S2_To_LOW__ // default to this, as original
//#define __MULTI_PUSH_S2__
MomentaryButton::MomentaryButton(int inputPin)
: wasClosed(false), pin(inputPin)
{}
void MomentaryButton::setThreshold(unsigned long newThreshold)
{
holdThreshold = newThreshold;
}
void MomentaryButton::setup()
{
pinMode(pin, INPUT);
#if defined (__S2_To_HIGH__)
digitalWrite(pin, LOW); // setting this to LOW, with pull down resistors, as the button S2 now pulls up.
#else
digitalWrite(pin, HIGH); // setting this to HIGH, with pull up resistors, as the button S2 now pulls down.
#endif
holdThreshold = 250;
bounceThreshold = 50;
}
void MomentaryButton::check()
{
unsigned long currentTimeMillis = millis();
#if defined (__S2_To_HIGH__)
boolean isClosed = (digitalRead(pin) == HIGH);
#else
boolean isClosed = (digitalRead(pin) == LOW);
#endif
if (isClosed & !wasClosed)
{
closeTimeMillis = currentTimeMillis;
}
else if (!isClosed & wasClosed)
{
held = (currentTimeMillis - closeTimeMillis) >= holdThreshold;
clicked = (!held && (currentTimeMillis - closeTimeMillis) >= bounceThreshold);
}
wasClosed = isClosed;
}
boolean MomentaryButton::wasClicked() {
if (clicked){
clicked = false;
return true;
}
else{
return false;
}
}
boolean MomentaryButton::wasHeld() {
if (held){
held = false;
return true;
}
else{
return false;
}
}