Arduino_Buzzer_Millis question

doesn't "prevoiusMillis_1" need to be set to 20 msec after previousMillis_2 expires?

what about

#undef MyHW
#ifdef MyHW
const int buzzerPin =  10;
#else
const int buzzerPin =  A2;// the number of the LED pin WAS LEDPin that is 13 for UNO
#endif

// 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
// constants won't change:
const long interval_1 = 20;           // interval at which to blink (milliseconds)
const long interval_2 = 1000;

void setup() {
    // set the digital pin as output:
    pinMode(buzzerPin, OUTPUT);
    digitalWrite(buzzerPin, LOW);
}
void loop() {
    unsigned long currentMillis = millis();
    if (currentMillis - previousMillis_1 >= interval_2) {
        // save the last time you blinked the LED
        previousMillis_1 = currentMillis;

        digitalWrite(buzzerPin, LOW);
        delay (interval_1);
        digitalWrite(buzzerPin, HIGH);
    }
}
1 Like