const byte playPin = 7;
const byte stopPin = 8;
const byte buzzerPin = 4;
enum state {SILENT, BEEPING_ON, BEEPING_OFF} state = SILENT;
unsigned long chrono;
const unsigned long soundOnDuration = 500; // ms
const unsigned long soundOffDuration = 500; // ms
void setup() {
pinMode(buzzerPin, OUTPUT);
pinMode(playPin, INPUT_PULLUP);
pinMode(stopPin, INPUT_PULLUP);
}
void loop() {
switch (state) {
case SILENT:
// we are silent, if we press the play button, then start playing the sound
if (digitalRead(playPin) == LOW) {
tone(buzzerPin, 3150); // start the sound
chrono = millis(); // note the start time
state = BEEPING_ON;
}
break;
case BEEPING_ON:
// the beeping is on, check if we need to stop
if (digitalRead(stopPin) == LOW) {
noTone(buzzerPin);
state = SILENT;
} else // check if ON duration has elapsed
if (millis() - chrono >= soundOnDuration) {
noTone(buzzerPin);
chrono = millis(); // note the start time
state = BEEPING_OFF;
}
break;
case BEEPING_OFF:
// the beeping is off check if we need to stop
if (digitalRead(stopPin) == LOW) {
noTone(buzzerPin);
state = SILENT;
} else // check if OFF duration has elapsed
if (millis() - chrono >= soundOffDuration) {
tone(buzzerPin, 3150); // start the sound again
chrono = millis(); // note the start time
state = BEEPING_ON;
}
break;
}
}