Skip to content

Commit 149e64c

Browse files
committed
Update
Refactored code, updated examples and readme.
1 parent ab099be commit 149e64c

File tree

12 files changed

+153
-152
lines changed

12 files changed

+153
-152
lines changed

README.md

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
# 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)
22

3-
Arduino button library for debouncing switch contacts and logic data. New pressCode() function detects fast, short and long button presses for 225 combinations. Works with all switch types, port expanders and other 8-bit data sources. Debounce algorithm ignores several consecutive spurious transitions.
3+
Arduino button library for debouncing switch contacts and input data. New pressCode() function detects fast, short and long button presses for 225 combinations. Works with all switch types, port expanders and other 8-bit data sources. Debounce algorithm ignores several consecutive spurious transitions.
44

55
## Features
66

77
### Flexible Inputs
88

9-
The inputs can be from a single pin or several pins allowing the use of 2 or 3-position switches and up to seven debounced states. When linking to a data (byte) input, the debouncer can work with any selected bit or it can debounce all 8-bits in one Toggle instance. Examples: [`Input_Bit_Test.ino`](https://github.com/Dlloydev/Toggle/blob/main/examples/Input_Bit_Test/Input_Bit_Test.ino) , [`Input_Bit.ino`](https://github.com/Dlloydev/Toggle/blob/main/examples/Input_Bit/Input_Bit.ino), [`Input_Port_Test.ino`](https://github.com/Dlloydev/Toggle/blob/main/examples/Input_Port_Test/Input_Port_Test.ino) and [`Input_Port.ino`](https://github.com/Dlloydev/Toggle/blob/main/examples/Input_Port/Input_Port.ino).
9+
The inputs can be from a single pin or several pins allowing the use of 2 or 3-position switches and up to seven debounced states. When linking to a data (byte) input, the debouncer can work with any selected bit or it can debounce all 8-bits in one Toggle instance. See [examples](https://github.com/Dlloydev/Toggle/tree/main/examples).
1010

1111
### Debounce Algorithm
1212

@@ -122,15 +122,9 @@ None.
122122
##### Example
123123
124124
```c++
125-
// a button or switch is connected from pin 2 to ground, 5000 μs sample interval (default),
126-
// pullup enabled (default), inverted = false (default)
127-
Toggle myInput(2);
128-
129-
// same as above, but a 3 position switch is connected to pins 2 and 3
130-
Toggle myInput(2, 3);
131-
132-
// a byte variable is linked to the Toggle object, defaults are same as above
133-
Toggle myInput(*Input);
125+
Toggle myInput(2); // Button connected pin 2 to GND, INPUT_PULLUP, debounced
126+
Toggle myInput(2, 3); // SPDT switch connected pins 2 and 3 to GND, INPUT_PULLUP, debounced
127+
Toggle myInput(*Input); // Byte variable as input, debounced
134128
```
135129

136130

@@ -267,19 +261,18 @@ else {
267261

268262
##### Description
269263

270-
This function will toggle the return value after each `onPress` state change. Useful to easily toggle an LED.
264+
This function can be used to convert a momentary push button to a toggle switch. By default, the retuen value will toggle `true-false` then `false-true` for each `onPress` action of the button.
271265

272-
##### Syntax
273-
274-
`myInput.toggle(invert);`
266+
- The `setToggleState()` function sets the initial state (*true* or *false*)
267+
- The `setToggleTrigger()` function sets the trigger mode: *false*: `onPress`, *true*: `onRelease`
275268

276269
##### Parameters
277270

278-
**invert:** can be used to invert the initial toggle state *(bool)*
271+
None.
279272

280273
##### Returns
281274

282-
*true* or *false*, toggles after each `onPress` state change. *(bool)
275+
*true* or *false*, (*bool*). Toggles as configured by `setToggleTrigger()`. Default is trigger `onPress`
283276

284277

285278

@@ -420,21 +413,20 @@ None.
420413
myInput.setInputMode(sw1.inMode::input_input); // high impedance input
421414
myInput.setInputMode(sw1.inMode::input_pullup); // pullup resistor enabled (default)
422415
myInput.setInputMode(sw1.inMode::input_pulldown); // pulldown resistor enabled (ESP32)
423-
myInput.setInputMode(sw1.inMode::input_bit); // input byte (bit0)
424-
myInput.setInputMode(sw1.inMode::input_port); // input byte (8-bit)
416+
myInput.setInputMode(sw1.inMode::input_byte); // input byte (8-bit)
425417
```
426418

427419

428420

429-
## setInvertMode()
421+
## setInputInvert()
430422

431423
##### Description
432424

433425
Set true if the button or switch pulls the signal high when pressed. Default is false (button or switch pulls the signal low when pressed).
434426

435427
##### Syntax
436428

437-
`myInput.setInvertMode(true);`
429+
`myInput.setInputInvert(true);`
438430

439431
##### Returns
440432

examples/Input_Bit/Input_Bit.ino

Lines changed: 0 additions & 36 deletions
This file was deleted.

examples/Input_Bit_Test/Input_Bit_Test.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ void setup() {
2424
while (!Serial) { }; // Leonardo
2525
Serial.begin(115200);
2626
myInput.begin(Input);
27-
myInput.setInputMode(myInput.inMode::input_port);
27+
myInput.setInputMode(myInput.inputMode::input_byte);
2828
myInput.setSamplePeriodUs(20); // 0-65535μs
2929

3030
for (int i = 0; i < 47; i++) {

examples/Input_Port/Input_Port.ino renamed to examples/Input_Byte/Input_Byte.ino

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/******************************************************************
2-
Input Port Example:
2+
Input Byte Example:
33
===================
44
This example demonstrates how you can debounce any byte variable
55
in the sketch. The Arduino pin functions are not used.
@@ -20,7 +20,7 @@ void setup() {
2020
while (!Serial) { }; // Leonardo
2121
Serial.begin(115200);
2222
myInput.begin(Input);
23-
myInput.setInputMode(myInput.inMode::input_port); // debounce all bits
23+
myInput.setInputMode(myInput.inputMode::input_byte); // debounce all bits
2424
myInput.setSamplePeriodUs(20); // 0-65535μs
2525
}
2626

@@ -32,5 +32,5 @@ void loop() {
3232
if (myInput.onRelease()) {
3333
Serial.print(F("b")); Serial.print(bit); Serial.println(F(": ON⇒OFF"));
3434
}
35-
digitalWrite(ledPin, myInput.toggle(bit));
35+
digitalWrite(ledPin, myInput.toggle());
3636
}

examples/Input_Port_Test/Input_Port_Test.ino renamed to examples/Input_Byte_Test/Input_Byte_Test.ino

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*************************************************************************
2-
Input Port Debouncer Test Example:
2+
Input Byte Debouncer Test Example:
33
==================================
44
This example demonstrates how you can debounce an 8-bit byte (8 signals)
55
in just one Toggle object. The Arduino pin functions are not used.
@@ -57,7 +57,7 @@ void setup() {
5757
while (!Serial) { }; // Leonardo
5858
Serial.begin(115200);
5959
myInput.begin(Input);
60-
myInput.setInputMode(myInput.inMode::input_port);
60+
myInput.setInputMode(myInput.inputMode::input_byte);
6161
myInput.setSamplePeriodUs(20); // 0-65535μs
6262

6363
for (int i = 0; i < 15; i++) {
@@ -66,7 +66,7 @@ void setup() {
6666
Serial.print(F("In: "));
6767
Serial.print(dat[i], BIN);
6868
Serial.print(F(" Out: "));
69-
Serial.println(myInput.debounceInput(), BIN);
69+
Serial.println(myInput.debouncer(), BIN);
7070
}
7171
}
7272

examples/Input_Pulldown/Input_Pulldown.ino

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,13 @@ Toggle sw1(buttonPin);
1515

1616
void setup() {
1717
pinMode(ledPin, OUTPUT);
18-
while (!Serial) { }; // Leonardo
19-
Serial.begin(115200);
2018
sw1.begin(buttonPin);
21-
sw1.setInputMode(sw1.inMode::input_pulldown);
22-
sw1.setInvertMode(true);
19+
sw1.setInputMode(sw1.inputMode::input_pulldown);
20+
sw1.setToggleState(0); // set initial state 0 or 1
21+
sw1.setToggleTrigger(0); // set trigger onPress: 0, or onRelease: 1
2322
}
2423

2524
void loop() {
2625
sw1.poll();
27-
if (sw1.onPress()) Serial.println(F("sw1: OFF⇒ON"));
28-
if (sw1.onRelease()) Serial.println(F("sw1: ON⇒OFF"));
2926
digitalWrite(ledPin, sw1.toggle());
3027
}

examples/Use_Button_As_Toggle/Use_Button_As_Toggle.ino

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1-
/*******************************************************************************
2-
TURN A MOMENTARY PUSHBUTTON INTO A TOGGLE SWITCH:
1+
/***********************************************
2+
USE A MOMENTARY PUSHBUTTON AS A TOGGLE SWITCH:
33
The button is connected from input pin to GND.
44
INPUT_PULLUP and debouncing are pre-configured.
5-
The built in LED changes state each time the button is pressed.
6-
For the LED to change state on release, un-comment button.setInvertMode(1);
7-
For LED startup in opposite state, use digitalWrite(ledPin, button.toggle(1));
8-
******************************************************************************/
5+
The built in LED changes state as configured.
6+
**********************************************/
97

108
#include <Toggle.h>
119

@@ -16,7 +14,9 @@ Toggle button(buttonPin);
1614

1715
void setup() {
1816
button.begin(buttonPin);
19-
//button.setInvertMode(1);
17+
button.setToggleState(0); // set initial state 0 or 1
18+
button.setToggleTrigger(0); // set trigger onPress: 0, or onRelease: 1
19+
2020
pinMode(ledPin, OUTPUT);
2121
}
2222

keywords.txt

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
Toggle KEYWORD1
1010
myInput KEYWORD1
11+
myButton KEYWORD1
1112
sw KEYWORD1
1213
sw0 KEYWORD1
1314
sw1 KEYWORD1
@@ -26,21 +27,23 @@ sw9 KEYWORD1
2627

2728
poll KEYWORD2
2829
setInputMode KEYWORD2
29-
setInvertMode KEYWORD2
30+
setInputInvert KEYWORD2
31+
toggle KEYWORD2
32+
setToggleState KEYWORD2
33+
setToggleTrigger KEYWORD2
3034
setSamplePeriodUs KEYWORD2
3135
getElapsedMs KEYWORD2
3236
isPressed KEYWORD2
3337
isReleased KEYWORD2
3438
onPress KEYWORD2
3539
onRelease KEYWORD2
3640
onChange KEYWORD2
37-
toggle KEYWORD2
3841
blink KEYWORD2
3942
pressedFor KEYWORD2
4043
releasedFor KEYWORD2
4144
retrigger KEYWORD2
4245
pressCode KEYWORD2
43-
debounceInput KEYWORD2
46+
debouncer KEYWORD2
4447
isUP KEYWORD2
4548
isMID KEYWORD2
4649
isDN KEYWORD2
@@ -57,6 +60,6 @@ input LITERAL1
5760
input_pullup LITERAL1
5861
input_pulldown LITERAL1
5962
input_bit LITERAL1
60-
input_port LITERAL1
63+
input_byte LITERAL1
6164
MULTI LITERAL1
6265
LONG LITERAL1

library.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "Toggle",
3-
"version": "3.1.4",
3+
"version": "3.1.5",
44
"description": "Arduino bounce library for debouncing hardware, signals and data. Works with all switch types, port expander and other 8-bit data sources. Robust debounce algorithm.",
55
"keywords": "debounce, toggle, button, switch, data, deglitch",
66
"repository":

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=Toggle
2-
version=3.1.4
2+
version=3.1.5
33
author=David Lloyd
44
maintainer=David Lloyd <[email protected]>
55
sentence=Arduino bounce library for deglitching and debouncing hardware, signals and data. Works with all switch types, port expander and other 8-bit data sources.

0 commit comments

Comments
 (0)