#include <Arduino.h>
const int ledPin = 18; // GPIO pin where the LED is connected
const int buttonPin = 35; // GPIO pin where the button is connected
volatile bool ledOn = false; // Flag to track LED state
hw_timer_t * timer = NULL; // Declare a pointer to the timer object
// Timer interrupt service routine
void IRAM_ATTR onTimer() {
digitalWrite(ledPin, ledOn ? LOW : HIGH); // Toggle LED state
ledOn = !ledOn;
}
void timerSetup() {
// Create an instance of the timer
timer = timerBegin(0, 80, true); // Timer 0, prescaler 80, count up
// Attach the ISR function to the timer
timerAttachInterrupt(timer, &onTimer, true);
// Set the timer to trigger every 500 milliseconds (2000000 microseconds)
timerAlarmWrite(timer, 2000000, true);
// Start the timer
timerAlarmEnable(timer);
}
// Button interrupt service routine
void IRAM_ATTR buttonInterrupt() {
// Turn off the LED
digitalWrite(ledPin, LOW);
ledOn = false;
}
void setup() {
pinMode(ledPin, OUTPUT); // LED pin as output
pinMode(buttonPin, INPUT_PULLUP); // Button pin as input with pull-up resistor
attachInterrupt(digitalPinToInterrupt(buttonPin), buttonInterrupt, FALLING); // Attach interrupt on falling edge (button press)
// Start LED blinking
timerSetup();
}
void loop() {
// Other code in loop, if any
}
LED Is continous ON , it is not blinking, there are no errors in the code.
My version of your code below pulses the TX led on my board at a 1 sec interval.
I changed your TimerSetup function to comply with Ver 3.02 of the ESP core. I also used the TX led and not the NeoPixel which is mounted on my board (ESP32 S3). Those, I think, are the only changes I made. I did not connect and check the button part of your sketch.
#include <Arduino.h>
//const int ledPin = 18; // GPIO pin where the LED is connected
#define ledPin 43 // Pulsing the TX led
const int buttonPin = 35; // GPIO pin where the button is connected
volatile bool ledOn = false; // Flag to track LED state
hw_timer_t * timer = NULL; // Declare a pointer to the timer object
// Timer interrupt service routine
void IRAM_ATTR onTimer() {
digitalWrite(ledPin, ledOn ? LOW : HIGH); // Toggle LED state
ledOn = !ledOn;
}
void timerSetup() {
// Create an instance of the timer
// timer = timerBegin(0, 80, true); // Timer 0, prescaler 80, count up
// Set timer frequency to 1Mhz
timer = timerBegin(1000000);
// Attach the ISR function to the timer
// timerAttachInterrupt(timer, &onTimer, true);
// Attach onTimer function to our timer.
timerAttachInterrupt(timer, &onTimer);
// Set alarm to call onTimer function every second (value in microseconds).
// Repeat the alarm (third parameter) with unlimited count = 0 (fourth parameter).
timerAlarm(timer, 1000000, true, 0);
// Set the timer to trigger every 500 milliseconds (2000000 microseconds)
// timerAlarmWrite(timer, 2000000, true);
// Start the timer
// timerAlarmEnable(timer);
}
// Button interrupt service routine
void IRAM_ATTR buttonInterrupt() {
// Turn off the LED
digitalWrite(ledPin, LOW);
ledOn = false;
}
void setup() {
pinMode(ledPin, OUTPUT); // LED pin as output
pinMode(buttonPin, INPUT_PULLUP); // Button pin as input with pull-up resistor
attachInterrupt(digitalPinToInterrupt(buttonPin), buttonInterrupt, FALLING); // Attach interrupt on falling edge (button press)
// Start LED blinking
timerSetup();
}
void loop() {
// Other code in loop, if any
}