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
Copy file name to clipboardExpand all lines: README.md
+55-30Lines changed: 55 additions & 30 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,66 +1,95 @@
1
1
# Toggle
2
2
## About
3
3
4
-
Arduino switch and button library for components having 3 switched positions of control (SP3T or On-Off-On). Simple to use, automatically configures the pin mode and uses very little memory. Ten status functions available include one-shot transition status (depicting direction), present status and previous position status.
4
+
Arduino switch and button library for SPST, SPDT or SP3T contacts. Simple to use, provides debouncing, deglitching and uses very little memory. Status indicates one-shot transitions (depicting direction)and current position status.
5
5
6
-
#### General features
6
+
#### Features
7
7
8
-
- Performs both de-bouncing and de-glitching.
8
+
- Performs both debouncing and deglitching.
9
9
- External pull-up resistors not required.
10
10
- Very simple to use.
11
11
- Very low memory use.
12
12
13
13
## Using Toggle
14
14
15
-
Declare each switch or button, by indicating any 2 digital pins:
15
+
Declaring a switch or button using 1 digital pin for SPST or SPDT contacts:
16
16
17
17
```c++
18
-
Toggle sw1(7, 8); // GPIO 7 and 8
18
+
Toggle sw1(6); // GPIO 6
19
19
```
20
20
21
-
The library sets pin in input mode with the internal pull-up resistor enabled by default. All switches have to be repeatedly polled which is done in the `loop()` function.
21
+
Declaring a switch or button using 2 digital pins for SP3T contacts:
22
+
23
+
```c++
24
+
Toggle sw2(7, 8); // GPIO 7 and 8
25
+
```
26
+
27
+
The library sets the pin in input mode with pull-up resistor enabled. All switches have to be polled in the `loop()` function.
22
28
23
29
```c++
24
30
sw1.poll();
31
+
sw2.poll();
32
+
```
33
+
34
+
###### The switch status options when using 1 input pin:
35
+
36
+
```c++
37
+
sw.isON;
38
+
sw.isOFF;
39
+
sw.ONtoOFF;
40
+
sw.OFFtoON;
25
41
```
26
42
27
-
The switch status can be checked with the following 10 get functions:
43
+
The switch has 2 positions referred to as OFF (input is high) and ON (input is low). The first 2 status options will continuously return true if the switch is at that current position. The last 2 status options 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.
44
+
45
+
###### The switch status options when using 2 input pins:
28
46
29
47
```c++
30
-
boolisUp();
31
-
boolisMid();
32
-
boolisDn();
33
-
boolwasUp();
34
-
boolwasMid();
35
-
boolwasDn();
36
-
boolupToMid();
37
-
boolmidToDn();
38
-
booldnToMid();
39
-
boolmidToUp();
48
+
sw.isUP;
49
+
sw.isMID;
50
+
sw.isDN;
51
+
sw.UPtoMID;
52
+
sw.MIDtoDN;
53
+
sw.DNtoMID;
54
+
sw.MIDtoUP;
40
55
```
41
56
42
-
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 current position. The next 3 functions will also continuously return true if the switch was previously at that position. The last 4 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.
57
+
The switch has 3 positions referred to as UP, MID (center) and DN (down). The first 3 status options will continuously return true if the switch is at that position. The last 4 status options 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.
43
58
44
59
#### Example Sketch
45
60
46
-
This sketch prints every sw1 transition (fully debounced and deglitched):
61
+
This sketch checks the status of a SP3T and a basic SPST switch or button:
47
62
48
63
```c++
49
64
#include<Toggle.h>
50
65
51
-
Toggle sw1(7, 8);
66
+
Toggle sw1(6,7);
67
+
Toggle sw2(8);
52
68
53
69
void setup() {
70
+
while (!Serial) { }; // Leonardo
54
71
Serial.begin(115200);
55
72
}
56
73
57
74
void loop() {
58
75
sw1.poll();
76
+
sw2.poll();
77
+
78
+
if (sw1.OFFtoON) Serial.println(F("sw1: OFF⇒ON"));
79
+
if (sw1.ONtoOFF) Serial.println(F("sw1: ON⇒OFF"));
80
+
81
+
if (sw2.OFFtoON) Serial.println(F("sw2: OFF⇒ON"));
82
+
if (sw2.ONtoOFF) Serial.println(F("sw2: ON⇒OFF"));
59
83
60
-
if (sw1.upToMid()) Serial.println(F("Up⇒Mid"));
61
-
else if (sw1.dnToMid()) Serial.println(F("Dn⇒Mid"));
62
-
else if (sw1.midToUp()) Serial.println(F("Mid⇒Up"));
63
-
else if (sw1.midToDn()) Serial.println(F("Mid⇒Dn"));
84
+
if (sw1.UPtoMID) Serial.println(F("sw1: UP⇒MID"));
85
+
if (sw1.MIDtoDN) Serial.println(F("sw1: MID⇒DN"));
86
+
if (sw1.DNtoMID) Serial.println(F("sw1: DN⇒MID"));
87
+
if (sw1.MIDtoUP) Serial.println(F("sw1: MID⇒UP"));
88
+
89
+
if (sw2.UPtoMID) Serial.println(F("sw1: UP⇒MID"));
90
+
if (sw2.MIDtoDN) Serial.println(F("sw1: MID⇒DN"));
91
+
if (sw2.DNtoMID) Serial.println(F("sw1: DN⇒MID"));
92
+
if (sw2.MIDtoUP) Serial.println(F("sw1: MID⇒UP"));
64
93
}
65
94
```
66
95
@@ -75,7 +104,7 @@ A set of connections are shown where 0.1μF capacitors are added to provide the
75
104
- beneficial for interrupt applications
76
105
- improves noise immunity when using longer cables
77
106
78
-
Connections are shown both without and with capacitors installed.
107
+
Connections shown below are for 3-position switches, both without and with capacitors installed. Note: Connections for basic SPST or SPDT buttons or switches requiring only 1 input are similar and not shown.
79
108
80
109
81
110
@@ -103,10 +132,6 @@ Connections are shown both without and with capacitors installed.
103
132
104
133
105
134
106
-
### Software Signal Conditioning
107
-
108
-
When monitoring the status of a 3-position switch, 2 inputs are used. Both software debouncing and deglitching are used for all 10 switch status conditions.
109
-
110
135
#### Polling
111
136
112
137
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.
@@ -117,7 +142,7 @@ Using the input pullups provides a high 20K-50K impedance that makes the signals
117
142
118
143
#### Debouncing
119
144
120
-
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. For the 2 signals that are monitored, contact closure will be detected in after at least 10ms have elapsed (de-glitch period). Contact release is detected in at least 80ms. For the unconnected middle position of the switch, logic status is determined (not measured) in at least 80ms for both contact closure and release.
145
+
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.
Copy file name to clipboardExpand all lines: library.json
+2-2Lines changed: 2 additions & 2 deletions
Original file line number
Diff line number
Diff line change
@@ -1,7 +1,7 @@
1
1
{
2
2
"name": "Toggle",
3
-
"version": "1.0.0",
4
-
"description": "Arduino library for 3-position switches (SP3T, On-Off-On). Simple to use, automatic pin mode configuration, uses very little memory. Ten status functions available",
3
+
"version": "2.0.0",
4
+
"description": "Arduino switch and button library for SPST, SPDT or SP3T contacts. Simple to use, provides debouncing, deglitching and uses very little memory. Status indicates one-shot transitions (depicting direction) and current position status.",
sentence=Arduino library for 3-position switches (SP3T, On-Off-On). Simple to use, automatic pin mode configuration, uses very little memory.
6
-
paragraph=Ten status functions available.
5
+
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=Status indicates one-shot transitions (depicting direction) and current position status.
0 commit comments