Skip to content

Commit a3af3b3

Browse files
committed
Update
Update blink() function, readme and examples. New Use_Button_As_Toggle.ino example
1 parent cfaae73 commit a3af3b3

File tree

9 files changed

+56
-63
lines changed

9 files changed

+56
-63
lines changed

README.md

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

99
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).
1010

11-
### Algorithm
11+
### Debounce Algorithm
1212

1313
The debounce 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.![image](https://user-images.githubusercontent.com/63488701/171260623-befe88a4-66c4-44a2-a38b-6c14c715a92d.png)
1414

@@ -293,7 +293,7 @@ There are 4 timer functions to make timing operations simple to use in your code
293293

294294
##### Description
295295

296-
This function sets the duration in milliseconds that the returned value is true. The mode parameter sets what blink responds to: onPress( 0), onRelease (1), onChange (2).
296+
This function sets the duration in milliseconds that the returned value is true. The mode parameter sets what blink responds to: onChange (0), onPress( 1) default, onRelease (2).
297297

298298
##### Syntax
299299

@@ -303,19 +303,15 @@ This function sets the duration in milliseconds that the returned value is true.
303303

304304
**ms:** The number of milliseconds *(unsigned int)*
305305

306-
**mode:** Blink onPress( 0), onRelease (1), onChange (2) *(byte)*
306+
**mode:** Blink onChange (0), onPress( 1) default, onRelease (2) *(byte)*
307307

308308
##### Returns
309309

310310
*true* or *false*, depending on whether the elapsed time has expired after any state change set by the mode parameter. *(bool)*
311311

312312
##### Example
313313

314-
```c++
315-
digitalWrite(ledPin, blink(100)); // blink an LED for 100ms just after state change set by mode
316-
```
317-
318-
314+
[Toggle_Basic.ino](https://github.com/Dlloydev/Toggle/blob/main/examples/Toggle_Basic/Toggle_Basic.ino)
319315

320316
## pressedFor(ms)
321317

examples/Blink_On_Change/Blink_On_Change.ino

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

examples/Eight_buttons/Eight_buttons.ino

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
/******************************************************
2-
Eight Buttons (or Switches) Example:
3-
====================================
1+
/****************************************************
2+
EIGHT BUTTONS EXAMPLE:
43
An example that checks the status of eight buttons.
54
All input pins will have their pullups enabled.
65
The LED blinks for each on press transition.
7-
*****************************************************/
6+
***************************************************/
87

98
#include <Toggle.h>
109

@@ -31,7 +30,6 @@ void loop() {
3130
} else {
3231
ledState--;
3332
}
34-
sw[i].onPress();
3533
}
3634
digitalWrite(ledPin, ledState);
3735
}
Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,25 @@
1-
/************************************************************************
2-
Toggle Basic Example:
3-
=====================
4-
A simple example that toggles an LED each time a button is pressed
5-
or switch is closed. The input pin will have its pullup enabled.
6-
The serial monitor indicates all switch transitions.
7-
***********************************************************************/
1+
/**********************************************************
2+
TOGGLE BASIC EXAMPLE:
3+
This example blinks the built in LED as configured below.
4+
INPUT_PULLUP and debouncing are pre-configured.
5+
*********************************************************/
86

9-
#include <Toggle.h>
7+
#include "Toggle.h"
108

119
const byte buttonPin = 2;
1210
const byte ledPin = LED_BUILTIN;
1311

14-
Toggle sw1(buttonPin);
12+
const byte blinkMs = 100; // blink duration (ms)
13+
const byte blinkMode = 1; // 0: on change, 1: on press (default), 2: on release
14+
15+
Toggle button(buttonPin);
1516

1617
void setup() {
17-
while (!Serial) { }; // Leonardo
18-
Serial.begin(115200);
19-
sw1.begin(buttonPin);
18+
button.begin(buttonPin);
2019
pinMode(ledPin, OUTPUT);
2120
}
2221

2322
void loop() {
24-
sw1.poll();
25-
if (sw1.onPress()) Serial.println(F("sw1: OFF⇒ON"));
26-
if (sw1.onRelease()) Serial.println(F("sw1: ON⇒OFF"));
27-
digitalWrite(ledPin, sw1.toggle());
23+
button.poll();
24+
digitalWrite(ledPin, button.blink(blinkMs, blinkMode));
2825
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*******************************************************************************
2+
TURN A MOMENTARY PUSHBUTTON INTO A TOGGLE SWITCH:
3+
The button is connected from input pin to GND.
4+
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+
******************************************************************************/
9+
10+
#include <Toggle.h>
11+
12+
const byte buttonPin = 2;
13+
const byte ledPin = LED_BUILTIN;
14+
15+
Toggle button(buttonPin);
16+
17+
void setup() {
18+
button.begin(buttonPin);
19+
//button.setInvertMode(1);
20+
pinMode(ledPin, OUTPUT);
21+
}
22+
23+
void loop() {
24+
button.poll();
25+
digitalWrite(ledPin, button.toggle());
26+
}

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.3",
3+
"version": "3.1.4",
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.3
2+
version=3.1.4
33
author=David Lloyd
44
maintainer=David Lloyd <dlloydev@testcor.ca>
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.

src/Toggle.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/************************************************
2-
Toggle Library for Arduino - Version 3.1.3
2+
Toggle Library for Arduino - Version 3.1.4
33
by dlloydev https://github.com/Dlloydev/Toggle
44
Licensed under the MIT License.
55
************************************************/
@@ -108,9 +108,11 @@ bool Toggle::toggle(bool invert, uint8_t bit) {
108108
/************* button timer functions ****************/
109109

110110
bool Toggle::blink(uint16_t ms, uint8_t mode) {
111-
if (mode == 2 && onChange()) startUs = micros();
112-
else if (mode == 1 && onChange() == 2) startUs = micros();
113-
else if (onChange() == 1) startUs = micros();
111+
if (mode == 2 && onChange() == 2) startUs = micros();
112+
else if (mode == 1 && onChange() == 1) startUs = micros();
113+
else if (mode == 0 && onChange()) startUs = micros();
114+
onPress();
115+
onRelease();
114116
return (bool)(ms > (getElapsedMs()));
115117
}
116118

@@ -152,7 +154,6 @@ uint8_t Toggle::pressCode(bool debug) {
152154
if (pCode && isReleased() && (elapsedMs > (CLICK::LONG + CLICK::MULTI))) _state = PB_DONE;
153155
if (onChange()) startUs = micros();
154156
if (onPress()) {
155-
//Serial.print(F(" Released:\t")); Serial.print(elapsedMs); Serial.println(" ms");
156157
_state = PB_ON_PRESS;
157158
}
158159
if (onRelease()) {

src/Toggle.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ class Toggle {
2626
bool isReleased(uint8_t bit = 0); // returns true if released
2727
bool onPress(); // returns true if just pressed
2828
bool onRelease(); // returns true if just released
29-
uint8_t onChange(); // returns (0) no change, (1) onPress, (2) onRelease
29+
uint8_t onChange(); // returns: no change(0), onPress(1), onRelease(2)
3030
bool toggle(bool invert = 0, uint8_t bit = 0); // returns true/false (toggle) on each press
31-
bool blink(uint16_t ms, uint8_t mode = 0); // returns true for given ms (blink) on mode press(0), release(1), change(2)
31+
bool blink(uint16_t ms, uint8_t mode = 1); // returns true for given ms (blink) on mode: change(0), press(1), release(2),
3232
bool pressedFor(uint16_t ms); // returns true if pressed for at least the given ms
3333
bool releasedFor(uint16_t ms); // returns true if released for at least the given ms
3434
bool retrigger(uint16_t ms); // returns true each time the given ms expires while the button is pressed

0 commit comments

Comments
 (0)