-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathHelloButton.ino
55 lines (44 loc) · 1.24 KB
/
HelloButton.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
/*
* A demo of a simplest AceButton that has a visible effect. One button is
* connected to the digital pin BUTTON_PIN. It uses the internal pull-up
* resistor (INPUT_PULLUP). Pressing the button turns on the built-in LED.
* Releasing the button turns off the LED.
*/
#include <AceButton.h>
using namespace ace_button;
// Some ESP32 boards have multiple builtin LEDs so don't define LED_BUILTIN.
#if defined(ESP32)
const int LED_PIN = 2;
#else
const int LED_PIN = LED_BUILTIN;
#endif
const int BUTTON_PIN = 2;
const int LED_ON = HIGH;
const int LED_OFF = LOW;
AceButton button(BUTTON_PIN);
// Forward reference to prevent Arduino compiler becoming confused.
void handleEvent(AceButton*, uint8_t, uint8_t);
void setup() {
delay(2000);
#if defined(ARDUINO_AVR_LEONARDO)
RXLED0; // LED off
TXLED0; // LED off
#endif
pinMode(LED_PIN, OUTPUT);
pinMode(BUTTON_PIN, INPUT_PULLUP);
button.setEventHandler(handleEvent);
}
void loop() {
button.check();
}
void handleEvent(AceButton* /* button */, uint8_t eventType,
uint8_t /* buttonState */) {
switch (eventType) {
case AceButton::kEventPressed:
digitalWrite(LED_PIN, LED_ON);
break;
case AceButton::kEventReleased:
digitalWrite(LED_PIN, LED_OFF);
break;
}
}