tone() command using passive buzzer

Hi guys,

I might have a simple and stupid problem, but I can't solve it. Could somebody explain me why this happens? I just want to make the Arduino play a song through the passive buzzer. But when I execute the following code:

void loop() {
tone(8,melody[1],500);
delay 1000;
};

I get this error:
error: expected ';' before numeric constant

The IDE somehow needs an ';' before the delay numeric.

Everything else is fine, I can execute the code without delay. But everytime I try to add a delay in order to add another follow-up tone, it doesn't work.

Hope somebody can help me.

Cheers :slight_smile:

  delay 1000;

(Are) (you) (missing) (something)?

The compiler thinks you are.

Do these two lines look like they are correct? :wink:

delay 1000;
};

(Oh) (my) (gosh).

(SORRY)!

Can someone explain to me why the above code

void loop() {
tone(8,melody[1],500);
delay (1000);
};

takes 1 sec to be executed? Somehow the two timespans start at the same time while I would rather have them to be processed one after another. So first 0.5 secs of tone and then 1 sec delay. Why isn't this the case? :confused:

Cheers!
fonzane

Can someone explain to me why the above code

What do you think the delay() function does? RTFM if you don't know.

If you provide no duration to tone() it plays forever until you send a noTone(). So how likely is it that tone() is a blocking command that stops execution until it completes?

Steve

slipstick:
If you provide no duration to tone() it plays forever until you send a noTone(). So how likely is it that tone() is a blocking command that stops execution until it completes?

I believe it is unlikely. The following snippet, which lacks a duration argument, plays a variable frequency tone until cancelled elsewhere by notone().
Since a number of other things are happening to modulate the frequency I conclude that tone() is not a blocking function.

/*
   Generate tone of varying pitch based on ram distance reading.
   Pitch increases as ram nears the load position,
*/
void toneRamp() { // parmVal sets slope of frequency change
  if (rangeToRam_mm < textAndParms[menuSetRangeLoad].parmVal * 3) {
    tone(piezo, 500 - rangeToRam_mm * textAndParms[menuBuzzer].parmVal);
  }
}

Since a number of other things are happening to modulate the frequency I conclude that tone() is not a blocking function.

Correct.

Yes I know that. I was rather hoping the OP would take the hint. Oh well, too subtle I guess.

Steve