Something like
//https://forum.arduino.cc/t/arduino-loop-with-buzzer/868057/4
// based on RepeatingMillisDelay.ino
// download SafeString V4.1.5+ library from the Arduino Library manager or from
// https://www.forward.com.au/pfod/ArduinoProgramming/SafeString/index.html
// for the millisDelay class
#include <millisDelay.h>
int buzzerPin = 13; // or buzzer
// Pin 13 has an LED connected on most Arduino boards.
bool buzzerOn = false; // keep track of the buzzerPin state
millisDelay buzzerDelay;
unsigned long BUZZER_DELAY_MS = 125;
millisDelay countDelay;
unsigned long COUNT_DELAY_MS = 500;
void setup() {
Serial.begin(9600);
for (int i = 10; i > 0; i--) {
Serial.print(' '); Serial.print(i);
delay(500);
}
Serial.println();
// initialize the digital pin as an output.
pinMode(buzzerPin, OUTPUT); // initialize the digital pin as an output.
digitalWrite(buzzerPin, LOW); // turn buzzerPin off
buzzerOn = false;
// start delay
countDelay.start(COUNT_DELAY_MS);
}
int buzzerCounter = 21; // > 20 so buzzer not running
void runBuzzer() {
if (buzzerCounter >= 20) {
buzzerDelay.stop();
return; // stop buzzer
} // else run buzzer
// check if delay has timed out
if (buzzerDelay.justFinished()) {
buzzerDelay.restart(); //
buzzerCounter++;
// toggle the buzzerPin
buzzerOn = !buzzerOn;
if (buzzerOn) {
digitalWrite(buzzerPin, HIGH); // turn buzzerPin on
} else {
digitalWrite(buzzerPin, LOW); // turn buzzerPin off
}
}
}
int count = 0;
int limit = 10;
void checkStartBuzzer() {
if (countDelay.justFinished()) {
countDelay.repeat();
count++;
if (count > limit) {
Serial.println(F("start buzzer"));
buzzerCounter = 0; // start buzzer
buzzerDelay.start(BUZZER_DELAY_MS);
count = 0; // repeat in 5sec
}
}
}
void loop() {
checkStartBuzzer();
runBuzzer();
}