Skip to content

Commit 73c5424

Browse files
committed
Update
Added new functions to set and get the timer mode. Now the timer can start onPress, onRelease or onChange events. New retrigger on press example added.
1 parent 1755ce9 commit 73c5424

File tree

7 files changed

+250
-146
lines changed

7 files changed

+250
-146
lines changed

README.md

Lines changed: 77 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ Looking at the columns (bit data) top to bottom, it can be seen that the debounc
3333
### Algorithms
3434

3535
```c++
36-
setAlgorithm(2); // Robust Mode, 2 glitches ignored
37-
setAlgorithm(1); // Normal Mode, 1 glitch ignored
38-
setAlgorithm(0); // Quick Mode, responds to spurious transitions
36+
setAlgorithm(2); // Robust Mode, 2 glitches ignored
37+
setAlgorithm(1); // Normal Mode, 1 glitch ignored
38+
setAlgorithm(0); // Quick Mode, responds to spurious transitions
3939
```
4040
4141
In Robust Mode, 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.
@@ -113,7 +113,7 @@ To use this library
113113
## Constructors
114114

115115
```c++
116-
Toggle(); // default constructor
116+
Toggle();
117117
Toggle(inA);
118118
Toggle(inA, inB);
119119
Toggle(*in);
@@ -123,18 +123,14 @@ Toggle(*in);
123123
124124
The constructor defines a button object. If the default constructor is used when declaring an array of pointers to button objects, then it must be followed by a call to begin in setup.
125125
126-
##### Syntax
127-
128-
`Toggle(inA);` <u>or</u> `Toggle(inA, inB);` <u>or</u> `Toggle(*in);`
129-
130126
##### Required parameter
131127
132-
**inA:** Arduino pin number that the button or switch is connected to *(byte)* , <u>or</u>
133-
***in:** Arduino variable representing the input signal *(byte)*
128+
**inA:** Arduino pin number that the button or switch is connected to *(byte)* , <u>or</u>
129+
***in:** Arduino variable representing the input signal *(byte)*
134130
135-
##### Optional parameters
131+
##### Optional parameter
136132
137-
**inB:** Second Arduino pin number *(byte)*. Use 2 inputs when connecting to 3 position switches.
133+
**inB:** Second Arduino pin number *(byte)*. Use 2 inputs when connecting to 3 position switches.
138134
139135
##### Returns
140136
@@ -143,11 +139,11 @@ None.
143139
##### Example
144140
145141
```c++
146-
// a button or switch is connected from pin 2 to ground
147-
// 5ms sample interval (default)
148-
// pullup enabled (default)
149-
// inverted = false (default)
150-
// algorithm = 2 glitches "Robust Mode" (default)
142+
/* a button or switch is connected from pin 2 to ground
143+
5000 μs sample interval (default)
144+
pullup enabled (default)
145+
inverted = false (default)
146+
algorithm = 2 glitches "Robust Mode" (default) */
151147
Toggle myInput(2);
152148
153149
// same as above, but a 3 position switch is connected to pins 2 and 3
@@ -169,9 +165,9 @@ This function is placed at the top of the loop. Initializes the Button object an
169165

170166
`myInput.poll();`
171167

172-
##### Parameters
168+
##### Optional parameter
173169

174-
None.
170+
**bit:** selects the bit number from an input data *(byte)*
175171

176172
##### Returns
177173

@@ -246,7 +242,7 @@ None.
246242
```c++
247243
if (myInput.onPress())
248244
{
249-
//do something (true only once per press)
245+
// do something (true only once per press)
250246
}
251247
```
252248

@@ -277,13 +273,11 @@ These functions checks the curent debounced output and its history to see if the
277273
##### Example
278274

279275
```c++
280-
if (myButton.isPressed())
281-
{
282-
//do something
276+
if (myButton.isPressed()) {
277+
// do something
283278
}
284-
else
285-
{
286-
//do something else
279+
else {
280+
// do something else
287281
}
288282
```
289283

@@ -319,7 +313,7 @@ There are 4 timer functions to make timing operations simple to use in your code
319313

320314
##### Description
321315

322-
This function sets the duration in milliseconds that the returned value is true after each state change. Useful to blink an LED to indicate every state change.
316+
This function sets the duration in milliseconds that the returned value is true after the set timer mode - onPress( 0), onRelease (1), onChange (2). Useful to blink an LED.
323317

324318
##### Syntax
325319

@@ -347,7 +341,7 @@ digitalWrite(ledPin, blink(100)); // blink an LED for 100ms just after each stat
347341
348342
##### Description
349343
350-
These functions check the the status register to see if the button or switch has set the pressedFor or releasedFor flag. These functions will return the flag status and clear the respective flag.
344+
These functions return true if the button or switch has been in the pressedFor or releasedFor state for at least the given number of milliseconds.
351345
352346
##### Syntax
353347
@@ -365,9 +359,8 @@ These functions check the the status register to see if the button or switch has
365359
##### Example
366360
367361
```c++
368-
if (myInput.pressedFor(500))
369-
{
370-
// true (once only) if button has been pressed for 500ms
362+
if (myInput.pressedFor(500)) {
363+
// true (once only) if button has been pressed for 500ms
371364
}
372365
```
373366

@@ -377,7 +370,7 @@ if (myInput.pressedFor(500))
377370

378371
##### Description
379372

380-
This function checks the duration in milliseconds that the button or switch is pressed and returns true (once only) each time the given millisecond duration has expired.
373+
This function checks the duration in milliseconds that the button or switch is is in the state as selected by timer mode and returns true (once only) each time the given millisecond duration has expired.
381374

382375
##### Syntax
383376

@@ -389,20 +382,19 @@ This function checks the duration in milliseconds that the button or switch is
389382

390383
##### Returns
391384

392-
*true* or *false*, returns true (once only) each time the given ms duration has expired while the button is pressed. *(bool)*
385+
*true* or *false*, returns true (once only) each time the given ms duration has expired while the button is in the state as selected by timer mode. *(bool)*
393386

394387
##### Example
395388

396389
```c++
397-
if (retrigger(500))
398-
{
390+
if (retrigger(500)) {
399391
// count every 500ms interval while the button is being pressed
400392
}
401393
```
402394

403395

404396

405-
## Set Functions
397+
## Set and Get Functions
406398

407399

408400

@@ -452,7 +444,7 @@ None.
452444

453445
##### Description
454446

455-
Sets the sample period in microseconds. Default is 5000μs.
447+
Sets the sample period in microseconds. Default is 5000 μs.
456448

457449
##### Syntax
458450

@@ -464,6 +456,54 @@ None.
464456

465457

466458

459+
## setTimerMode()
460+
461+
##### Description
462+
463+
Sets the timer mode to start onPress (0), onRelease (1) or onChange (2).
464+
465+
##### Syntax
466+
467+
`myInput.setTimerMode(0);`
468+
469+
##### Returns
470+
471+
None.
472+
473+
474+
475+
## getTimerMode()
476+
477+
##### Description
478+
479+
Gets the timer mode to start onPress (0), onRelease (1) or onChange (2).
480+
481+
##### Syntax
482+
483+
`myInput.getTimerMode();`
484+
485+
##### Returns
486+
487+
Timer Mode *(byte)*.
488+
489+
490+
491+
## getElapsedMs(()
492+
493+
##### Description
494+
495+
Gets the elapsed ms since the last state change selected by timer mode.
496+
497+
##### Syntax
498+
499+
`myInput.getElapsedMs();`
500+
501+
##### Returns
502+
503+
Elapsed milliseconds *(unsigned int)*.
504+
505+
506+
467507
## setAlgorithm()
468508

469509
##### Description
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**************************************************************************
2+
Retrigger On Press Example:
3+
===========================
4+
An example that shows how to retrigger an event while a button is pressed.
5+
The retrigger period (ms) determines the rate. Each expired time period is
6+
counted and displayed on the serial monitor. The built in LED toggles on
7+
each trigger event.
8+
*************************************************************************/
9+
10+
#include <Toggle.h>
11+
12+
const byte buttonPin = 2;
13+
const word retriggerMs = 200;
14+
const byte ledPin = LED_BUILTIN;
15+
bool ledState = false;
16+
byte trigCount = 0;
17+
18+
Toggle sw1(buttonPin);
19+
20+
void setup() {
21+
while (!Serial) { }; // Leonardo
22+
Serial.begin(115200);
23+
pinMode(ledPin, OUTPUT);
24+
sw1.begin(buttonPin);
25+
}
26+
27+
void loop() {
28+
sw1.poll();
29+
if (sw1.retrigger(retriggerMs)) {
30+
trigCount++;
31+
Serial.println(trigCount);
32+
ledState = !ledState;
33+
digitalWrite(ledPin, ledState);
34+
}
35+
if (sw1.onRelease()) {
36+
Serial.println(F("Released"));
37+
trigCount = 0;
38+
}
39+
}

keywords.txt

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,30 +25,32 @@ sw9 KEYWORD1
2525
##########################################
2626

2727
poll KEYWORD2
28-
toggle KEYWORD2
29-
blink KEYWORD2
28+
setAlgorithm KEYWORD2
29+
setInputMode KEYWORD2
30+
setInvertMode KEYWORD2
31+
setSamplePeriodUs KEYWORD2
32+
setTimerMode KEYWORD2
33+
getTimerMode KEYWORD2
34+
clearTimer KEYWORD2
35+
getElapsedMs KEYWORD2
3036
isPressed KEYWORD2
3137
isReleased KEYWORD2
3238
onPress KEYWORD2
3339
onRelease KEYWORD2
3440
onChange KEYWORD2
41+
toggle KEYWORD2
42+
blink KEYWORD2
3543
pressedFor KEYWORD2
3644
releasedFor KEYWORD2
3745
retrigger KEYWORD2
46+
debounceInput KEYWORD2
3847
isUP KEYWORD2
3948
isMID KEYWORD2
4049
isDN KEYWORD2
4150
UPtoMID KEYWORD2
4251
MIDtoDN KEYWORD2
4352
DNtoMID KEYWORD2
4453
MIDtoUP KEYWORD2
45-
setInputMode KEYWORD2
46-
setInvertMode KEYWORD2
47-
setSamplePeriodUs KEYWORD2
48-
setAlgorithm KEYWORD2
49-
debounceInput KEYWORD2
50-
getDebounceCount KEYWORD2
51-
getElapsedMs KEYWORD2
5254

5355
##########################################
5456
# Constants (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.0.1",
3+
"version": "3.0.2",
44
"description": "Arduino bounce library for deglitching and debouncing hardware, signals and data. Works with all switch types, port expander and other 8-bit data sources. Flexible algorithm with Robust, Normal and Quick response modes.",
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.0.1
2+
version=3.0.2
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)