lbernstone/Tone32

Tone function not working in Timer interrupt

Autobot86 opened this issue · 1 comments

I try to simple code code for pizzo buzzer working in esp32 timer interrupt but this code not working please help me

#include<Tone32.h>
boolean Timer_Flag = false;
const int Buzzer = 25;

hw_timer_t * timer1 = NULL;

void IRAM_ATTR onTimer1(void) {
Timer_Flag = !Timer_Flag;
if(Timer_Flag)
tone(Buzzer, 5000, 800,0);
else
noTone(Buzzer);

}

void user_init(void) {
/* Use 1st timer of 4 /
/
1 tick take 1/(80MHZ/80) = 1us so we set divider 80 and count up */

timer1 = timerBegin(0, 80, true);
/* Attach onTimer function to our timer */

timerAttachInterrupt(timer1, &onTimer1, true);
/* Set alarm to call onTimer function every second 1 tick is 1us
=> 1 second is 1000000us /
/
Repeat the alarm (third parameter) */

timerAlarmWrite(timer1, 100000 , true);

timerAlarmEnable(timer1);
}
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
pinMode(Buzzer, OUTPUT);
user_init();

}
void loop() {

}

Don't directly interact with hardware peripherals in an interrupt. Set a semaphore and deal with it in the loop, or else use the Ticker library (which does that for you).