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.

user1584421
  • 1,349
  • 3
  • 19
  • 32
  • https://www.arduino.cc/en/Tutorial/BuiltInExamples/BlinkWithoutDelay – VE7JRO Jun 15 '22 at 19:13
  • @VE7JRO Thank you! But sorry but i dont understand. How can this be implemented in the `tone()` function? – user1584421 Jun 15 '22 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 Jun 15 '22 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. – user1584421 Jun 15 '22 at 19:18
  • I see. They made it non-blocking. That's not really what I expected from ArduinoLand. – timemage Jun 15 '22 at 19:19
  • Replace the `delay` with a `millis` timer. – VE7JRO Jun 15 '22 at 19:24
  • @VE7JRO, why do you point them to millis()? that is not a solution to what the question asks. btw: https://arduino.stackexchange.com/questions/17355/playing-melody-with-tone-without-using-delay/89697#89697 – Juraj Jun 15 '22 at 19:28
  • @Juraj This was it! Thank you so much! If you want, you can make a proper answer, so i can upvote and mark it as the selected answer. Once again, thanks a ton! – user1584421 Jun 15 '22 at 19:32
  • @Juraj because ditching the use of `delay` is the most important lesson an Arduino user can learn. – VE7JRO Jun 15 '22 at 19:33
  • @VE7JRO, OP is not there yet – Juraj Jun 15 '22 at 19:34
  • @Juraj - The blocking `delay` will be the next question :) – VE7JRO Jun 15 '22 at 19:39
  • I am not sure i understand what you suggest. I should replace `delay` with a `millis` timer to do the delay work? I thought `millis` only counts time. Does it also perform countdown until a specific time? – user1584421 Jun 15 '22 at 21:54
  • @VE7JRO, that question would be a duplicate of https://arduino.stackexchange.com/questions/17355/playing-melody-with-tone-without-using-delay/89697 – Juraj Jun 16 '22 at 05:13

1 Answers1

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

Juraj
  • 17,050
  • 4
  • 27
  • 43