Skip to content

Commit c46c2f5

Browse files
committed
Update
More responsive pressCode() function
1 parent 71573f8 commit c46c2f5

File tree

7 files changed

+33
-16
lines changed

7 files changed

+33
-16
lines changed

README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -307,11 +307,11 @@ if (retrigger(500)) {
307307
##### Description
308308

309309
- Up to 225 possible codes with one button. The returned code (*byte*) is easy to interpret when viewed in hex format. For example, `47` is 4 long, 7 short presses. `F2` is double-click, `F7` is 7 `F`ast clicks.
310-
- Fast-click mode is detected if the first 2 clicks (presses) are less than 0.4 sec, then counts any extra presses if the duration is less than 1 sec, up to 15 max (code `FF`)
311-
- Detection of long (greater than 1 sec) presses and short (less than 1 sec) presses occurs if the first press is 0.4 sec or longer.
310+
- Fast-click mode is detected if the first several clicks (presses) are less than 0.2 sec, then all presses are counted as fast, up to 15 max (code `FF`)
311+
- Detection of long presses occurs if the first press is greater than 0.2 sec, then all presses greater than 0.2 sec are counted as long and all presses less than 0.2 sec are counted as short presses.
312312
- Detect up to 15 short presses
313313
- Detect up to 14 long presses
314-
- Returns code after button is released for 1 sec
314+
- Returns code after button is released for 0.5 sec
315315
- simplifies your code while adding maximum functionality to one button
316316

317317
##### Example

examples/Press_Code/Press_Code.ino

+17-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,23 @@
33
===================
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.
6-
In debug mode, the serial monitor shows timing results and code.
6+
7+
⦿ Up to 225 possible codes with one button. The returned code (byte) is easy to
8+
interpret when viewed in hex format. For example, `47` is 4 long, 7 short presses.
9+
F2 is double-click, F7 is 7 `F`ast clicks.
10+
11+
⦿ Fast-click mode is detected if the first several presses are less than 0.2 sec,
12+
then all presses are counted as fast, up to 15 max (code `FF`).
13+
14+
⦿ Detection of long presses occurs if the first press is greater than 0.2 sec,
15+
then all presses greater than 0.2 sec are counted as long
16+
and all presses less than 0.2 sec are counted as short presses.
17+
18+
⦿ Detect up to 15 short presses
19+
⦿ Detect up to 14 long presses
20+
⦿ Returns code after button is released for 0.5 sec
21+
⦿ simplifies your code while adding maximum functionality to one button
22+
723
Try on Wokwi UNO: https://wokwi.com/projects/334284248581145170
824
ESP32-S2: https://wokwi.com/projects/334320246564323924
925
***********************************************************************/

keywords.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -62,5 +62,5 @@ input_pullup LITERAL1
6262
input_pulldown LITERAL1
6363
input_bit LITERAL1
6464
input_byte LITERAL1
65-
MULTI LITERAL1
66-
LONG LITERAL1
65+
FAST LITERAL1
66+
DONE LITERAL1

library.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "Toggle",
3-
"version": "3.1.7",
3+
"version": "3.1.8",
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.7
2+
version=3.1.8
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-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/************************************************
2-
Toggle Library for Arduino - Version 3.1.7
2+
Toggle Library for Arduino - Version 3.1.8
33
by dlloydev https://github.com/Dlloydev/Toggle
44
Licensed under the MIT License.
55
************************************************/
@@ -196,7 +196,7 @@ uint8_t Toggle::pressCode(bool debug) {
196196
switch (_state) {
197197
case PB_DEFAULT:
198198
elapsedMs = getElapsedMs();
199-
if (pCode && isReleased() && (elapsedMs > (CLICK::LONG))) _state = PB_DONE;
199+
if (pCode && isReleased() && (elapsedMs > (CLICK::DONE))) _state = PB_DONE;
200200
if (onChange()) clearTimer();
201201
if (onPress()) {
202202
_state = PB_ON_PRESS;
@@ -214,12 +214,13 @@ uint8_t Toggle::pressCode(bool debug) {
214214
break;
215215

216216
case PB_ON_RELEASE:
217-
if ((elapsedMs < CLICK::MULTI) && (!pCode || (pCode > 0xEF))) _state = PB_MULTI_CLICKS;
218-
else if (elapsedMs < CLICK::LONG) _state = PB_SHORT_CLICKS;
219-
else _state = PB_LONG_CLICKS;
217+
if (elapsedMs > CLICK::FAST && (!pCode || pCode >= 0x10)) _state = PB_LONG_CLICKS;
218+
else if (elapsedMs < CLICK::FAST && pCode >= 0x10) _state = PB_SHORT_CLICKS;
219+
else if (elapsedMs < CLICK::FAST && !pCode) _state = PB_FAST_CLICKS;
220+
else _state = PB_SHORT_CLICKS;
220221
break;
221222

222-
case PB_MULTI_CLICKS:
223+
case PB_FAST_CLICKS:
223224
pCode |= 0xF0;
224225
if ((pCode & 0x0F) < 0x0F) pCode += 1;
225226
_state = PB_DEFAULT;

src/Toggle.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,12 @@ class Toggle {
4747

4848
private:
4949

50-
enum CLICK : uint16_t {MULTI = 400, LONG = 1000};
50+
enum CLICK : uint16_t {FAST = 200, DONE = 500};
5151
enum fsm_t : uint8_t { // finite state machine
5252
PB_DEFAULT = 0,
5353
PB_ON_PRESS = 1,
5454
PB_ON_RELEASE = 2,
55-
PB_MULTI_CLICKS = 3,
55+
PB_FAST_CLICKS = 3,
5656
PB_SHORT_CLICKS = 4,
5757
PB_LONG_CLICKS = 5,
5858
PB_DONE = 6

0 commit comments

Comments
 (0)