Agree @gcjr there is something not quite right about the code with only the small change I suggested.
It does blink (LED) or beep, that is, but it is oddly flawed and seems to need the on and off times to be related, I got a headache trying to figure out or fix it. Life too short. Mine anyway.
And the code in #4 above works perfectly, but uses delay() to time the ON interval, which kinda defeats half the purpose. You might as well go back to timing both the ON and OFF periods using delay.
So take your finger for a stroll through this and see it it lights up either half or even both halves of your brain - this tracks the state of the controlled device, I used a variable that was in your first post.
const int buzzerPin = A2;
int buzzerState = LOW; // ledState used to set the LED
unsigned long previousMillis_1 = 0; // will store last time LED was updated
unsigned long previousMillis_2 = 0; // will store last time LED was updated
const long interval_1 = 337; // ON time
const long interval_2 = 1000 - 337; // OFF time
void setup() {
pinMode(buzzerPin, OUTPUT);
digitalWrite(buzzerPin, LOW);
}
void loop() {
// here is where you'd put code that needs to be running all the time.
//
// and this checks if it is time to change the buzzer
unsigned long currentMillis = millis();
if (buzzerState == HIGH) {
if (currentMillis - previousMillis_1 >= interval_1) {
digitalWrite(buzzerPin, LOW);
previousMillis_2 = currentMillis;
buzzerState = LOW;
}
}
else { // buzzerState == LOW
if (currentMillis - previousMillis_2 >= interval_2) {
digitalWrite(buzzerPin, HIGH);
previousMillis_1 = currentMillis;
buzzerState = HIGH;
}
}
}
Or see it for yourself here:
HTH
a7