Button: Debounce & Interrupt
2025-09-20 路 ~1 min read
Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
| #include <Arduino.h>
const int BTN = 0,
LED = 2;
volatile bool pressed = false;
void IRAM_ATTR onPress() {
pressed = true;
}
void setup() {
pinMode(LED, OUTPUT);
pinMode(BTN, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(BTN), onPress, FALLING);
}
void loop() {
static unsigned long last = 0;
if (pressed && millis() - last > 50) {
last = millis();
pressed = false;
digitalWrite(LED, !digitalRead(LED));
}
}
|
馃敄
#gpio
#interrupt
#debounce