Go Down

Topic: Problem with the function tone() (Read 43 times) previous topic - next topic

Samuele97

I'm imitating this code https://www.arduino.cc/en/Tutorial/toneMelody, but i don't understand why there is the need to introduce a delay between notes.
If i don't set a delay and the first one note lasts 5 second and the second one 2 second they don't last 7 second but they last very little time and you don't hear two notes clearly.
I use delay if i want increas the thime between notes but...
If i increase the delay rightly the time between two notes increase but if i reduce the delay, also the time of the note reduces. Why?
Thank you, in advance.

PaulS

Quote
Why?
It's your code, that you didn't post, that is making it happen. If you want to ask questions about your code, you must show YOUR code.

Samuele97

Code:
Code: [Select]

void loop() {
      for (int k = 0; k < 105; k++)
    {
      float noteDuration = 1000 / noteDurations[k];
      tone(6, melody[k], noteDuration);
      //float pauseBetweenNotes = noteDuration*1.3;
      //delay(pauseBetweenNotes);
      //if i comment these two instructions the melody is reproduced very fast and singles notes don't lasts as specified with noteDuration.
    }

If i comment these two instructions the melody is reproduced very fast and singles notes don't lasts as specified with noteDuration.

jurs

If i comment these two instructions the melody is reproduced very fast and singles notes don't lasts as specified with noteDuration.
The tone() function is non-blocking and plays in the background.

Other than with a blocking function
- you call the function
- tone is played while time goes by
- after all time has passed, tone goes off and function returns
the tone function is non-blocking and will
- start playing the tone in the background
- returns to caller immediately after you called the function
(tone is then played later as time goes by)

So if you call tone(), tone(), tone(), tone(), the functions will execute within a few microseconds and only the last tone() is actually played.

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy