Skip to content

Commit c333169

Browse files
committed
Update
Major release with new functions and updated readme with function reference and updated examples.
1 parent dd05173 commit c333169

16 files changed

Lines changed: 618 additions & 333 deletions

File tree

README.md

Lines changed: 394 additions & 104 deletions
Large diffs are not rendered by default.

examples/Eight_buttons/Eight_buttons.ino

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,30 @@
33
====================================
44
An example that checks the status of eight buttons.
55
All input pins will have their pullups enabled.
6-
The serial monitor shows switch transitions by pin number.
6+
The LED toggles for each on press transition.
77
**********************************************************/
88

99
#include <Toggle.h>
1010

1111
const byte num = 8; // number of buttons
1212
const byte pin[num] = {2, 3, 4, 5, 6, 7, 8, 9}; //button pins
13+
const byte ledPin = 13;
14+
byte ledState = LOW;
1315

1416
Toggle *sw = new Toggle[num];
1517

1618
void setup() {
17-
while (!Serial) { }; // Leonardo
18-
Serial.begin(115200);
1919
for (int i = 0; i < num; ++i) {
20+
sw[i].begin(pin[i]);
2021
sw[i] = pin[i];
2122
}
2223
}
2324

2425
void loop() {
25-
for (int i = 0; i < num; i++) {
26+
27+
for (int i = 0; i < num; i++) {
2628
sw[i].poll();
27-
if (sw[i].OFFtoON()) {
28-
Serial.print("pin"); Serial.print(pin[i]); Serial.println(": OFF⇒ON");
29-
}
30-
if (sw[i].ONtoOFF()) {
31-
Serial.print("pin"); Serial.print(pin[i]); Serial.println(": ON⇒OFF");
32-
}
29+
if (sw[i].onPress()) ledState = !ledState;
3330
}
31+
digitalWrite(ledPin, ledState);
3432
}

examples/Input_Bit/Input_Bit.ino

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,17 @@ void setup() {
2121
while (!Serial) { }; // Leonardo
2222
Serial.begin(115200);
2323
myInput.setInputMode(myInput.inMode::input_bit);
24-
myInput.setSampleUs(20); // sample ASAP
24+
myInput.setSamplePeriodUs(20); // 0-65535μs
2525
}
2626

2727
void loop() {
2828
myInput.poll(bit);
29-
if (myInput.OFFtoON(bit)) {
29+
if (myInput.onPress()) {
3030
Serial.print(F("b")); Serial.print(bit); Serial.println(F(": OFF⇒ON"));
3131
ledState = !ledState;
3232
digitalWrite(ledPin, ledState);
3333
}
34-
if (myInput.ONtoOFF(bit)) {
34+
if (myInput.onRelease()) {
3535
Serial.print(F("b")); Serial.print(bit); Serial.println(F(": ON⇒OFF"));
3636
}
3737
}

examples/Input_Bit_Test/Input_Bit_Test.ino

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,19 @@ void setup() {
2525
Serial.begin(115200);
2626

2727
myInput.setInputMode(myInput.inMode::input_port);
28-
myInput.setSampleUs(20);
29-
myInput.setAlgorithm(2);
28+
myInput.setAlgorithm(2); // ignore(2), (1) or (0) glitches for robust, normal and quick response
29+
myInput.setSamplePeriodUs(20); // 0-65535μs
3030

3131
for (int i = 0; i < 47; i++) {
3232
Input = dat[i] << bit;
3333
myInput.poll(bit);
3434

3535
for (int i = 0; i < 10; i++) { // zoom horizontal
3636
plot("In", Input + 8, false);
37-
plot("isOFF", myInput.isOFF(bit) + 6, false);
38-
plot("OFF2ON", myInput.OFFtoON(bit) + 4, false);
39-
plot("ON2OFF", myInput.ONtoOFF(bit) + 2, false);
40-
plot("isON", myInput.isON(bit), true);
37+
plot("isOFF", myInput.isReleased(bit) + 6, false);
38+
plot("OFF2ON", myInput.onPress() + 4, false);
39+
plot("ON2OFF", myInput.onRelease() + 2, false);
40+
plot("isON", myInput.isPressed(bit), true);
4141
}
4242
}
4343
}

examples/Input_Port/Input_Port.ino

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,17 @@ void setup() {
2121
while (!Serial) { }; // Leonardo
2222
Serial.begin(115200);
2323
myInput.setInputMode(myInput.inMode::input_port); // debounce all bits
24-
myInput.setSampleUs(20);
24+
myInput.setSamplePeriodUs(20); // 0-65535μs
2525
}
2626

2727
void loop() {
2828
myInput.poll();
29-
if (myInput.OFFtoON(bit)) {
29+
if (myInput.onPress()) {
3030
Serial.print(F("b")); Serial.print(bit); Serial.println(F(": OFF⇒ON"));
3131
ledState = !ledState;
3232
digitalWrite(ledPin, ledState);
3333
}
34-
if (myInput.ONtoOFF(bit)) {
34+
if (myInput.onRelease()) {
3535
Serial.print(F("b")); Serial.print(bit); Serial.println(F(": ON⇒OFF"));
3636
}
3737
}

examples/Input_Port_Test/Input_Port_Test.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ void setup() {
5959

6060
myInput.setInputMode(myInput.inMode::input_port);
6161
myInput.setAlgorithm(2); // ignore(2), (1) or (0) glitches for robust, normal and quick response
62-
myInput.setSampleUs(20);
62+
myInput.setSamplePeriodUs(20); // 0-65535μs
6363

6464
for (int i = 0; i < 15; i++) {
6565
Input = dat[i];

examples/Input_Pulldown/Input_Pulldown.ino

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ void setup() {
2424

2525
void loop() {
2626
sw1.poll();
27-
if (sw1.OFFtoON()) {
27+
if (sw1.onPress()) {
2828
Serial.println(F("sw1: OFF⇒ON"));
2929
ledState = !ledState;
3030
digitalWrite(ledPin, ledState);
3131
}
32-
if (sw1.ONtoOFF()) Serial.println(F("sw1: ON⇒OFF"));
32+
if (sw1.onRelease()) Serial.println(F("sw1: ON⇒OFF"));
3333
}
Lines changed: 8 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,32 @@
11
/************************************************************************
22
One Button and One Three Position Switch:
33
=========================================
4-
This example that blinks an LED each time a button or 3-position switch
4+
This example checks each time a button or 3-position switch
55
has transitioned. All input pins will have their pullups enabled.
6-
• the built-in LED will blink once for each transition.
7-
• open Serial Monitor to view transition status.
6+
Open the Serial Monitor to view transition status.
87
***********************************************************************/
98

109
#include <Toggle.h>
1110

1211
const byte buttonPin = 2;
1312
const byte swPinA = 3;
1413
const byte swPinB = 4;
15-
const byte ledPin = LED_BUILTIN;
1614

1715
Toggle sw1(buttonPin); // button
1816
Toggle sw2(swPinA, swPinB); // 3-position switch
1917

2018
void setup() {
21-
pinMode(ledPin, OUTPUT);
2219
while (!Serial) { }; // Leonardo
2320
Serial.begin(115200);
2421
}
2522

2623
void loop() {
2724
sw1.poll();
2825
sw2.poll();
29-
30-
if (sw1.OFFtoON()) {
31-
Serial.println(F("sw1: OFF⇒ON"));
32-
blink();
33-
}
34-
if (sw1.ONtoOFF()) {
35-
Serial.println(F("sw1: ON⇒OFF"));
36-
blink();
37-
}
38-
if (sw2.UPtoMID()) {
39-
Serial.println(F("sw2: UP⇒MID"));
40-
blink();
41-
}
42-
if (sw2.MIDtoDN()) {
43-
Serial.println(F("sw2: MID⇒DN"));
44-
blink();
45-
}
46-
if (sw2.DNtoMID()) {
47-
Serial.println(F("sw2: DN⇒MID"));
48-
blink();
49-
}
50-
if (sw2.MIDtoUP()) {
51-
Serial.println(F("sw2: MID⇒UP"));
52-
blink();
53-
}
54-
}
55-
56-
void blink() {
57-
digitalWrite(ledPin, HIGH);
58-
delay(20);
59-
digitalWrite(ledPin, LOW);
26+
if (sw1.onPress()) Serial.println(F("sw1: OFF⇒ON"));
27+
if (sw1.onRelease()) Serial.println(F("sw1: ON⇒OFF"));
28+
if (sw2.UPtoMID()) Serial.println(F("sw2: UP⇒MID"));
29+
if (sw2.MIDtoDN()) Serial.println(F("sw2: MID⇒DN"));
30+
if (sw2.DNtoMID()) Serial.println(F("sw2: DN⇒MID"));
31+
if (sw2.MIDtoUP()) Serial.println(F("sw2: MID⇒UP"));
6032
}

examples/Three_Position_Switch/Three_Position_Switch.ino

Lines changed: 7 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
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-
• the built-in LED will blink once for each transition.
87
• open Serial Monitor to view transition status.
98
***********************************************************************/
109

@@ -24,26 +23,11 @@ void setup() {
2423

2524
void loop() {
2625
sw1.poll();
27-
if (sw1.UPtoMID()) {
28-
Serial.println(F("sw1: UP⇒MID"));
29-
blink();
30-
}
31-
if (sw1.MIDtoDN()) {
32-
Serial.println(F("sw1: MID⇒DN"));
33-
blink();
34-
}
35-
if (sw1.DNtoMID()) {
36-
Serial.println(F("sw1: DN⇒MID"));
37-
blink();
38-
}
39-
if (sw1.MIDtoUP()) {
40-
Serial.println(F("sw1: MID⇒UP"));
41-
blink();
42-
}
43-
}
44-
45-
void blink() {
46-
digitalWrite(ledPin, HIGH);
47-
delay(20);
48-
digitalWrite(ledPin, LOW);
26+
// call toggle() just after poll(). Toggles on MID⇒UP transitions only
27+
digitalWrite(ledPin, sw1.toggle());
28+
29+
if (sw1.UPtoMID()) Serial.println(F("sw1: UP⇒MID"));
30+
if (sw1.MIDtoDN()) Serial.println(F("sw1: MID⇒DN"));
31+
if (sw1.DNtoMID()) Serial.println(F("sw1: DN⇒MID"));
32+
if (sw1.MIDtoUP()) Serial.println(F("sw1: MID⇒UP"));
4933
}

examples/Toggle_Basic/Toggle_Basic.ino

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

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

1514
Toggle sw1(buttonPin);
1615

@@ -22,10 +21,7 @@ void setup() {
2221

2322
void loop() {
2423
sw1.poll();
25-
if (sw1.OFFtoON()) {
26-
Serial.println(F("sw1: OFF⇒ON"));
27-
ledState = !ledState;
28-
digitalWrite(ledPin, ledState);
29-
}
30-
if (sw1.ONtoOFF()) Serial.println(F("sw1: ON⇒OFF"));
24+
digitalWrite(ledPin, sw1.toggle()); // call toggle() just after poll()
25+
if (sw1.onPress()) Serial.println(F("sw1: OFF⇒ON"));
26+
if (sw1.onRelease()) Serial.println(F("sw1: ON⇒OFF"));
3127
}

0 commit comments

Comments
 (0)