-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathClickVersusDoubleClickUsingBoth.ino
73 lines (62 loc) · 2.26 KB
/
ClickVersusDoubleClickUsingBoth.ino
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
/*
* A demo that combines the techniques of ClickVersusDoubleClickUsingReleased
* and ClickVersusDoubleClickUsingSuppression by using both event types to
* trigger a "Clicked". I think this is the best of both worlds. If someone does
* a simple Press/Release, the Release gets triggered. If someone does a quick
* click, then a Click gets triggers (after a delay to wait for the potential
* DoubleClick).
*
* See Also:
* examples/ClickVersusDoubleClickUsingReleased/
* examples/ClickVersusDoubleClickUsingSuppression/
*/
#include <AceButton.h>
using namespace ace_button;
// The pin number attached to the button.
const int BUTTON_PIN = 2;
#ifdef ESP32
// Different ESP32 boards use different pins
const int LED_PIN = 2;
#else
const int LED_PIN = LED_BUILTIN;
#endif
// LED states. Some microcontrollers wire their built-in LED the reverse.
const int LED_ON = HIGH;
const int LED_OFF = LOW;
// One button wired to the pin at BUTTON_PIN. Automatically uses the default
// ButtonConfig. The alternative is to call the AceButton::init() method in
// setup() below.
AceButton button(BUTTON_PIN);
// Forward reference to prevent Arduino compiler becoming confused.
void handleEvent(AceButton*, uint8_t, uint8_t);
void setup() {
// initialize built-in LED as an output
pinMode(LED_PIN, OUTPUT);
// Button uses the built-in pull up register.
pinMode(BUTTON_PIN, INPUT_PULLUP);
ButtonConfig* buttonConfig = button.getButtonConfig();
buttonConfig->setEventHandler(handleEvent);
buttonConfig->setFeature(ButtonConfig::kFeatureDoubleClick);
buttonConfig->setFeature(
ButtonConfig::kFeatureSuppressClickBeforeDoubleClick);
buttonConfig->setFeature(ButtonConfig::kFeatureSuppressAfterClick);
buttonConfig->setFeature(ButtonConfig::kFeatureSuppressAfterDoubleClick);
}
void loop() {
// Should be called every 4-5ms or faster, for the default debouncing time
// of ~20ms.
button.check();
}
// The event handler for the button.
void handleEvent(AceButton* /* button */, uint8_t eventType,
uint8_t /* buttonState */) {
switch (eventType) {
case AceButton::kEventClicked:
case AceButton::kEventReleased:
digitalWrite(LED_PIN, LED_ON);
break;
case AceButton::kEventDoubleClicked:
digitalWrite(LED_PIN, LED_OFF);
break;
}
}