Use Simple Timer to turn a buzzer on and off

PieterP:
What if the sensor goes low while it's still beeping? (e.g. after two beeps) Should it stop beeping immediately, or should it finish the last two beeps first?

If you want it to stop immediately, you could add an "off" method to the buzzer class, that clears the buffer and turns off the buzzer:

    void off() {

digitalWrite(pin, LOW); // disable the buzzer
      offDelay = 0;          // no delay before executing the next beep
      beepsInQueue = 0;      // no more beeps in the queue
      index = writeIndex;    // start reading beeps from the index where a new one will be written
      beeping = false;        // remember that the buzzer is off
    }



What kind of sensor are we talking about? Does it need software debouncing?

Pieter

The sensor is just a float sensor with a reed switch inside. It has a pull-up so the arduino sees High and Low. Actually in my final project I will be using a switch case to turn the buzzer on. Example when mode is 2, activate the buzzer.