0

I tried to experiment with the tone() function that comes with the arduino library.

I played around with the standard code example located here: https://www.arduino.cc/en/Tutorial/BuiltInExamples/toneMelody

This is the code:

#include "pitches.h"

int melody[] =
{
  NOTE_C4, NOTE_G3, NOTE_G3, NOTE_A3, NOTE_G3, 0, NOTE_B3, NOTE_C4
};

int noteDurations[] =
{
  4, 8, 8, 4, 4, 4, 4, 4
};

void setup()
{
   for (int thisNote = 0; thisNote < 8; thisNote++)
   {
      int noteDuration = 1000 / noteDurations[thisNote];
      tone(8, melody[thisNote], noteDuration);
 
      // to distinguish the notes, set a minimum time between them.
      // the note's duration + 30% seems to work well:
      int pauseBetweenNotes = noteDuration * 1.30;
      delay(pauseBetweenNotes);

      // stop the tone playing:
      noTone(8);

   }
}

void loop()
{
  // no need to repeat the melody.
}

However, the problem is that there is always a pause between notes. In musical terms, this is called staccato. I want to be able to play short melodies where there are no delays between the notes, and one note progresses to the next naturally. By the way, this is called legato in music theory.

Now, in this example, the delay between the notes sounds good because this "music piece" is supposed to be played this way. But in every other song that i experimented with, there were always some pauses between notes.

This is what this song sounds like: https://soundcloud.com/nikowow-1/normal

Naturally, my mind went to the delay(pauseBetweenNotes) function. So i thought this is what causes the delay between the notes. So i set it up to 0.5. This is what it sounds like: https://soundcloud.com/nikowow-1/05a

As you can hear, it just speeds up the whole song. The delay between the notes remain, but the whole song is sped up (in a higher tempo, so to speak).

In order to better hear the delay between the notes, please take a listen at the song when i selected a value of 3 - causing a smaller tempo, so that the delay between the notes can be more easily audible: https://soundcloud.com/nikowow-1/3a-1

I want to create some melodies where there are no delays between the notes and i am stuck.

EDIT: If you remove the delay line alltogether, the song is played so quickly that only a click sound is heard.

13
  • @VE7JRO Thank you! But sorry but i dont understand. How can this be implemented in the tone() function? Commented Jun 15, 2022 at 19:15
  • I'm probably messing something. So, I'll just ask: What happens if you remove (or comment out) delay(pauseBetweenNotes); altogether?
    – timemage
    Commented Jun 15, 2022 at 19:16
  • @timemage Sorry i ommited this in the question and will edit it. But if you remove the delay, the song is played so quickly that only a feint click is heard. Commented Jun 15, 2022 at 19:18
  • I see. They made it non-blocking. That's not really what I expected from ArduinoLand.
    – timemage
    Commented Jun 15, 2022 at 19:19

1 Answer 1

3

there is

      // to distinguish the notes, set a minimum time between them.
      // the note's duration + 30% seems to work well:
      int pauseBetweenNotes = noteDuration * 1.30;
      delay(pauseBetweenNotes);

so the *1.30 creates the pause.

if you change it to

   int pauseBetweenNotes = noteDuration;

there will be no pause between notes.

The tone length is specified with the last parameter of the tone() function (noteDuration here).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.