Code: Select all
byte buzzerPin = 12;
hw_timer_t * toneTimer = NULL;
portMUX_TYPE timerMux = portMUX_INITIALIZER_UNLOCKED;
/*----------------------------------------------------------------------*
toneOff() ISR
----------------------------------------------------------------------*/
void IRAM_ATTR toneOff() {
timerAlarmDisable(toneTimer);
ledcWriteTone(4, 0);
Serial.println("OFF");
}
void setup() {
Serial.begin(115200);
while (!Serial);
toneTimer = timerBegin(1, 80, true); // initialize timer - timer_id = 1; divider=80; countUp = true;
timerAttachInterrupt(toneTimer, &toneOff, true); // edge = true
ledcAttachPin(buzzerPin, 4);
ledcSetup(4, 2000, 8);
}
void loop() {
toneOn(50, 40, 1000); // for testing
delay(2000);
}
void toneOn(byte volume, int dur, int frequency) {
ledcWriteTone(4, frequency);
ledcWrite(4, volume);
timerAlarmWrite(toneTimer, dur * 1000, true); // 1000 per ms
timerAlarmEnable(toneTimer);
Serial.println("ON");
}