Skip to content

Commit cb06f35

Browse files
committed
several updates
1 parent 2c5bcc6 commit cb06f35

File tree

3 files changed

+39
-12
lines changed

3 files changed

+39
-12
lines changed

esp32_interrupts/esp32_interrupts.ino

+39-12
Original file line numberDiff line numberDiff line change
@@ -5,36 +5,58 @@ Features: WiFi, BT, Dual Core, 240MHz, VRef calibration in efuse, Coding Scheme
55
MAC: 24:0a:c4:c5:4e:c0
66
*/
77

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+
818
struct Button
919
{
1020
const uint8_t PIN;
1121
uint32_t numberKeyPresses;
1222
bool pressed;
1323
};
1424

15-
Button button1 = {32, 0, false};
25+
Button button_1 = {32, 0, false};
1626

17-
void IRAM_ATTR isr()
27+
void IRAM_ATTR button_isr()
1828
{
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+
}
2139
}
2240

2341
void setup()
2442
{
2543
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
3052
}
3153

3254
void loop()
3355
{
34-
if (button1.pressed)
56+
if (button_1.pressed)
3557
{
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;
3860
}
3961

4062
// Detach Interrupt after 1 Minute
@@ -43,7 +65,12 @@ void loop()
4365
if (millis() - lastMillis > 60000)
4466
{
4567
lastMillis = millis();
46-
detachInterrupt(button1.PIN);
68+
detachInterrupt(button_1.PIN);
4769
Serial.println("Interrupt Detached!");
4870
}
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);
4976
}
33.6 KB
Binary file not shown.
Loading

0 commit comments

Comments
 (0)