Why does this code loop twice?

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);


}

All your tone commands are indented as if they are controlled by the if above them but you have omitted the curly brackets that would actually make that the case.

C or C++ is not like python... indenting does not define blocks, {} braces do.

Ok, adding a brace to the end of this line did the trick:

if (buttonState == LOW) {

I'm not quite sure I understand how the addition of the brace is making the tones place once rather than twice.

Thisisliam:
Ok, adding a brace to the end of this line did the trick:

if (buttonState == LOW) {

I'm not quite sure I understand how the addition of the brace is making the tones place once rather than twice.

Then learn how to program in C / C++.

Without the braces, your tone stuff runs whenever buttonstate != lastButtonState. So when you press the button and get LOW, it runs. When you release the button it will be HIGH and so it runs again.

Thisisliam:
I'm not quite sure I understand how the addition of the brace is making the tones place once rather than twice.

in C or C++ your code is a list of statements that are executed. there are various types of statements

and simple ones are

  1. expression statements;
  2. compound statements;
  3. selection statements;

an expression statement is a simple operation like [color=blue]x = 3;[/color]. it's an expression followed by a semicolon.

a simple if is a selection statement and it looks like this

[color=blue]if ( condition ) statement[/color]

you see the if executes only 1 statement

if you want to execute multiple things if the condition is true, you need to group your stuff into a compound statement which is a list of statements within brackets

[color=blue]{ 
  statement1
  statement2
  statement3
  ...
}[/color]

so when you write

if (buttonState == LOW)
        buttonStartTime = millis();
        tone(buzzer, 550);
        delay(700);
        tone(buzzer, 440);
        delay(1400);
        noTone(buzzer);
        delay(2000);

the compiler actually sees only one expression statement in the true condition and will compile this way: an if statementif (buttonState == LOW) buttonStartTime = millis();followed by other statements that are always executed

tone(buzzer, 550);
delay(700);
tone(buzzer, 440);
delay(1400);
noTone(buzzer);
delay(2000);

if you group everything in brackets, then everything becomes part of a compound statement executed when the condition is true

if (buttonState == LOW) {
  buttonStartTime = millis();
  tone(buzzer, 550);
  delay(700);
  tone(buzzer, 440);
  delay(1400);
  noTone(buzzer);
  delay(2000);
}

--> read a C tutorial... if you don't get the syntax and grammar, it will be very frustrating to code...

Thanks guys. Much appreciated.

@ieee488 - I'm a beginner and still learning, among other things such as working and putting myself through flight school. These forums are one of many tools to learn this stuff and I can only go at a certain pace. I don't appreciate your passive aggressive comment. If you can't say anything constructive I ask that you don't contribute. I'm well aware this requires understanding of C/C++.

Thisisliam:
I don't appreciate your passive aggressive comment. If you can't say anything constructive I ask that you don't contribute.

that goes both ways... if you don't like an answer, just ignore it..

Get used to direct answers and build up a filter, don't take comments personally - they are not a value judgment in most cases, just plain simple facts.

The point and advice - even if stated a bit bluntly — was right, understanding [color=blue]{}[/color] is a fundamental part of the language.

I had a bit more time, so typed in an answer... but does not mean you should not read a C tutorial :slight_smile:

The reason it happens exactly twice: Since the buzzer code is not inside the "if (buttonState == LOW)" it is controlled only by the "if (buttonState != lastButtonState)". That means it gets executed when the buttonState goes from HIGH to LOW and again when it goes from LOW to HIGH. If you hold the button for a while you will find that the second round of buzzes happens when you release the button.