Skip to content

Commit 233d2f6

Browse files
committed
Update
- New feature to debounce byte data as an input. - Updated readme with test results - New examples
1 parent f00b189 commit 233d2f6

File tree

8 files changed

+214
-109
lines changed

8 files changed

+214
-109
lines changed

README.md

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,17 @@
44

55
# Toggle [![arduino-library-badge](https://www.ardu-badge.com/badge/Toggle.svg?)](https://www.ardu-badge.com/Toggle) [![PlatformIO Registry](https://badges.registry.platformio.org/packages/dlloydev/library/Toggle.svg)](https://registry.platformio.org/libraries/dlloydev/Toggle)
66

7-
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.
8+
9+
![image](https://user-images.githubusercontent.com/63488701/167322856-91eba08a-687d-4b0b-813a-97d32b092327.png)
810

911
#### Features
1012

1113
- Performs both debouncing and deglitching.
14+
- Works with signals, stored data, buttons and switches.
1215
- External pull-up resistors not required.
1316
- Very simple to use.
14-
- Ultra low memory use (14 bytes per button).
17+
- Ultra low memory use.
1518

1619
## Using Toggle
1720

@@ -36,7 +39,7 @@ sw1.poll();
3639
sw2.poll();
3740
```
3841

39-
###### The switch functions when using 1 input pin:
42+
#### The switch functions when using 1 input pin:
4043

4144
```c++
4245
bool isOFF();
@@ -47,7 +50,7 @@ bool ONtoOFF();
4750

4851
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.
4952

50-
###### The switch functions when using 2 input pins:
53+
#### The switch functions when using 2 input pins:
5154

5255
```c++
5356
bool isUP();
@@ -61,7 +64,19 @@ bool MIDtoUP();
6164

6265
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.
6366

64-
#### Example Sketch
67+
#### Other functions:
68+
69+
```c++
70+
void setInputMode(inMode inputMode);
71+
72+
// options:
73+
sw1.setInputMode(sw1.inMode::input_input); // high impedance input
74+
sw1.setInputMode(sw1.inMode::input_pullup); // pullup resistor enabled (default)
75+
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:
6580
6681
This sketch checks the status of a SP3T and a basic SPST switch or button:
6782
@@ -80,21 +95,13 @@ void loop() {
8095
sw1.poll();
8196
sw2.poll();
8297
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-
8998
if (sw1.UPtoMID) Serial.println(F("sw1: UP⇒MID"));
9099
if (sw1.MIDtoDN) Serial.println(F("sw1: MID⇒DN"));
91100
if (sw1.DNtoMID) Serial.println(F("sw1: DN⇒MID"));
92101
if (sw1.MIDtoUP) Serial.println(F("sw1: MID⇒UP"));
93102
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"));
98105
}
99106
```
100107

@@ -147,23 +154,23 @@ A set of connections are shown where 0.1μF capacitors are optionally added to p
147154

148155
#### Polling
149156

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

152159
#### Deglitching
153160

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

156163
#### Debouncing
157164

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

160167
#### Memory Comparison on Leonardo with 2 buttons attached :
161168

162169
| Library | Version | Bytes | Bytes Used |
163170
| ------------ | --------- | ------- | ---------- |
164171
| Empty sketch | -- | 149 | -- |
165-
| **Toggle.h** | **2.1.1** | **177** | **28** |
166172
| JC_Button.h | 2.1.2 | 186 | 37 |
173+
| **Toggle.h** | **2.2.0** | **190** | **41** |
167174
| Bounce2.h | 2.71.0 | 193 | 44 |
168175
| AceButton.h | 1.9.2 | 205 | 56 |
169176
| ezButton.h | 1.0.3 | 331 | 182 |
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/************************************************************************
2+
Input logic Test Example:
3+
=========================
4+
A simple example that reads array bytes as the input signal.
5+
• open Serial Monitor to view/copy the results in csv format.
6+
• open Serial Plotter to view the noisy input and debounced outputs.
7+
• can be used to monitor, deglitch and debounce an input representing
8+
RPM pulses, flow meter pulses, noisy comparator output signal, etc.
9+
***********************************************************************/
10+
11+
#include <Toggle.h>
12+
13+
const byte ledPin = LED_BUILTIN;
14+
const byte dat[47] = {
15+
1, 0, 1, 1, 0, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1
16+
};
17+
18+
byte logic;
19+
20+
Toggle sw1(&logic);
21+
22+
void setup() {
23+
pinMode(ledPin, OUTPUT);
24+
while (!Serial) { }; // Leonardo
25+
Serial.begin(115200);
26+
27+
sw1.setInputMode(sw1.inMode::input_logic);
28+
29+
for (int i = 0; i < 47; i++) {
30+
logic = dat[i];
31+
sw1.poll();
32+
33+
bool off = sw1.isOFF();
34+
bool on = sw1.isON();
35+
digitalWrite(ledPin, on);
36+
37+
for (int i = 0; i < 10; i++) { // zoom horizontal
38+
plot("In", logic + 4, false);
39+
plot("isOFF", off + 2, false);
40+
plot("isON", on, true);
41+
}
42+
delay(50);
43+
}
44+
}
45+
46+
void loop() {
47+
}
48+
49+
void plot(String label, float value, bool last) {
50+
Serial.print(label); // can be empty
51+
if (label != "") Serial.print(":");
52+
Serial.print(value);
53+
if (last == false) Serial.print(", ");
54+
else Serial.println();
55+
}
Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,34 @@
1+
/************************************************************************
2+
Toggle Basic Example:
3+
=====================
4+
A simple example that toggles an LED each time a button is pressed
5+
or switch is closed. The input pin is pulled high and connected to GND.
6+
Therefore, when released (OFF),the signal is HIGH. When pressed (ON),
7+
the signal is LOW. The serial monitor indicates switch transitions.
8+
***********************************************************************/
9+
110
#include <Toggle.h>
211

3-
Toggle sw1(6,7);
4-
Toggle sw2(8);
12+
const byte buttonPin = 2;
13+
const byte ledPin = LED_BUILTIN;
14+
byte ledState = LOW;
15+
16+
Toggle sw1(buttonPin);
17+
Toggle sw2(buttonPin + 1);
518

619
void setup() {
20+
pinMode(ledPin, OUTPUT);
721
while (!Serial) { }; // Leonardo
822
Serial.begin(115200);
923
}
1024

1125
void loop() {
1226
sw1.poll();
1327
sw2.poll();
14-
15-
if (sw1.OFFtoON()) Serial.println(F("sw1: OFF⇒ON"));
1628
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"));
29+
if (sw1.OFFtoON()) {
30+
Serial.println(F("sw1: OFF⇒ON"));
31+
ledState = !ledState;
32+
digitalWrite(ledPin, ledState);
33+
}
3034
}

keywords.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,15 @@ isON KEYWORD2
3434
isOFF KEYWORD2
3535
ONtoOFF KEYWORD2
3636
OFFtoON KEYWORD2
37+
setInputMode KEYWORD2
38+
getInputMode KEYWORD2
3739

3840
##########################################
3941
# Constants (LITERAL1)
4042
##########################################
43+
44+
input LITERAL1
45+
input_pullup LITERAL1
46+
input_pulldown LITERAL1
47+
input_logic LITERAL1
48+
input_float LITERAL1

library.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
{
22
"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.",
5+
"keywords": "debounce, toggle, button, switch, data, deglitch",
66
"repository":
77
{
88
"type": "git",

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=2.1.1
2+
version=2.2.0
33
author=David Lloyd
44
maintainer=David Lloyd <[email protected]>
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=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.
77
category=Signal Input/Output
88
url=https://github.com/Dlloydev/Toggle
99
architectures=*

0 commit comments

Comments
 (0)