/
clickButtonDemo.cpp
68 lines (44 loc) · 1.44 KB
/
clickButtonDemo.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
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
/* ClickButton library demo
OUtput a message on Serial according to different clicks on one button.
Short clicks:
Single click -
Double click -
Triple click -
Long clicks (hold button for one second or longer on last click):
Single-click -
Double-click -
Triple-click -
2010, 2013 raron
GNU GPLv3 license
*/
#include "clickButton.h"
// the Button
const int buttonPin1 = 4;
ClickButton button1(buttonPin1, LOW, CLICKBTN_PULLUP);
// Button results
int function = 0;
void setup()
{
Serial.begin(9600);
pinMode(D4, INPUT_PULLUP);
// Setup button timers (all in milliseconds / ms)
// (These are default if not set, but changeable for convenience)
button1.debounceTime = 20; // Debounce timer in ms
button1.multiclickTime = 250; // Time limit for multi clicks
button1.longClickTime = 1000; // time until "held-down clicks" register
}
void loop()
{
// Update button state
button1.Update();
// Save click codes in LEDfunction, as click codes are reset at next Update()
if(button1.clicks != 0) function = button1.clicks;
if(function == 1) Serial.println("SINGLE click");
if(function == 2) Serial.println("DOUBLE click");
if(function == 3) Serial.println("TRIPLE click");
if(function == -1) Serial.println("SINGLE LONG click");
if(function == -2) Serial.println("DOUBLE LONG click");
if(function == -3) Serial.println("TRIPLE LONG click");
function = 0;
delay(5);
}