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.