Created a small project with a button and piezo buzzer. Push the button = play the buzzer tones.
The code works, however the buzzer tones loop twice before waiting for another button push. As far as I can tell, I only scripted them to play once before waiting for a button push. Can anyone advise what I've inadvertently done to make the playback double?
Cheers
int buzzer = 10;
int interval = 0;
int button = 9;
int buttonState;
int lastButtonState;
bool buttonMode = false;
unsigned long buttonStartTime = 0;
void setup () {
pinMode(buzzer, OUTPUT);
pinMode(button, INPUT);
}
void loop () {
int buttonState = digitalRead (button);
if (buttonState != lastButtonState ) {
lastButtonState = buttonState;
if (buttonState == LOW)
buttonStartTime = millis();
tone(buzzer, 550);
delay(700);
tone(buzzer, 440);
delay(1400);
noTone(buzzer);
delay(2000);
}
else
noTone(buzzer);
}