19
19
A click on the button turns the led on.
20
20
A doubleclick on the button changes the blink rate from ON to SLOW to FAST and back.
21
21
In the loop function the button.tick function has to be called as often as you like.
22
+
23
+ State-Diagram
24
+
25
+ start
26
+ | +-------\
27
+ V V |
28
+ -------- ------ |
29
+ | OFF |<--click-+->| ON | |
30
+ -------- | ------ |
31
+ | | |
32
+ | d-click |
33
+ | | |
34
+ | V |
35
+ | ------ |
36
+ +- | SLOW | |
37
+ | ------ |
38
+ | | |
39
+ | d-click |
40
+ | | |
41
+ | V d-click
42
+ | ------ |
43
+ +--| FAST |---/
44
+ ------
22
45
*/
23
46
24
47
// 06.10.2012 created by Matthias Hertel
48
+ // 26.03.2017 state diagram added, minor changes
25
49
26
50
#include " OneButton.h"
27
51
28
52
// The actions I ca do...
29
53
typedef enum {
30
- ACTION_NONE, // do nothing .
54
+ ACTION_OFF, // set LED "OFF" .
31
55
ACTION_ON, // set LED "ON"
32
56
ACTION_SLOW, // blink LED "SLOW"
33
- ACTION_FAST // blink LED "FAST"
57
+ ACTION_FAST // blink LED "FAST"
34
58
}
35
59
MyActions;
36
60
37
61
// Setup a new OneButton on pin A1.
38
62
OneButton button (A1, true );
39
63
40
- MyActions nextAction = ACTION_NONE; // no action when starting
64
+ MyActions nextAction = ACTION_OFF; // no action when starting
65
+
41
66
42
- // setup code here, to run once:
67
+ // setup code here, to run once.
43
68
void setup () {
44
69
// enable the standard led on pin 13.
45
70
pinMode (13 , OUTPUT); // sets the digital pin as output
@@ -61,7 +86,7 @@ void loop() {
61
86
62
87
// You can implement other code in here or just wait a while
63
88
64
- if (nextAction == ACTION_NONE ) {
89
+ if (nextAction == ACTION_OFF ) {
65
90
// do nothing.
66
91
digitalWrite (13 , LOW);
67
92
@@ -84,18 +109,16 @@ void loop() {
84
109
} else {
85
110
digitalWrite (13 , HIGH);
86
111
} // if
87
-
88
112
} // if
89
-
90
113
} // loop
91
114
92
115
93
116
// this function will be called when the button was pressed 1 time and them some time has passed.
94
117
void myClickFunction () {
95
- if (nextAction == ACTION_NONE )
118
+ if (nextAction == ACTION_OFF )
96
119
nextAction = ACTION_ON;
97
120
else
98
- nextAction = ACTION_NONE ;
121
+ nextAction = ACTION_OFF ;
99
122
} // myClickFunction
100
123
101
124
0 commit comments