/*
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 the button and LED pin numbers
#define buttonPin 2
#define ledPin 5
bool reset; // Flag to indicate if a reset is needed
void setup() {
pinMode(buttonPin, INPUT_PULLUP); // Set button pin as input with internal pull-up resistor
pinMode(ledPin, OUTPUT); // Set LED pin as output
pinMode(LED_BUILTIN, OUTPUT); // Set built-in LED pin as output
attachInterrupt(digitalPinToInterrupt(buttonPin), ISR_BUTTON, FALLING); // Attach interrupt for button press
}
void loop() {
digitalWrite(LED_BUILTIN, HIGH); // Turn built-in LED on
digitalWrite(ledPin, HIGH); // Turn LED on
delay(1000); // Delay 1000 ms
digitalWrite(LED_BUILTIN, LOW); // Turn built-in LED off
digitalWrite(ledPin, LOW); // Turn LED off
delay(1000); // Delay 1000 ms
}
void yield() {
if (reset) { // Check if a reset is needed
digitalWrite(ledPin, LOW); // Turn LED off
reset = false; // Clear the reset flag
}
}
void ISR_BUTTON() {
reset = true; // Set the flag to indicate that a reset is needed when the button is pressed
}