|
8 | 8 | #include <Arduino.h> |
9 | 9 | #include <EasyButton.h> |
10 | 10 |
|
11 | | -#define BUTTON_PIN 2 //Should be a pin that supports external interrupts |
12 | | -#define BAUDRATE 9600 |
| 11 | +/* |
| 12 | + Arduino pin where the buttons are connected to. |
| 13 | + Should be a pin that supports external interrupts. |
| 14 | + */ |
| 15 | +#define BUTTON_PIN 2 |
13 | 16 |
|
| 17 | +#define BAUDRATE 115200 |
| 18 | + |
| 19 | +// Instance of the button. |
14 | 20 | EasyButton button(BUTTON_PIN); |
15 | 21 |
|
16 | 22 | void buttonPressedTwoSeconds() |
17 | 23 | { |
18 | | - Serial.println("Button Pressed for two seconds"); |
| 24 | + Serial.println("Button pressed for two seconds"); |
19 | 25 | } |
20 | 26 |
|
21 | 27 | void buttonISR() |
22 | 28 | { |
23 | | - //When button is being used through external interrupts, parameter INTERRUPT must be passed to read() function |
24 | | - button.read(EASYBUTTON_READ_TYPE_INTERRUPT); |
| 29 | + // When button is being used through external interrupts, parameter INTERRUPT must be passed to read() function. |
| 30 | + button.read(); |
25 | 31 | } |
26 | 32 |
|
27 | | -void setup() { |
28 | | - // put your setup code here, to run once: |
| 33 | +void setup() |
| 34 | +{ |
| 35 | + // Initialize Serial for debuging purposes. |
29 | 36 | Serial.begin(BAUDRATE); |
| 37 | + |
| 38 | + Serial.println(); |
| 39 | + Serial.println(">>> EasyButton onPressedFor interrupt example <<<"); |
| 40 | + |
| 41 | + // Initialize the button. |
30 | 42 | button.begin(); |
| 43 | + |
31 | 44 | button.onPressedFor(2000, buttonPressedTwoSeconds); |
| 45 | + |
32 | 46 | if (button.supportsInterrupt()) |
33 | 47 | { |
34 | 48 | button.enableInterrupt(buttonISR); |
35 | | - Serial.println("EasyButton onPressedFor Interrupt example"); |
| 49 | + Serial.println("Button will be used through interrupts"); |
36 | 50 | } |
37 | 51 | } |
38 | 52 |
|
39 | | -void loop() { |
40 | | - // put your main code here, to run repeatedly: |
41 | | - // update() function must be called repeatedly only if onPressedFor functionality is being used and interrupt is enabled |
| 53 | +void loop() |
| 54 | +{ |
| 55 | + /* |
| 56 | + update() function must be called repeatedly only if onPressedFor |
| 57 | + functionality is being used and interrupt is enabled. |
| 58 | + */ |
42 | 59 | button.update(); |
43 | 60 | } |
0 commit comments