How to convert sheet music into an Arduino Sketch - nseidle/AxelF_DoorBell GitHub Wiki

Here's how I converted the sheet music to tones played through a piezo buzzer or speaker on an Arduino. First, open up the toneMelody sketch under Examples->Digital under the Arduino IDE.

Axel F Sheet Music

Next, find the music you want to play. Keep it simple. You can only play one note at a time.

We need two things to make the sketch play this music: tone frequency and length of time to play the tone.

Note names

I know almost nothing about piano or music notes so I had to use the above note names figure out the type of each note. Here's a gotcha that took me awhile. The scale starts at C so it goes C4 D4 E4 F4 G4 A4 B4 C5 D5... See what happened there? A4/B4 come after C4. That's important.

I put dashes in place of no notes:

D4   -   F4    D4     -       D4     G4  D4   C4

Above is the the first measure.

Note Lengths

Next I had to figure out how long each note was. Then I translated the note lengths into a value.

D4   -   F4    D4     -       D4     G4  D4   C4
.5   .5  .75    .25    .25    .25    .5  .5   .5

The decimals should equal 4 if you add them all up. .5 is a fraction of a 4 beat measure so I then converted the decimals into note lengths (8th, 16th, etc):

D4   -   F4    D4     -       D4     G4  D4   C4
.5   .5  .75    .25    .25    .25    .5  .5   .5
8    8   6     16     16      16     8   8    8

Now combine these two things and write your arrays in your sketch:

int line1[] = {
  NOTE_D4, 0, NOTE_F4, NOTE_D4, 0, NOTE_D4, NOTE_G4, NOTE_D4, NOTE_C4,
  NOTE_D4, 0, NOTE_A4, NOTE_D4, 0, NOTE_D4, NOTE_AS4, NOTE_A4, NOTE_F4,
  NOTE_D4, NOTE_A4, NOTE_D5, NOTE_D4, NOTE_C4, 0, NOTE_C4, NOTE_A3, NOTE_E4, NOTE_D4,
  0};

int line1_durations[] = {
  8, 8, 6, 16, 16, 16, 8, 8, 8, 
  8, 8, 6, 16, 16, 16, 8, 8, 8,
  8, 8, 8, 16, 16, 16, 16, 8, 8, 2,
  2};

The helper header file called pitches.h contains all the frequencies for various notes.

Watch out for sharps and flats in the sheet music. If you miss them the melody will sound really off. In the case of Axel F there's a flat on B4. pitches.h doesn't have a B Flat but A sharp is the same thing so we use it instead.