10
10
... and the processor datasheet.
11
11
12
12
Setup a test circuit:
13
- * Connect a pushbutton to pin A1 (ButtonPin ) and ground.
14
- * The pin 13 (StatusPin ) is used for output attach a led and resistor to ground
15
- or see the built-in led on the standard arduino board.
13
+ * Connect a pushbutton to the PIN_INPUT (see defines for processor specific examples ) and ground.
14
+ * The PIN_LED (see defines for processor specific examples ) is used for output attach a led and resistor to VCC.
15
+ or maybe use a built-in led on the standard arduino board.
16
16
17
17
The sketch shows how to setup the library and bind the functions (singleClick, doubleClick) to the events.
18
18
In the loop function the button.tick function must be called as often as you like.
39
39
#define PIN_INPUT D3
40
40
#define PIN_LED D4
41
41
42
+ #elif defined(ESP32)
43
+ // Example pin assignments for a ESP32 board
44
+ // Some boards have a BOOT switch using GPIO 0.
45
+ #define PIN_INPUT 0
46
+ // Attach a LED using GPIO 25 and VCC. The LED is on when output level is LOW.
47
+ #define PIN_LED 25
48
+
42
49
#endif
43
50
44
51
// Setup a new OneButton on pin PIN_INPUT
@@ -59,16 +66,20 @@ unsigned long pressStartTime;
59
66
60
67
// This function is called from the interrupt when the signal on the PIN_INPUT has changed.
61
68
// do not use Serial in here.
62
- #if defined(ARDUINO_AVR_UNO) || defined (ARDUINO_AVR_NANO_EVERY)
63
- void checkTicks ()
64
- {
69
+ #if defined(ARDUINO_AVR_UNO) || defined(ARDUINO_AVR_NANO_EVERY)
70
+ void checkTicks () {
65
71
// include all buttons here to be checked
66
72
button.tick (); // just call tick() to check the state.
67
73
}
68
74
69
75
#elif defined(ESP8266)
70
- ICACHE_RAM_ATTR void checkTicks ()
71
- {
76
+ ICACHE_RAM_ATTR void checkTicks () {
77
+ // include all buttons here to be checked
78
+ button.tick (); // just call tick() to check the state.
79
+ }
80
+
81
+ #elif defined(ESP32)
82
+ void IRAM_ATTR checkTicks () {
72
83
// include all buttons here to be checked
73
84
button.tick (); // just call tick() to check the state.
74
85
}
@@ -77,15 +88,13 @@ ICACHE_RAM_ATTR void checkTicks()
77
88
78
89
79
90
// this function will be called when the button was pressed 1 time only.
80
- void singleClick ()
81
- {
91
+ void singleClick () {
82
92
Serial.println (" singleClick() detected." );
83
93
} // singleClick
84
94
85
95
86
96
// this function will be called when the button was pressed 2 times in a short timeframe.
87
- void doubleClick ()
88
- {
97
+ void doubleClick () {
89
98
Serial.println (" doubleClick() detected." );
90
99
91
100
ledState = !ledState; // reverse the LED
@@ -94,37 +103,40 @@ void doubleClick()
94
103
95
104
96
105
// this function will be called when the button was pressed multiple times in a short timeframe.
97
- void multiClick ()
98
- {
99
- Serial.print (" multiClick(" );
100
- Serial.print (button.getNumberClicks ());
101
- Serial.println (" ) detected." );
106
+ void multiClick () {
107
+ int n = button.getNumberClicks ();
108
+ if (n == 3 ) {
109
+ Serial.println (" tripleClick detected." );
110
+ } else if (n == 4 ) {
111
+ Serial.println (" quadrupleClick detected." );
112
+ } else {
113
+ Serial.print (" multiClick(" );
114
+ Serial.print (n);
115
+ Serial.println (" ) detected." );
116
+ }
102
117
103
118
ledState = !ledState; // reverse the LED
104
119
digitalWrite (PIN_LED, ledState);
105
120
} // multiClick
106
121
107
122
108
123
// this function will be called when the button was held down for 1 second or more.
109
- void pressStart ()
110
- {
124
+ void pressStart () {
111
125
Serial.println (" pressStart()" );
112
126
pressStartTime = millis () - 1000 ; // as set in setPressTicks()
113
127
} // pressStart()
114
128
115
129
116
130
// this function will be called when the button was released after a long hold.
117
- void pressStop ()
118
- {
131
+ void pressStop () {
119
132
Serial.print (" pressStop(" );
120
133
Serial.print (millis () - pressStartTime);
121
134
Serial.println (" ) detected." );
122
135
} // pressStop()
123
136
124
137
125
138
// setup code here, to run once:
126
- void setup ()
127
- {
139
+ void setup () {
128
140
Serial.begin (115200 );
129
141
Serial.println (" One Button Example with interrupts." );
130
142
@@ -164,8 +176,7 @@ void setup()
164
176
165
177
166
178
// main code here, to run repeatedly:
167
- void loop ()
168
- {
179
+ void loop () {
169
180
// keep watching the push button, even when no interrupt happens:
170
181
button.tick ();
171
182
0 commit comments