// https://forum.arduino.cc/t/arduino-buzzer-millis-question/903934/1
// original code had a ///// important line, but that didna fix it
// constants won't change. Used here to set a pin number:
const int buzzerPin = A2;// the number of the LED pin WAS LEDPin that is 13 for UNO
// Variables will change:
int buzzerState = LOW; // ledState used to set the LED
// Generally, you should use "unsigned long" for variables that hold time
// The value will quickly become too large for an int to store
unsigned long previousMillis_1 = 0; // will store last time LED was updated
unsigned long previousMillis_2 = 0; // will store last time LED was updated
// constants won't change:
const long interval_1 = 221; // interval at which to blink (milliseconds)
const long interval_2 = 900;
void setup() {
// set the digital pin as output:
pinMode(buzzerPin, OUTPUT);
digitalWrite(buzzerPin, LOW);
}
void loop() {
// here is where you'd put code that needs to be running all the time.
// check to see if it's time to blink the LED; that is, if the difference
// between the current time and last time you blinked the LED is bigger than
// the interval at which you want to blink the LED.
unsigned long currentMillis = millis();
if (currentMillis - previousMillis_1 >= interval_1) {
// save the last time you blinked the LED
previousMillis_1 = currentMillis;
digitalWrite(buzzerPin, LOW);
}
if (currentMillis - previousMillis_2 >= interval_2) {
// save the last time you blinked the LED
previousMillis_2 = currentMillis;
// comment out the following line to see the bad behaviour
previousMillis_1 = currentMillis; // reschedule the turn off time!
digitalWrite(buzzerPin, HIGH);
}
}