You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Arduino switch and button library for SPST, SPDT or SP3T contacts. Simple to use, provides debouncing, deglitching and uses very little memory. Captures one-shot transitions (depicting direction) and current position status.
7
+
Arduino library for deglitching and debouncing signals, buttons and switches. One or two inputs can be monitored to decode transistions (depicting direction) for SPST, SPDT or SP3T contacts. Simple to use and requires very little memory. Captures one-shot transitions (depicting direction) and current status.
- Works with signals, stored data, buttons and switches.
12
15
- External pull-up resistors not required.
13
16
- Very simple to use.
14
-
- Ultra low memory use (14 bytes per button).
17
+
- Ultra low memory use.
15
18
16
19
## Using Toggle
17
20
@@ -36,7 +39,7 @@ sw1.poll();
36
39
sw2.poll();
37
40
```
38
41
39
-
######The switch functions when using 1 input pin:
42
+
#### The switch functions when using 1 input pin:
40
43
41
44
```c++
42
45
boolisOFF();
@@ -47,7 +50,7 @@ bool ONtoOFF();
47
50
48
51
The switch has 2 positions referred to as OFF (input is high) and ON (input is low). The first 2 functions will continuously return true if the switch is at that current position. The last 2 functions return true (once only) if the switch has just transitioned to the checked position. This is very handy to execute code based on direction of switched operation or for any one-shot processing of code.
49
52
50
-
######The switch functions when using 2 input pins:
53
+
#### The switch functions when using 2 input pins:
51
54
52
55
```c++
53
56
boolisUP();
@@ -61,7 +64,19 @@ bool MIDtoUP();
61
64
62
65
The switch has 3 positions referred to as UP, MID (center) and DN (down). The first 3 functions will continuously return true if the switch is at that position. The last 4 functions return true (once only) if the switch has just transitioned to that position. This is very handy to execute code based on direction of switched operation or for any one-shot processing of code.
63
66
64
-
#### Example Sketch
67
+
#### Other functions:
68
+
69
+
```c++
70
+
voidsetInputMode(inMode inputMode);
71
+
72
+
// options:
73
+
sw1.setInputMode(sw1.inMode::input_input); // high impedance input
sw1.setInputMode(sw1.inMode::input_pulldown); // not used
76
+
sw1.setInputMode(sw1.inMode::input_logic); // uses a byte variable for input data
77
+
```
78
+
79
+
#### Example Sketch:
65
80
66
81
This sketch checks the status of a SP3T and a basic SPST switch or button:
67
82
@@ -80,21 +95,13 @@ void loop() {
80
95
sw1.poll();
81
96
sw2.poll();
82
97
83
-
if (sw1.OFFtoON) Serial.println(F("sw1: OFF⇒ON"));
84
-
if (sw1.ONtoOFF) Serial.println(F("sw1: ON⇒OFF"));
85
-
86
-
if (sw2.OFFtoON) Serial.println(F("sw2: OFF⇒ON"));
87
-
if (sw2.ONtoOFF) Serial.println(F("sw2: ON⇒OFF"));
88
-
89
98
if (sw1.UPtoMID) Serial.println(F("sw1: UP⇒MID"));
90
99
if (sw1.MIDtoDN) Serial.println(F("sw1: MID⇒DN"));
91
100
if (sw1.DNtoMID) Serial.println(F("sw1: DN⇒MID"));
92
101
if (sw1.MIDtoUP) Serial.println(F("sw1: MID⇒UP"));
93
102
94
-
if (sw2.UPtoMID) Serial.println(F("sw1: UP⇒MID"));
95
-
if (sw2.MIDtoDN) Serial.println(F("sw1: MID⇒DN"));
96
-
if (sw2.DNtoMID) Serial.println(F("sw1: DN⇒MID"));
97
-
if (sw2.MIDtoUP) Serial.println(F("sw1: MID⇒UP"));
103
+
if (sw2.OFFtoON) Serial.println(F("sw2: OFF⇒ON"));
104
+
if (sw2.ONtoOFF) Serial.println(F("sw2: ON⇒OFF"));
98
105
}
99
106
```
100
107
@@ -147,23 +154,23 @@ A set of connections are shown where 0.1μF capacitors are optionally added to p
147
154
148
155
#### Polling
149
156
150
-
The switch inputs are polled in the main loop and the history of readings is stored in an 8-bit shift register (byte) where bit0 is the present reading and bit7 is the oldest reading. Data is shifted and updated every 10ms.
157
+
The switch inputs are polled in the main loop and the history of readings is stored in an 8-bit shift register (byte) where bit0 is the present reading and bit7 is the oldest reading. Data is shifted and updated every 5ms.
151
158
152
159
#### Deglitching
153
160
154
-
Using the input pullups provides a high 20K-50K impedance that makes the signals more susceptible to noise pickup, especially if longer cables are used in a noisy environment. To improve this possible situation, software deglitching is internally set to 1 read sample which represents a minimum period of 10ms that any reading change is ignored.
161
+
Using the input pullups provides a high 20K-50K impedance that makes the signals more susceptible to noise pickup, especially if longer cables are used in a noisy environment. To improve this possible situation, software deglitching is internally set to 1 read sample which represents a minimum period of 5ms that any reading change is ignored.
155
162
156
163
#### Debouncing
157
164
158
-
Debouncing requires the shift register to be completely filled with 1's or 0's to signify a stable state. This occurs 80ms after the last transition. Contact closure will be detected after at least 10ms have elapsed (de-glitch period). Contact release is detected in at least 80ms.
165
+
Debouncing requires the shift register to be completely filled with 1's or 0's to signify a stable state. This occurs 40ms after bouncing stops. Contact closure will be detected after 2 stable samples (readings) are made. This allows single sample anomalies to be ignored (deglitched). Contact release is detected when 8 stable samples (readings) have been made.
159
166
160
167
#### Memory Comparison on Leonardo with 2 buttons attached :
Copy file name to clipboardExpand all lines: library.json
+3-3Lines changed: 3 additions & 3 deletions
Original file line number
Diff line number
Diff line change
@@ -1,8 +1,8 @@
1
1
{
2
2
"name": "Toggle",
3
-
"version": "2.1.1",
4
-
"description": "Arduino switch and button library for SPST, SPDT or SP3T contacts. Simple to use, provides debouncing, deglitching and uses very little memory. Captures one-shot transitions (depicting direction) and current position status.",
5
-
"keywords": "toggle, switch",
3
+
"version": "2.2.0",
4
+
"description": "Arduino library for deglitching and debouncing signals, stored data, buttons and switches. Decodes transistions (depicting direction) for SPST, SPDT or SP3T contacts. Captures one-shot transitions and current status. Simple to use.",
sentence=Arduino switch and button library for SPST, SPDT or SP3T contacts. Simple to use, provides debouncing, deglitching and uses very little memory.
6
-
paragraph=Captures one-shot transitions (depicting direction) and current position status.
5
+
sentence=Arduino library for deglitching and debouncing signals, stored data, buttons and switches. Decodes transistions (depicting direction) for SPST, SPDT or SP3T contacts.
6
+
paragraph=Captures one-shot transitions and current status. Simple to use.
0 commit comments