|
1 | 1 | /************************************************************************ |
2 | 2 | One Button and One Three Position Switch: |
3 | 3 | ========================================= |
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 |
5 | 5 | 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. |
8 | 7 | ***********************************************************************/ |
9 | 8 |
|
10 | 9 | #include <Toggle.h> |
11 | 10 |
|
12 | 11 | const byte buttonPin = 2; |
13 | 12 | const byte swPinA = 3; |
14 | 13 | const byte swPinB = 4; |
15 | | -const byte ledPin = LED_BUILTIN; |
16 | 14 |
|
17 | 15 | Toggle sw1(buttonPin); // button |
18 | 16 | Toggle sw2(swPinA, swPinB); // 3-position switch |
19 | 17 |
|
20 | 18 | void setup() { |
21 | | - pinMode(ledPin, OUTPUT); |
22 | 19 | while (!Serial) { }; // Leonardo |
23 | 20 | Serial.begin(115200); |
24 | 21 | } |
25 | 22 |
|
26 | 23 | void loop() { |
27 | 24 | sw1.poll(); |
28 | 25 | 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")); |
60 | 32 | } |
0 commit comments