/*
Arduino Forum
Topics: Delay() in main loop preventing ISR button detection
Sub-Category: Programming Questions
Category: Using Arduino
Link: https://forum.arduino.cc/t/delay-in-main-loop-preventing-isr-button-detection/1148422/14
*/
#define buttonPin 2
#define ledPin 5
bool reset;
void setup() {
pinMode(buttonPin, INPUT_PULLUP); // Button Pin Mode
pinMode(ledPin, OUTPUT); // LED Pin Mode
pinMode(LED_BUILTIN, OUTPUT); // LED_BUILTIN Pin Mode
attachInterrupt(digitalPinToInterrupt(buttonPin), ISR_BUTTON, FALLING); // Attached Interrupt with edge mode
}
void loop() {
digitalWrite(LED_BUILTIN, HIGH); // Turn LED_BUILTIN On
digitalWrite(ledPin, HIGH); // Turn LED On
delay(1000); // Delay 1000 ms
digitalWrite(LED_BUILTIN, LOW); // Turn LED_BUILTIN Off
digitalWrite(ledPin, LOW); // Turn LED Off
delay(1000); // Delay 1000 ms
}
void yield() {
if (reset) { // Check Reset state
digitalWrite(ledPin, LOW); // Turn LED Off
reset = false; // Clear reset state
}
}
void ISR_BUTTON() {
reset = true; // Set reset state
}