Skip to content

Commit ae43662

Browse files
Martin-LaclaustraMartin-Laclaustra
Martin-Laclaustra
authored and
Martin-Laclaustra
committed
Added singleClick and reorganized code
1 parent 3a24284 commit ae43662

File tree

8 files changed

+248
-139
lines changed

8 files changed

+248
-139
lines changed
Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
11
#include "Arduino.h"
22
#include "avdweb_Switch.h"
33

4-
const byte toggleSwitchpin = 13;
5-
const byte buttonGNDpin = 4;
6-
const byte ButtonVCCpin = 2;
7-
const byte Button10mspin = 12;
4+
const byte toggleSwitchpin = 4;
5+
const byte multiresponseButtonpin = 12;
86

9-
Switch buttonGND = Switch(buttonGNDpin); // button to GND, use internal 20K pullup resistor
107
Switch toggleSwitch = Switch(toggleSwitchpin);
11-
Switch buttonVCC = Switch(ButtonVCCpin, INPUT, HIGH); // button to VCC, 10k pull-down resistor, no internal pull-up resistor, HIGH polarity
12-
Switch button10ms = Switch(Button10mspin, INPUT_PULLUP, LOW, 1); // debounceTime 1ms
8+
Switch multiresponseButton = Switch(multiresponseButtonpin);
139

1410
void buttonCallbackFunction(void* s) {
1511
Serial.print("Button: ");
@@ -24,15 +20,16 @@ void toggleCallbackFunction(void* s) {
2420
void setup()
2521
{
2622
Serial.begin(9600);
27-
buttonGND.setPushedCallback(&buttonCallbackFunction, (void*)"pushed");
28-
buttonGND.setReleasedCallback(&buttonCallbackFunction, (void*)"released");
23+
toggleSwitch.setPushedCallback(&toggleCallbackFunction, (void*)"turned on");
24+
toggleSwitch.setReleasedCallback(&toggleCallbackFunction, (void*)"turned off");
2925

30-
toggleSwitch.setLongPressCallback(&toggleCallbackFunction, (void*)"long press");
31-
toggleSwitch.setDoubleClickCallback(&toggleCallbackFunction, (void*)"double click");
26+
multiresponseButton.setLongPressCallback(&buttonCallbackFunction, (void*)"long press");
27+
multiresponseButton.setDoubleClickCallback(&buttonCallbackFunction, (void*)"double click");
28+
multiresponseButton.setSingleClickCallback(&buttonCallbackFunction, (void*)"single click");
3229
}
3330

3431
void loop()
3532
{
36-
buttonGND.poll();
3733
toggleSwitch.poll();
34+
multiresponseButton.poll();
3835
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#include "Arduino.h"
2+
#include "avdweb_Switch.h"
3+
4+
const byte pushButtonpin = 2;
5+
const byte toggleSwitchpin = 4;
6+
const byte multiresponseButtonpin = 12;
7+
const byte alleventsButtonpin = 13;
8+
int i;
9+
10+
Switch pushButton = Switch(pushButtonpin); // button to GND, use internal 20K pullup resistor
11+
Switch toggleSwitch = Switch(toggleSwitchpin);
12+
Switch multiresponseButton = Switch(multiresponseButtonpin);
13+
Switch alleventsButton = Switch(alleventsButtonpin);
14+
// Other examples of constructors
15+
// Switch pushButtonVCC = Switch(pushButtonpin, INPUT, HIGH); // button to VCC, 10k pull-down resistor, no internal pull-up resistor, HIGH polarity
16+
// Switch pushButton1ms = Switch(pushButtonpin, INPUT_PULLUP, LOW, 1); // debounceTime 1ms
17+
18+
void setup()
19+
{ Serial.begin(9600);
20+
}
21+
22+
void loop()
23+
{ // pushButton simple events
24+
pushButton.poll();
25+
if(pushButton.switched()) Serial.print("pushButton switched ");
26+
if(pushButton.pushed()) {Serial.print("pushButton pushed "); Serial.print(++i); Serial.println(" times");}
27+
if(pushButton.released()) Serial.println("pushButton released");
28+
29+
// toggleSwitch report status only when changed
30+
if(toggleSwitch.poll()) {Serial.print("toggleSwitch status changed to "); Serial.println(toggleSwitch.on());}
31+
32+
// multiresponseButton complex events
33+
multiresponseButton.poll();
34+
if(multiresponseButton.longPress()) Serial.println("multiresponseButton longPress");
35+
if(multiresponseButton.doubleClick()) Serial.println("multiresponseButton doubleClick");
36+
if(multiresponseButton.singleClick()) Serial.println("multiresponseButton singleClick");
37+
38+
// alleventsButton complex events
39+
alleventsButton.poll();
40+
if(alleventsButton.switched()) {Serial.println("all_e_B switched."); Serial.print(" all_e_B status to "); Serial.print(alleventsButton.on()); Serial.println(".");}
41+
if(alleventsButton.pushed()) {Serial.println(" all_e_B pushed.");}
42+
if(alleventsButton.released()) Serial.println(" all_e_B released.");
43+
if(alleventsButton.longPress()) Serial.println(" ==> all_e_B longPress.");
44+
if(alleventsButton.doubleClick()) Serial.println(" ==> all_e_B doubleClick.");
45+
if(alleventsButton.singleClick()) Serial.println(" ==> all_e_B singleClick.");
46+
}

Examples/Test_switch/Test_switch.ino

Lines changed: 0 additions & 30 deletions
This file was deleted.

README.md

Lines changed: 67 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Available at: https://github.com/avandalen/avdweb_Switch
88
#### General features of the Switch library
99
- Performs not just de-bouncing, but also de-glitching against EMC pulses.
1010
- External pull-up resistors are not required.
11-
- Supports also long press and double click detection.
11+
- Supports also long press, double click, and single click detection.
1212
- Callback functions.
1313

1414
## Introduction
@@ -52,7 +52,7 @@ It returns "true" if the pin voltage agrees with the one defined by `polarity` (
5252

5353
#### pushed()
5454

55-
It returns "true" if a button was pushed (switched towards the on position). Use only for push buttons. Note that this is in fact "single-click".
55+
It returns "true" if a button was pushed (switched towards the on position). Use only for push buttons. Note that this provides instant response and can be used as "single-click" if "double-click" and "long-press" are not watched events. "Pushed" occurs simultaneously with "double-click" and "long-press".
5656

5757
#### released()
5858

@@ -68,6 +68,11 @@ It returns "true" if a push button is double clicked within 250ms (by default).
6868

6969
The anteceding pushed() event can't be avoided, because to restrict the pushed() function to single clicks it would have to wait for a possible second click, which would introduce an annoying delay. So, the action on doubleClick() has to undo the previous action on pushed().
7070

71+
#### singleClick()
72+
73+
It returns "true" if a push button is clicked once and the requirements for doubleClick and longPress are not met. The event thus occur several miliseconds after the actual push, because it depends on the other events not happening after the push. Use this if three different interactions are needed in combination with doubleClick and longPress. Note that a singleClick() always will be preceded by pushed(). Use pushed() instead if immediate response is required and there is no interest in monitoring doubleClick and longPress.
74+
75+
7176
#### Example
7277

7378
See the example in the library for a complete working program.
@@ -104,7 +109,7 @@ void setup() {
104109
}
105110
```
106111

107-
The available callback setting functions are `setPushedCallback()`, `setReleasedCallback()`, `setLongPressCallback()`, and `setDoubleClickCallback()` which allow defining the functions that will be called on such events. If using a toggle switch and not a push button, the "pushed" event will be of interest when the switch is turned on, and "released" when it is turned off.
112+
The available callback setting functions are `setPushedCallback()`, `setReleasedCallback()`, `setLongPressCallback()`, `setDoubleClickCallback()`, and `setSingleClickCallback()` which allow defining the functions that will be called on such events. If using a toggle switch and not a push button, the "pushed" event will be of interest when the switch is turned on, and "released" when it is turned off.
108113

109114
If the conditions for more than one event occur simultaneously and there are callback functions registered for them, they will be executed in the order of the functions above.
110115

@@ -169,6 +174,10 @@ A long-press generates first a pushed event and after 300ms (by default) the lon
169174
170175
The same happens with doubleClick, which also generates two pushed() events. When doubleClick is used, ignore the second pushed() result or don't call pushed(). When doubleClick is not needed, simply don't call doubleClick().
171176
177+
#### Combining singleClick, doubleClick, and longPress events
178+
179+
If these three events must be used toghether the best strategy is to stick to their get functions and avoid using pushed().
180+
172181
## Hardware considerations
173182
174183
#### Connecting switches to the microcontroler
@@ -222,68 +231,89 @@ Several discrete signals (updated at poll times) are created. The raw signal is
222231

223232
```
224233
..........................................DEGLITCHING..............................
225-
234+
226235
________________ _
227-
on | | | | _
228-
| | | | | |
229-
| |_| |___| |__
230-
analog off_____|_____________________________|____________________________
231-
236+
on | | | | _
237+
| | | | | |
238+
| |_| |___| |__
239+
analog off_____|_____________________________|____________________________
240+
232241
________________ _ _
233-
input _____| |_| |___| |_______________________________
234-
235-
poll ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^
242+
input _____| |_| |___| |_______________________________
243+
244+
poll ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^
236245
237246
equal 0 1 1 0 1 1 1 1 1 1 1 1 0 0 0 1 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
238247
239248
deglitchPeriod <--------><-- <-- <- <--------><--------><--------
240249
___________________________
241-
deglitched _________________| |__________________
250+
deglitched _________________| |__________________
242251
243252
deglitchTime ^ ^ ^ ^ ^ ^ ^
244253
245254
..........................................DEBOUNCING.............................
246255
247-
debouncePeriod <-------------------------------->
256+
debouncePeriod <-------------------------------->
248257
_________________________________
249-
debounced _________________| |____________
258+
debounced _________________| |____________
250259
_ _
251-
_switched _________________| |_______________________________| |__________
252-
253-
switchedTime ^ ^
254-
260+
_switched _________________| |_______________________________| |__________
261+
262+
switchedTime ^ ^
263+
255264
256265
**********************************************************************************
257266
........................................DOUBLE CLICK..............................
258-
259-
__________ ______
260-
debounced ________| |_______| |_____________________________
261267
262-
poll ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^
268+
__________ ______
269+
debounced ________| |_______| |_____________________________
270+
271+
poll ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^
263272
_ _
264-
pushed _________| |________________| |__________________________________
265-
266-
pushedTime ^ ^
273+
pushed _________| |________________| |__________________________________
267274
268-
doubleClickPeriod <------------------------------------->
275+
pushedTime ^ ^
276+
_ _
277+
released ____________________| |____________| |___________________________
278+
279+
releasedTime ^ ^
280+
281+
doubleClickPeriod <------------------------------------->
269282
_
270283
_doubleClick ___________________________| |__________________________________
271284
272-
285+
273286
........................................LONG PRESS................................
274-
275-
___________________________
276-
debounced ________| |___________________________
277287
278-
poll ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^
279-
280-
longPressPeriod <--------------->
288+
___________________________
289+
debounced ________| |___________________________
290+
291+
poll ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^
292+
293+
longPressPeriod <--------------->
281294
_ _
282-
_switched _________| |_________________________| |________________________
295+
_switched _________| |_________________________| |________________________
283296
__________
284-
longPressDisable ___________________________| |_________________________
297+
longPressDisable ___________________________| |_________________________
285298
_
286-
_longPress ___________________________| |__________________________________
299+
_longPress ___________________________| |__________________________________
300+
301+
302+
........................................SINGLE CLICK..............................
303+
304+
__________ ______
305+
debounced ________| |_______________________________| |_____
306+
307+
poll ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^
308+
309+
longPressPeriod <--------------->
310+
doubleClickPeriod <------------------------------------->
311+
_ _ _ _
312+
_switched _________| |_______| |_____________________________| |____| |___
313+
_____
314+
singleClickDisable______________________________________________| |__________
315+
_
316+
_singleClick _______________________________________________| |______________
287317
```
288318

289319
### Additional information

0 commit comments

Comments
 (0)