Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
Refactored code, all input configurations use the same debounce algorithm, examples and readme updated.
  • Loading branch information
Dlloydev committed May 13, 2022
1 parent 92e4f42 commit 321ea6f
Show file tree
Hide file tree
Showing 11 changed files with 144 additions and 295 deletions.
43 changes: 21 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,11 @@ Arduino library for deglitching and debouncing signals, buttons and switches. O

![image](https://user-images.githubusercontent.com/63488701/167769114-92fcdfe5-1408-48d5-aeaa-efef8b3d4495.png)

Now you can easily debounce one or two 8-bit ports (8-16 signals) at a time in just one Toggle object. Useful for16-bit I/O expanders or other hardware, sensor data or stored data. The functions `debouncePortA()`and `debouncePortB()`are used to return the debounced data bytes. More below.
Now you can easily debounce an 8-bit ports (8 signals) at a time in just one Toggle object. Useful for I/O expanders or other hardware, sensor data or stored data. The functions `debouncePortA()`and `debouncePortB()`are used to return the debounced data bytes. More below.

### Debouncing Buttons, Switches and 1-bit Data:

![image](https://user-images.githubusercontent.com/63488701/167657951-a8ee4703-2336-4a1c-8230-8062402d8a6e.png)

#### Features
#### ![image](https://user-images.githubusercontent.com/63488701/168192044-6dffe0f5-da86-4546-8eca-711f89f1ca70.png)

- Performs both debouncing and deglitching.
- Works with signals, stored data, buttons and switches.
Expand Down Expand Up @@ -77,16 +75,17 @@ The switch has 3 positions referred to as UP, MID (center) and DN (down). The fi
This Port Debouncer uses a robust algorithm that removes spurious signal transitions. The algorithm adds only 2 sample periods of time lag to the output signal. A 3-sample stable period is required for an output bit to change. Therefore, to set an output bit, 3 consecutive 1's are required. When 3 consecutive 0's are detected, that bit value is cleared.

```c++
if (_inputMode == inMode::input_portA) {
for (int i = 0; i < 8; i++) {
if (*_inA & (1 << i) && regA & (1 << i) && lastRegA & (1 << i)) _pinA |= (1 << i);
if (!(*_inA & 1 << i) && !(regA & 1 << i) && !(lastRegA & 1 << i)) _pinA &= ~(1 << i);
}
lastRegA = regA;
regA = *_inA;
return _pinA;
uint8_t Toggle::debouncePort() {
pOut = out;
uint8_t bits = 2;
if (_inputMode == inMode::input_port) bits = 8;
for (int i = 0; i < bits; i++) {
if (dat & (1 << i) && pDat & (1 << i) && ppDat & (1 << i)) out |= (1 << i);
else if (!(dat & 1 << i) && !(pDat & 1 << i) && !(ppDat & 1 << i)) out &= ~(1 << i);
}
return 0;
ppDat = pDat;
pDat = dat;
return out;
}
```

Expand Down Expand Up @@ -225,15 +224,15 @@ Debouncing requires the shift register to be filled with 1's or 0's to signify a

#### Memory Comparison on Leonardo :

| Library | Version | Buttons | Bytes | Bytes Used |
| ------------ | --------- | ----------------------- | ------- | ---------- |
| Empty sketch | -- | 2 | 149 | -- |
| **Toggle.h** | **2.4.1** | **2** | **185** | **36** |
| JC_Button.h | 2.1.2 | 2 | 186 | 37 |
| Bounce2.h | 2.71.0 | 2 | 193 | 44 |
| AceButton.h | 1.9.2 | 2 | 205 | 56 |
| **Toggle.h** | **2.4.1** | **128** (16 ports x 8) | **321** | **172** |
| ezButton.h | 1.0.3 | 2 | 331 | 182 |
| Library | Version | Buttons | Bytes | Bytes Used |
| ------------ | --------- | -------------------------- | ------- | ---------- |
| Empty sketch | -- | 2 | 149 | -- |
| **Toggle.h** | **2.5.0** | **2** | **185** | **36** |
| JC_Button.h | 2.1.2 | 2 | 186 | 37 |
| Bounce2.h | 2.71.0 | 2 | 193 | 44 |
| AceButton.h | 1.9.2 | 2 | 205 | 56 |
| **Toggle.h** | **2.5.0** | **64** (8 ports x 8-bits) | **321** | **172** |
| ezButton.h | 1.0.3 | 2 | 331 | 182 |

### References

Expand Down
2 changes: 1 addition & 1 deletion examples/Input_Logic_Test/Input_Logic_Test.ino
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ void setup() {
while (!Serial) { }; // Leonardo
Serial.begin(115200);

sw1.setInputMode(sw1.inMode::input_logic);
sw1.setInputMode(sw1.inMode::input_port);
sw1.setSampleUs(100); // 100µs sample period

for (int i = 0; i < 47; i++) {
Expand Down
82 changes: 0 additions & 82 deletions examples/PortA_PortB_Debouncer_Test/PortA_PortB_Debouncer_Test.ino

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/****************************************************************************
PortA Debouncer Test Example:
=============================
Port Debouncer Test Example:
============================
This example demonstrates how you can easily debounce a complete 8-bit port
(8 signals) at a time in just one Toggle object.
Expand Down Expand Up @@ -48,24 +48,24 @@ const byte dat[15] = {
0b00000000
};

byte portA;
byte port;

Toggle sw1(&portA);
Toggle sw1(&port);

void setup() {
while (!Serial) { }; // Leonardo
Serial.begin(115200);

sw1.setInputMode(sw1.inMode::input_portA);
sw1.setInputMode(sw1.inMode::input_port);
sw1.setSampleUs(100); // 100µs sample period

for (int i = 0; i < 15; i++) {
portA = dat[i];
port = dat[i];
sw1.poll();
Serial.print(F("In: "));
Serial.print(dat[i], BIN);
Serial.print(F(" Out: "));
Serial.println(sw1.debouncePortA(), BIN);
Serial.println(sw1.debouncePort(), BIN);
}
}

Expand Down
1 change: 0 additions & 1 deletion examples/Three_Position_Switch/Three_Position_Switch.ino
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ void setup() {

void loop() {
sw1.poll();
digitalWrite(ledPin, sw1.getAllTransitions());
if (sw1.UPtoMID()) Serial.println(F("sw1: UP⇒MID"));
if (sw1.MIDtoDN()) Serial.println(F("sw1: MID⇒DN"));
if (sw1.DNtoMID()) Serial.println(F("sw1: DN⇒MID"));
Expand Down
2 changes: 1 addition & 1 deletion examples/Toggle_LED/Toggle_LED.ino
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ void setup() {
pinMode(ledPin, OUTPUT);
while (!Serial) { }; // Leonardo
Serial.begin(115200);
sw1.setInputMode(sw1.inMode::input);
sw1.setInputMode(sw1.inMode::input_pullup);
sw1.setInvertMode(true);
}

Expand Down
7 changes: 2 additions & 5 deletions keywords.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ setInputMode KEYWORD2
setInvertMode KEYWORD2
setSampleUs KEYWORD2
getAllTransitions KEYWORD2
debouncePortA KEYWORD2
debouncePortB KEYWORD2
debouncePort KEYWORD2

##########################################
# Constants (LITERAL1)
Expand All @@ -49,6 +48,4 @@ debouncePortB KEYWORD2
input LITERAL1
input_pullup LITERAL1
input_pulldown LITERAL1
input_logic LITERAL1
input_portA LITERAL1
input_portB LITERAL1
input_port LITERAL1
2 changes: 1 addition & 1 deletion library.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "Toggle",
"version": "2.4.1",
"version": "2.5.0",
"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.",
"keywords": "debounce, toggle, button, switch, data, deglitch",
"repository":
Expand Down
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=Toggle
version=2.4.1
version=2.5.0
author=David Lloyd
maintainer=David Lloyd <[email protected]>
sentence=Arduino library for deglitching and debouncing signals, stored data, buttons and switches. Decodes transistions (depicting direction) for SPST, SPDT or SP3T contacts.
Expand Down
Loading

0 comments on commit 321ea6f

Please sign in to comment.