int BUZZER_PIN = 10;
int BUZZER_CHANNEL = 0;
int BUTTON = 5;
bool WARNING = true;
unsigned long soundStartTime = 0;
void IRAM_ATTR TURN_OFF(){
WARNING = false;
}
void setup() {
ledcSetup(0, 5000, 8);
pinMode(BUTTON, INPUT_PULLUP);
ledcAttachPin(BUZZER_PIN, BUZZER_CHANNEL);
attachInterrupt(BUTTON, TURN_OFF, FALLING);
}
void loop() {
if (WARNING == true) {
ledcWriteNote(BUZZER_CHANNEL, (note_t)NOTE_D, 8);
soundStartTime = millis(); // Lưu thời gian bắt đầu âm thanh
// Tắt âm thanh sau 5 giây
while (millis() - soundStartTime < 5000) {
if(WARNING == false) break;
}
// Tắt âm thanh
ledcWrite(0, 0);
delay(10);
WARNING = false; // Đặt lại trạng thái để có thể kêu lần tiếp theo
} else {
ledcWrite(0, 0);
delay(10);
}
}