Skip to content

Commit 1755ce9

Browse files
committed
Update
- Fixed toggle function - Added `begin()` function to setup in examples - Updated readme
1 parent c333169 commit 1755ce9

File tree

13 files changed

+47
-42
lines changed

13 files changed

+47
-42
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ Toggle(*in);
121121
122122
##### Description
123123
124-
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. Otherwise, begin() is automatically called and not needed in setup.
124+
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
126126
##### Syntax
127127
@@ -293,7 +293,7 @@ else
293293

294294
##### Description
295295

296-
This function will toggle the return value after each state change. Useful to easily toggle an LED responding to every state change.
296+
This function will toggle the return value after each `onPress` state change. Useful to easily toggle an LED.
297297

298298
##### Syntax
299299

@@ -305,7 +305,7 @@ This function will toggle the return value after each state change. Useful to ea
305305

306306
##### Returns
307307

308-
*true* or *false*, toggles after any state change. *(bool)
308+
*true* or *false*, toggles after each `onPress` state change. *(bool)
309309

310310

311311

examples/Input_Bit/Input_Bit.ino

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
#include <Toggle.h>
1010

1111
const byte ledPin = LED_BUILTIN;
12-
byte ledState = LOW;
1312

1413
byte bit = 0; // choose bit(0-7)
1514
byte Input;
@@ -20,6 +19,7 @@ void setup() {
2019
pinMode(ledPin, OUTPUT);
2120
while (!Serial) { }; // Leonardo
2221
Serial.begin(115200);
22+
myInput.begin(Input);
2323
myInput.setInputMode(myInput.inMode::input_bit);
2424
myInput.setSamplePeriodUs(20); // 0-65535μs
2525
}
@@ -28,10 +28,9 @@ void loop() {
2828
myInput.poll(bit);
2929
if (myInput.onPress()) {
3030
Serial.print(F("b")); Serial.print(bit); Serial.println(F(": OFF⇒ON"));
31-
ledState = !ledState;
32-
digitalWrite(ledPin, ledState);
33-
}
31+
}
3432
if (myInput.onRelease()) {
3533
Serial.print(F("b")); Serial.print(bit); Serial.println(F(": ON⇒OFF"));
3634
}
35+
digitalWrite(ledPin, myInput.toggle(bit));
3736
}

examples/Input_Bit_Test/Input_Bit_Test.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ Toggle myInput(&Input);
2323
void setup() {
2424
while (!Serial) { }; // Leonardo
2525
Serial.begin(115200);
26-
26+
myInput.begin(Input);
2727
myInput.setInputMode(myInput.inMode::input_port);
2828
myInput.setAlgorithm(2); // ignore(2), (1) or (0) glitches for robust, normal and quick response
2929
myInput.setSamplePeriodUs(20); // 0-65535μs

examples/Input_Port/Input_Port.ino

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
#include <Toggle.h>
1010

1111
const byte ledPin = LED_BUILTIN;
12-
byte ledState = LOW;
1312

1413
byte bit = 0; // choose bit(0-7)
1514
byte Input;
@@ -20,6 +19,7 @@ void setup() {
2019
pinMode(ledPin, OUTPUT);
2120
while (!Serial) { }; // Leonardo
2221
Serial.begin(115200);
22+
myInput.begin(Input);
2323
myInput.setInputMode(myInput.inMode::input_port); // debounce all bits
2424
myInput.setSamplePeriodUs(20); // 0-65535μs
2525
}
@@ -28,10 +28,9 @@ void loop() {
2828
myInput.poll();
2929
if (myInput.onPress()) {
3030
Serial.print(F("b")); Serial.print(bit); Serial.println(F(": OFF⇒ON"));
31-
ledState = !ledState;
32-
digitalWrite(ledPin, ledState);
3331
}
3432
if (myInput.onRelease()) {
3533
Serial.print(F("b")); Serial.print(bit); Serial.println(F(": ON⇒OFF"));
3634
}
35+
digitalWrite(ledPin, myInput.toggle(bit));
3736
}

examples/Input_Port_Test/Input_Port_Test.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ Toggle myInput(&Input);
5656
void setup() {
5757
while (!Serial) { }; // Leonardo
5858
Serial.begin(115200);
59-
59+
myInput.begin(Input);
6060
myInput.setInputMode(myInput.inMode::input_port);
6161
myInput.setAlgorithm(2); // ignore(2), (1) or (0) glitches for robust, normal and quick response
6262
myInput.setSamplePeriodUs(20); // 0-65535μs

examples/Input_Pulldown/Input_Pulldown.ino

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,21 @@
1010

1111
const byte buttonPin = 2;
1212
const byte ledPin = LED_BUILTIN;
13-
byte ledState = LOW;
1413

1514
Toggle sw1(buttonPin);
1615

1716
void setup() {
1817
pinMode(ledPin, OUTPUT);
1918
while (!Serial) { }; // Leonardo
2019
Serial.begin(115200);
20+
sw1.begin(buttonPin);
2121
sw1.setInputMode(sw1.inMode::input_pulldown);
2222
sw1.setInvertMode(true);
2323
}
2424

2525
void loop() {
2626
sw1.poll();
27-
if (sw1.onPress()) {
28-
Serial.println(F("sw1: OFF⇒ON"));
29-
ledState = !ledState;
30-
digitalWrite(ledPin, ledState);
31-
}
27+
if (sw1.onPress()) Serial.println(F("sw1: OFF⇒ON"));
3228
if (sw1.onRelease()) Serial.println(F("sw1: ON⇒OFF"));
29+
digitalWrite(ledPin, sw1.toggle());
3330
}
Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,38 @@
1-
/************************************************************************
1+
/******************************************************************
22
One Button and One Three Position Switch:
33
=========================================
4-
This example checks each time a button or 3-position switch
4+
This example that blinks an LED each time the button
55
has transitioned. All input pins will have their pullups enabled.
66
Open the Serial Monitor to view transition status.
7-
***********************************************************************/
7+
*****************************************************************/
88

99
#include <Toggle.h>
1010

1111
const byte buttonPin = 2;
1212
const byte swPinA = 3;
1313
const byte swPinB = 4;
14+
const byte ledPin = LED_BUILTIN;
1415

1516
Toggle sw1(buttonPin); // button
1617
Toggle sw2(swPinA, swPinB); // 3-position switch
1718

1819
void setup() {
20+
pinMode(ledPin, OUTPUT);
1921
while (!Serial) { }; // Leonardo
2022
Serial.begin(115200);
23+
sw1.begin(buttonPin);
24+
sw2.begin(swPinA, swPinB);
2125
}
2226

2327
void loop() {
2428
sw1.poll();
2529
sw2.poll();
30+
2631
if (sw1.onPress()) Serial.println(F("sw1: OFF⇒ON"));
2732
if (sw1.onRelease()) Serial.println(F("sw1: ON⇒OFF"));
2833
if (sw2.UPtoMID()) Serial.println(F("sw2: UP⇒MID"));
2934
if (sw2.MIDtoDN()) Serial.println(F("sw2: MID⇒DN"));
3035
if (sw2.DNtoMID()) Serial.println(F("sw2: DN⇒MID"));
3136
if (sw2.MIDtoUP()) Serial.println(F("sw2: MID⇒UP"));
37+
digitalWrite(ledPin, sw1.toggle());
3238
}
Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,32 @@
1-
/************************************************************************
1+
/***********************************************************************
22
Using Three Position Switches:
33
==============================
44
A simple example that toggles an LED each time a 3-position switch has
55
transitioned. Both input pins will have its pullup enabled. The
66
switch is in the disconnected MID position if both inpits read high.
7-
• open Serial Monitor to view transition status.
8-
***********************************************************************/
7+
Open the Serial Monitor to view transition status.
8+
**********************************************************************/
99

1010
#include <Toggle.h>
1111

1212
const byte pinA = 2;
1313
const byte pinB = 3;
1414
const byte ledPin = LED_BUILTIN;
1515

16-
Toggle sw1(pinA, pinB); // 3-position switch
16+
Toggle sw1(pinA, pinB); // 3-position switch
1717

1818
void setup() {
1919
pinMode(ledPin, OUTPUT);
2020
while (!Serial) { }; // Leonardo
2121
Serial.begin(115200);
22+
sw1.begin(pinA, pinB);
2223
}
2324

2425
void loop() {
2526
sw1.poll();
26-
// call toggle() just after poll(). Toggles on MID⇒UP transitions only
27-
digitalWrite(ledPin, sw1.toggle());
28-
2927
if (sw1.UPtoMID()) Serial.println(F("sw1: UP⇒MID"));
3028
if (sw1.MIDtoDN()) Serial.println(F("sw1: MID⇒DN"));
3129
if (sw1.DNtoMID()) Serial.println(F("sw1: DN⇒MID"));
3230
if (sw1.MIDtoUP()) Serial.println(F("sw1: MID⇒UP"));
31+
digitalWrite(ledPin, sw1.toggle()); // toggles on MID⇒UP transition only
3332
}

examples/Toggle_Basic/Toggle_Basic.ino

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,15 @@ const byte ledPin = LED_BUILTIN;
1414
Toggle sw1(buttonPin);
1515

1616
void setup() {
17-
pinMode(ledPin, OUTPUT);
1817
while (!Serial) { }; // Leonardo
1918
Serial.begin(115200);
19+
sw1.begin(buttonPin);
20+
pinMode(ledPin, OUTPUT);
2021
}
2122

2223
void loop() {
2324
sw1.poll();
24-
digitalWrite(ledPin, sw1.toggle()); // call toggle() just after poll()
2525
if (sw1.onPress()) Serial.println(F("sw1: OFF⇒ON"));
2626
if (sw1.onRelease()) Serial.println(F("sw1: ON⇒OFF"));
27+
digitalWrite(ledPin, sw1.toggle());
2728
}

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.0",
3+
"version": "3.0.1",
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":

0 commit comments

Comments
 (0)