@@ -5,36 +5,58 @@ Features: WiFi, BT, Dual Core, 240MHz, VRef calibration in efuse, Coding Scheme
5
5
MAC: 24:0a:c4:c5:4e:c0
6
6
*/
7
7
8
+ #define LED_GPIO_PIN GPIO_NUM_23
9
+
10
+ bool led_state = 0 ;
11
+
12
+ void led_toggle ()
13
+ {
14
+ // set GPIO value to the opposite of the current value
15
+ digitalWrite (LED_GPIO_PIN, !digitalRead (LED_GPIO_PIN));
16
+ }
17
+
8
18
struct Button
9
19
{
10
20
const uint8_t PIN;
11
21
uint32_t numberKeyPresses;
12
22
bool pressed;
13
23
};
14
24
15
- Button button1 = {32 , 0 , false };
25
+ Button button_1 = {32 , 0 , false };
16
26
17
- void IRAM_ATTR isr ()
27
+ void IRAM_ATTR button_isr ()
18
28
{
19
- button1.numberKeyPresses += 1 ;
20
- button1.pressed = true ;
29
+ volatile static unsigned long last_interrupt_time = 0 ;
30
+ unsigned long interrupt_time = millis ();
31
+ // software debouncing
32
+ if (interrupt_time - last_interrupt_time > 200UL ) // ignores interupts for 200 milliseconds, UL stands for unsigned long
33
+ {
34
+ last_interrupt_time = interrupt_time;
35
+ button_1.numberKeyPresses += 1 ;
36
+ button_1.pressed = true ;
37
+ led_toggle ();
38
+ }
21
39
}
22
40
23
41
void setup ()
24
42
{
25
43
Serial.begin (115200 );
26
- Serial.printf (" Setting up parameters.\n " );
27
- Serial.printf (" Button PIN: %u\n " , button1.PIN );
28
- pinMode (button1.PIN , INPUT_PULLUP);
29
- attachInterrupt (button1.PIN , isr, FALLING);
44
+ Serial.printf (" Setting up ....\n " );
45
+ Serial.printf (" Button PIN: %u\n " , button_1.PIN );
46
+ pinMode (button_1.PIN , INPUT_PULLUP);
47
+ attachInterrupt (button_1.PIN , button_isr, FALLING);
48
+ // init LED GPIO
49
+ Serial.printf (" LED PIN: %u\n " , LED_GPIO_PIN);
50
+ pinMode (LED_GPIO_PIN, OUTPUT);
51
+ digitalWrite (LED_GPIO_PIN, 0 ); // instead of 0 you can also use LOW
30
52
}
31
53
32
54
void loop ()
33
55
{
34
- if (button1 .pressed )
56
+ if (button_1 .pressed )
35
57
{
36
- Serial.printf (" Button 1 has been pressed %u times\n " , button1 .numberKeyPresses );
37
- button1 .pressed = false ;
58
+ Serial.printf (" Button 1 has been pressed %u times\n " , button_1 .numberKeyPresses );
59
+ button_1 .pressed = false ;
38
60
}
39
61
40
62
// Detach Interrupt after 1 Minute
@@ -43,7 +65,12 @@ void loop()
43
65
if (millis () - lastMillis > 60000 )
44
66
{
45
67
lastMillis = millis ();
46
- detachInterrupt (button1 .PIN );
68
+ detachInterrupt (button_1 .PIN );
47
69
Serial.println (" Interrupt Detached!" );
48
70
}
71
+
72
+ // the following statements are used to show that loop is still working
73
+ // when button is pressed and hold, remove if you want to check software debouncing
74
+ Serial.printf (" void loop() is still doing something!\n " );
75
+ delay (2000 );
49
76
}
0 commit comments