1
1
#include < Button_SL.hpp>
2
+ #include " Button_SL.hpp"
3
+
4
+ // ////////////////////////////////////////////////////////////////////////////
5
+ // / @brief Auxiliary class for time-controlled functions.
6
+ // /
7
+ // ////////////////////////////////////////////////////////////////////////////
8
+ class Timer {
9
+ public:
10
+ void start () { timeStamp = millis (); }
11
+ bool operator ()(const unsigned long duration) const { return millis () - timeStamp >= duration; }
12
+
13
+ private:
14
+ unsigned long timeStamp {0 };
15
+ };
2
16
3
17
// ////////////////////////////////////////////////
4
18
// Global constants and variables
5
19
// ////////////////////////////////////////////////
6
- constexpr uint8_t BUTTON_PIN1{2 };
7
- constexpr uint8_t BUTTON_PIN2{3 };
8
- constexpr uint8_t BUTTON_PIN3{4 };
20
+ constexpr uint8_t btn_pin1 {2 };
21
+ constexpr uint8_t btn_pin2 {3 };
22
+ constexpr uint8_t btn_pin3 {4 };
23
+ constexpr uint8_t led_short {6 };
24
+ constexpr uint8_t led_long {7 };
25
+ constexpr unsigned long duration {500 };
26
+ constexpr unsigned long blink_duration {200 };
9
27
10
28
using namespace Btn ;
11
29
12
- Button btn{BUTTON_PIN1 };
30
+ Button btn{btn_pin1 };
13
31
ButtonSL bArray[]{
14
- {BUTTON_PIN2},
15
- {BUTTON_PIN3 , 1500 , HIGH}
32
+ {btn_pin2},
33
+ {btn_pin3 , 1500 , HIGH}
16
34
};
17
- // ////////////////////////////////////////////////
18
- // Function forward declaration
19
- // ////////////////////////////////////////////////
35
+ Timer wait;
20
36
21
37
// ////////////////////////////////////////////////////////////////////////////
22
38
// / @brief Initialize the program.
@@ -25,14 +41,16 @@ ButtonSL bArray[]{
25
41
void setup () {
26
42
Serial.begin (115200 );
27
43
Serial.println (" Start" );
44
+ pinMode (led_short, OUTPUT);
45
+ pinMode (led_long, OUTPUT);
46
+
28
47
// Initialize Buttons
29
48
for (auto &buttons : bArray) {
30
49
buttons.begin ();
31
50
}
32
51
btn.begin ();
33
52
btn.setDebounceTime_ms (20 );
34
53
bArray[1 ].releaseOn ();
35
-
36
54
}
37
55
38
56
// ////////////////////////////////////////////////////////////////////////////
@@ -45,12 +63,12 @@ void loop() {
45
63
46
64
switch (bArray[0 ].tick ()) {
47
65
case ButtonState::shortPressed:
48
- Serial.print (" B1 . " );
66
+ Serial.print (" B1 (short) " );
49
67
Serial.print (bArray[0 ].getDuration_ms ());
50
68
Serial.println (" ms" );
51
69
break ;
52
70
case ButtonState::longPressed:
53
- Serial.print (" B1 + " );
71
+ Serial.print (" B1 (long) " );
54
72
Serial.print (bArray[0 ].getDuration_ms ());
55
73
Serial.println (" ms" );
56
74
break ;
@@ -59,16 +77,27 @@ void loop() {
59
77
60
78
switch (bArray[1 ].tick ()) {
61
79
case ButtonState::shortPressed:
62
- Serial.print (" B2 . " );
80
+ wait .start ();
81
+ digitalWrite (led_short, HIGH);
82
+ Serial.print (" B2 (short) " );
63
83
Serial.print (bArray[1 ].getDuration_ms ());
64
84
Serial.println (" ms" );
65
85
break ;
66
- case ButtonState::longPressed:
67
- Serial.print (" B2 + " );
86
+ case ButtonState::longPressed:
87
+ Serial.print (" B2 (long) " );
68
88
Serial.print (bArray[1 ].getDuration_ms ());
69
89
Serial.println (" ms" );
70
90
Serial.println (" Auto released" );
71
91
break ;
72
- default : break ;
92
+ case ButtonState::pressed:
93
+ if (wait (blink_duration)) {
94
+ digitalWrite (led_long, !digitalRead (led_long));
95
+ wait .start ();
96
+ }
97
+ break ;
98
+ default :
99
+ if (digitalRead (led_short) && wait (duration)) { digitalWrite (led_short, LOW); }
100
+ if (digitalRead (led_long)) { digitalWrite (led_long, LOW); }
101
+ break ;
73
102
}
74
103
}
0 commit comments