Skip to content

Commit 33fc9e9

Browse files
committed
Update
New Version now works with more button or switch types: SPST, SPDT or SP3T types.
1 parent 5852adb commit 33fc9e9

File tree

7 files changed

+158
-165
lines changed

7 files changed

+158
-165
lines changed

README.md

Lines changed: 55 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,95 @@
11
# Toggle
22
## About
33

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.
55

6-
#### General features
6+
#### Features
77

8-
- Performs both de-bouncing and de-glitching.
8+
- Performs both debouncing and deglitching.
99
- External pull-up resistors not required.
1010
- Very simple to use.
1111
- Very low memory use.
1212

1313
## Using Toggle
1414

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:
1616

1717
```c++
18-
Toggle sw1(7, 8); // GPIO 7 and 8
18+
Toggle sw1(6); // GPIO 6
1919
```
2020
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.
2228

2329
```c++
2430
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;
2541
```
2642

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:
2846

2947
```c++
30-
bool isUp();
31-
bool isMid();
32-
bool isDn();
33-
bool wasUp();
34-
bool wasMid();
35-
bool wasDn();
36-
bool upToMid();
37-
bool midToDn();
38-
bool dnToMid();
39-
bool midToUp();
48+
sw.isUP;
49+
sw.isMID;
50+
sw.isDN;
51+
sw.UPtoMID;
52+
sw.MIDtoDN;
53+
sw.DNtoMID;
54+
sw.MIDtoUP;
4055
```
4156

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.
4358

4459
#### Example Sketch
4560

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:
4762

4863
```c++
4964
#include <Toggle.h>
5065

51-
Toggle sw1(7, 8);
66+
Toggle sw1(6,7);
67+
Toggle sw2(8);
5268

5369
void setup() {
70+
while (!Serial) { }; // Leonardo
5471
Serial.begin(115200);
5572
}
5673

5774
void loop() {
5875
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"));
5983

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"));
6493
}
6594
```
6695
@@ -75,7 +104,7 @@ A set of connections are shown where 0.1μF capacitors are added to provide the
75104
- beneficial for interrupt applications
76105
- improves noise immunity when using longer cables
77106
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.
79108
80109
81110
@@ -103,10 +132,6 @@ Connections are shown both without and with capacitors installed.
103132
104133
105134
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-
110135
#### Polling
111136
112137
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
117142
118143
#### Debouncing
119144
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.
121146
122147
### References
123148
Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,30 @@
11
#include <Toggle.h>
22

3-
Toggle sw1(7, 8);
3+
Toggle sw1(6,7); //SP3T
4+
Toggle sw2(8); //SPDT
45

56
void setup() {
7+
while (!Serial) { }; // Leonardo
68
Serial.begin(115200);
79
}
810

911
void loop() {
1012
sw1.poll();
13+
sw2.poll();
1114

12-
if (sw1.upToMid()) Serial.println(F("Up⇒Mid"));
13-
else if (sw1.dnToMid()) Serial.println(F("Dn⇒Mid"));
14-
else if (sw1.midToUp()) Serial.println(F("Mid⇒Up"));
15-
else if (sw1.midToDn()) Serial.println(F("Mid⇒Dn"));
15+
if (sw1.OFFtoON) Serial.println(F("sw1: OFF⇒ON"));
16+
if (sw1.ONtoOFF) Serial.println(F("sw1: ON⇒OFF"));
17+
18+
if (sw2.OFFtoON) Serial.println(F("sw2: OFF⇒ON"));
19+
if (sw2.ONtoOFF) Serial.println(F("sw2: ON⇒OFF"));
20+
21+
if (sw1.UPtoMID) Serial.println(F("sw1: UP⇒MID"));
22+
if (sw1.MIDtoDN) Serial.println(F("sw1: MID⇒DN"));
23+
if (sw1.DNtoMID) Serial.println(F("sw1: DN⇒MID"));
24+
if (sw1.MIDtoUP) Serial.println(F("sw1: MID⇒UP"));
25+
26+
if (sw2.UPtoMID) Serial.println(F("sw1: UP⇒MID"));
27+
if (sw2.MIDtoDN) Serial.println(F("sw1: MID⇒DN"));
28+
if (sw2.DNtoMID) Serial.println(F("sw1: DN⇒MID"));
29+
if (sw2.MIDtoUP) Serial.println(F("sw1: MID⇒UP"));
1630
}

keywords.txt

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,35 +16,26 @@ sw5 KEYWORD1
1616
sw6 KEYWORD1
1717
sw7 KEYWORD1
1818
sw8 KEYWORD1
19+
sw9 KEYWORD1
1920

2021
##########################################
2122
# Methods and Functions (KEYWORD2)
2223
##########################################
2324

2425
poll KEYWORD2
25-
isUp KEYWORD2
26-
isMid KEYWORD2
27-
isDn KEYWORD2
28-
wasUp KEYWORD2
29-
wasMid KEYWORD2
30-
wasDn KEYWORD2
31-
upToMid KEYWORD2
32-
midToDn KEYWORD2
33-
dnToMid KEYWORD2
34-
midToUp KEYWORD2
3526

3627
##########################################
3728
# Constants (LITERAL1)
3829
##########################################
3930

40-
x LITERAL1
41-
isUp LITERAL1
42-
isMid LITERAL1
43-
isDn LITERAL1
44-
wasUp LITERAL1
45-
wasMid LITERAL1
46-
wasDn LITERAL1
47-
upToMid LITERAL1
48-
midToDn LITERAL1
49-
dnToMid LITERAL1
50-
midToUp LITERAL1
31+
isUP LITERAL1
32+
isMID LITERAL1
33+
isDN LITERAL1
34+
UPtoMID LITERAL1
35+
MIDtoDN LITERAL1
36+
DNtoMID LITERAL1
37+
MIDtoUP LITERAL1
38+
isON LITERAL1
39+
isOFF LITERAL1
40+
ONtoOFF LITERAL1
41+
OFFtoON LITERAL1

library.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"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.",
55
"keywords": "toggle, switch",
66
"repository":
77
{

library.properties

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
name=Toggle
2-
version=1.0.0
2+
version=2.0.0
33
author=David Lloyd
44
maintainer=David Lloyd <[email protected]>
5-
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.
77
category=Signal Input/Output
88
url=https://github.com/Dlloydev/Toggle
99
architectures=*

0 commit comments

Comments
 (0)