Referring to the figure above. The BLACK colour wire are connected to ground(GND) while the RED wire are connected to digital pin-9 as the signal tone.
Define the pitches value using for typical note.
int melody[ ]
Preset the melody for birthday song.
int noteDuration[ ]
Preset the delay value for each melody note playing.
int noteDuration = 1000/noteDuration[thisnote];
To calculate the note duration, take the 1 second and divided with the note type.
E.g: 1000/3 = 333
tone (9, melody[thisnote], noteDuration);
Tone(pin, frequency, duration).
Generates a square wave of the specified frequency (and 50% duty cycle) on a pin-9.
int pauseBetweenNotes = noteDuration + 50;
Delay between the notes by adding 50ms to the noteDuration.
Stops the generation of a square wave triggered by tone(). Has no effect if no tone is being generated.