#define LED 12
#define BTN 4
// ms delay to avoid bouncing problems
#define DEBOUNCE_DELAY 50
// last time the button has been triggered
volatile int last_millis = 0;
// interrupt function
void IRAM_ATTR trigger_led() {
if(millis() - last_millis > DEBOUNCE_DELAY)
digitalWrite(LED, !digitalRead(BTN));
last_millis = millis();
}
void setup() {
pinMode(LED, OUTPUT);
pinMode(BTN, INPUT_PULLUP);
attachInterrupt(BTN, trigger_led, RISING);
Serial.begin(115200);
}
void loop() {
// the loop part can be empty
// because all the logic is "event-based" thanks
// to the interrupt
}