ISR not being called

lorencc

Member
Been all over the Internet. No proposed solution worked.


#include <Arduino.h>
volatile bool led_state = false;
#define LED_PIN 13
///////////////////////////////////////////////////////////////////
void timerISR() {
led_state = !led_state; // Toggle the LED state
digitalWrite(LED_PIN, led_state); // Update the LED
}
///////////////////////////////////////////////////////////////////
void setup() {
Serial.begin(115200);
while (!Serial);
// Initialize the LED pin as an output
pinMode(LED_PIN, OUTPUT);
digitalWrite(LED_PIN, LOW); // Start with LED off
// Create an IntervalTimer object
Serial.println("starting timer");
IntervalTimer myTimer;
// Start the timer to call timerISR every 1 second (1,000,000 microseconds)
myTimer.begin(timerISR, 1000000);
Serial.println ("timer start");
// Optional: Set interrupt priority (0-255, lower is higher priority)
myTimer.priority(128); // Default priority
}
/////////////////////////////////////////////////////////////////
void loop () {
delay(400);
Serial.printf("led state %d\n", led_state);
}
 
This:
Code:
IntervalTimer myTimer;
Should be at the top of your file. It is a declaration, not an executable statement.
Declaring myTimer within the setup() function means that the myTimer instance will disappear when the setup function ends.

Pete
 
Back
Top