Skip to content

Commit 6353a34

Browse files
committed
Update
Updated examples New Wokwi examples Updated timer functions
1 parent 4760e5e commit 6353a34

File tree

10 files changed

+74
-14
lines changed

10 files changed

+74
-14
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Arduino button debounce library for various switch types, port expanders and oth
55
## Features
66

77
- Fast, robust and symetrical [debouncing](https://github.com/Dlloydev/Toggle/wiki/Debounce-Algorithm) of both press and release button transitions
8-
- Works with various [switch types](https://github.com/Dlloydev/Toggle/wiki/Switch-Connections)
8+
- Works with various [switch types and connections](https://github.com/Dlloydev/Toggle/wiki/Switch-Connections).
99
- Identifies 7 unique transitions for 3-position switches
1010
- Use momentary button as toggle switch (configurable)
1111
- Return up to 225 codes from one button

examples/Press_Code/Press_Code.ino

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
A simple example that demonstrates how fast-clicks, short presses and
55
long presses can be automatically detected and combined into a byte code.
66
In debug mode, the serial monitor shows timing results and code.
7+
Try on Wokwi UNO: https://wokwi.com/projects/334284248581145170
8+
ESP32-S2: https://wokwi.com/projects/334320246564323924
79
***********************************************************************/
810

911
#include <Toggle.h>

examples/Pressed_For/Pressed_For.ino

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*****************************************************************
2+
PRESSED FOR EXAMPLE:
3+
While a button is pressed, returns true after elapsed ms
4+
Try on Wokwi UNO: https://wokwi.com/projects/334457647787934292
5+
ESP32-S2: https://wokwi.com/projects/334454109412262483
6+
****************************************************************/
7+
8+
#include <Toggle.h>
9+
10+
const byte buttonPin = 2;
11+
const byte ledPin = LED_BUILTIN;
12+
const unsigned int ms = 1000;
13+
14+
Toggle sw1(buttonPin);
15+
16+
void setup() {
17+
pinMode(ledPin, OUTPUT);
18+
sw1.begin(buttonPin);
19+
}
20+
21+
void loop() {
22+
sw1.poll();
23+
digitalWrite(ledPin, sw1.pressedFor(ms));
24+
}
+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*****************************************************************
2+
RELEASED FOR EXAMPLE:
3+
While a button is released, returns true after elapsed ms
4+
Try on Wokwi UNO: https://wokwi.com/projects/334458460245590611
5+
ESP32-S2: https://wokwi.com/projects/334458628485415508
6+
****************************************************************/
7+
8+
#include <Toggle.h>
9+
10+
const byte buttonPin = 2;
11+
const byte ledPin = LED_BUILTIN;
12+
const unsigned int ms = 1000;
13+
14+
Toggle sw1(buttonPin);
15+
16+
void setup() {
17+
pinMode(ledPin, OUTPUT);
18+
sw1.begin(buttonPin);
19+
}
20+
21+
void loop() {
22+
sw1.poll();
23+
digitalWrite(ledPin, sw1.releasedFor(ms));
24+
}

examples/Retrigger_On_Press/Retrigger_On_Press.ino renamed to examples/Retrigger_While_Pressed/Retrigger_While_Pressed.ino

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
/**************************************************************************
2-
Retrigger On Press Example:
3-
===========================
2+
Retrigger While Pressed Example:
3+
================================
44
An example that shows how to retrigger an event while a button is pressed.
55
The retrigger period (ms) determines the rate. Each expired time period is
66
counted and displayed on the serial monitor. The built in LED toggles on
77
each trigger event.
8+
Try on Wokwi UNO: https://wokwi.com/projects/334458963671122515
9+
ESP32-S2: https://wokwi.com/projects/334459280465855059
810
*************************************************************************/
911

1012
#include <Toggle.h>
1113

1214
const byte buttonPin = 2;
13-
const word retriggerMs = 200;
15+
const word retriggerMs = 1000;
1416
const byte ledPin = LED_BUILTIN;
1517
bool ledState = false;
1618
byte trigCount = 0;

examples/Toggle_Basic/Toggle_Basic.ino

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
1-
/**********************************************************
1+
/****************************************************************
22
TOGGLE BASIC EXAMPLE:
33
This example blinks the built in LED as configured below.
44
INPUT_PULLUP and debouncing are pre-configured.
5-
*********************************************************/
5+
Try on Wokwi UNO: https://wokwi.com/projects/334443631893021266
6+
ESP32-S2: https://wokwi.com/projects/334453650605736531
7+
***************************************************************/
68

79
#include <Toggle.h>
810

911
const byte buttonPin = 2;
1012
const byte ledPin = LED_BUILTIN;
1113

12-
const byte blinkMs = 100; // blink duration (ms)
14+
const unsigned int blinkMs = 100; // blink duration (ms)
1315
const byte blinkMode = 1; // 0: on change, 1: on press (default), 2: on release
1416

1517
Toggle button(buttonPin);

examples/Use_Button_As_Toggle/Use_Button_As_Toggle.ino

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
/***********************************************
1+
/***************************************************************
22
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.
55
The built in LED changes state as configured.
6-
**********************************************/
6+
Try on Wokwi UNO: https://wokwi.com/projects/334321940028195411
7+
ESP32-S2: https://wokwi.com/projects/334322217034711635
8+
****************************************************************/
79

810
#include <Toggle.h>
911

library.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "Toggle",
3-
"version": "3.1.6",
3+
"version": "3.1.7",
44
"description": "Arduino button debounce library for various switch types, port expanders and other 8-bit data sources. Fast and robust debounce algorithm.",
55
"keywords": "debounce, toggle, button, switch, data, deglitch",
66
"repository":

library.properties

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=Toggle
2-
version=3.1.6
2+
version=3.1.7
33
author=David Lloyd
44
maintainer=David Lloyd <dlloydev@testcor.ca>
55
sentence=AArduino button debounce library for various switch types, port expanders and other 8-bit data sources.

src/Toggle.cpp

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/************************************************
2-
Toggle Library for Arduino - Version 3.1.6
2+
Toggle Library for Arduino - Version 3.1.7
33
by dlloydev https://github.com/Dlloydev/Toggle
44
Licensed under the MIT License.
55
************************************************/
@@ -159,24 +159,28 @@ bool Toggle::blink(uint16_t ms, uint8_t mode) {
159159
else if (mode == 0 && onChange()) clearTimer();
160160
onPress();
161161
onRelease();
162-
return (bool)(ms > (getElapsedMs()));
162+
if ((startUs * 0.001) > ms) return (bool)(ms > (getElapsedMs()));
163+
else return 0;
163164
}
164165

165166
bool Toggle::pressedFor(uint16_t ms) {
167+
if (onPress()) clearTimer();
166168
if (isPressed() && getElapsedMs() > ms) {
167169
return true;
168170
}
169171
return false;
170172
}
171173

172174
bool Toggle::releasedFor(uint16_t ms) {
175+
if (onRelease()) clearTimer();
173176
if (isReleased() && getElapsedMs() > ms) {
174177
return true;
175178
}
176179
return false;
177180
}
178181

179182
bool Toggle::retrigger(uint16_t ms) {
183+
if (onPress()) clearTimer();
180184
if (isPressed() && getElapsedMs() > ms) {
181185
clearTimer();
182186
return true;
@@ -191,7 +195,7 @@ uint8_t Toggle::pressCode(bool debug) {
191195
switch (_state) {
192196
case PB_DEFAULT:
193197
elapsedMs = getElapsedMs();
194-
if (pCode && isReleased() && (elapsedMs > (CLICK::LONG + CLICK::MULTI))) _state = PB_DONE;
198+
if (pCode && isReleased() && (elapsedMs > (CLICK::LONG))) _state = PB_DONE;
195199
if (onChange()) clearTimer();
196200
if (onPress()) {
197201
_state = PB_ON_PRESS;

0 commit comments

Comments
 (0)