-
Notifications
You must be signed in to change notification settings - Fork 0
/
clickButton.h
78 lines (60 loc) · 3 KB
/
clickButton.h
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
78
/* ClickButton
A library that decodes multiple clicks on one button.
Also copes with long clicks and click-and-hold.
Usage: ClickButton buttonObject(pin [LOW/HIGH, [CLICKBTN_PULLUP]]);
where LOW/HIGH denotes active LOW or HIGH button (default is LOW)
CLICKBTN_PULLUP is only possible with active low buttons.
Returned click counts:
A positive number denotes the number of (short) clicks after a released button
A negative number denotes the number of "long" clicks
NOTE!
This is the OPPOSITE/negative of click codes from the last pre-2013 versions!
(this seemed more logical and simpler, so I finally changed it)
Based on the Debounce example at arduino playground site
Copyright (C) 2010,2012, 2013 raron
GNU GPLv3 license
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
Contact: raronzen@gmail.com
History:
2013.08.29 - Some small clean-up of code, more sensible variable names etc.
Added another example code for multiple buttons in an object array
2013.04.23 - A "minor" debugging: active-high buttons now work (wops)!
Thanks goes to John F. H. for pointing that out!
2013.02.17 - Some improvements, simplified click codes.
Added a LED fader example. Thanks to Tom K. for the idea.
2012.01.31 - Tiny update for Arduino 1.0
2010.06.15 - First version. Basically just a small OOP programming exercise.
*/
// -------- clickButton.h --------
#include "application.h"
#define CLICKBTN_PULLUP HIGH
class ClickButton
{
public:
ClickButton(uint8_t buttonPin);
ClickButton(uint8_t buttonPin, boolean active);
ClickButton(uint8_t buttonPin, boolean active, boolean internalPullup);
void Update();
int clicks; // button click counts to return
boolean depressed; // the currently debounced button (press) state (presumably it is not sad :)
long debounceTime;
long multiclickTime;
long longClickTime;
private:
uint8_t _pin; // Arduino pin connected to the button
boolean _activeHigh; // Type of button: Active-low = 0 or active-high = 1
boolean _btnState; // Current appearant button state
boolean _lastState; // previous button reading
int _clickCount; // Number of button clicks within multiclickTime milliseconds
long _lastBounceTime; // the last time the button input pin was toggled, due to noise or a press
};
// -------- end clickButton.h --------