Making A Melody

I further investigated making melodies on the Arduino and found this tutorial:

This was helpful because it gives a library of note names for each frequency value in tone and also shows a way to make divisions of the beat per each value in the array. And also, you just make the file yourself and reference it using pitch.h instead of a library. This was a helpful feature to learn. I modified the code to play the first few notes of Claire De Lune. The code for the patch is below and the link to the pitch.h file is in the tutorial above.


  Plays a melody

  - 8 ohm speaker on digital pin 8

  created 21 Jan 2010
  modified 30 Aug 2011
  by Tom Igoe

  This example code is in the public domain.

//you must make the pitches.h file yourself 
//which references the frequency values by note names
#include "pitches.h"

// notes in the melody:
int melody[] = {

// note durations: 4 = quarter note, 8 = eighth note, etc.:
int noteDurations[] = {
  4, 4, 1, 8, 8, 1,

void setup() {
  // iterate over the notes of the melody:
  for (int thisNote = 0; thisNote < 6; thisNote++) {

    // to calculate the note duration, take one second divided by the note type.
    //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
    int noteDuration = 1250 / 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.1;
    // stop the tone playing:

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