Arduino_Buzzer_Millis question

kinda awkward

this is pretty minimal, yet clear

const int buzzerPin =  13;

enum { Off = HIGH, On = LOW };

#define Period   1000
#define OnTime   20

unsigned long msecTime = Period;

void setup  () {
    Serial.begin (9600);

    digitalWrite  (buzzerPin, Off);
    pinMode       (buzzerPin, OUTPUT);
}

void loop  () {
    unsigned long msec = millis  ();

    if (msec > msecTime)  {
        byte state = digitalRead (buzzerPin);
        digitalWrite (buzzerPin, ! state);

        if (Off == state)
            msecTime = msec + OnTime;
        else
            msecTime = msec + Period;
    }
}
1 Like