Step 3: The Music
The
intent of this Instructable is not to reinvent the wheel, but to
improve it. The code representing musical composition for the Arduino
has been presented many times before with minor variations. Notes,
rests, and note values (note duration) are assigned to variables that
Arduino interprets and outputs as tones of proper frequency and
duration. This is essentially Arduino “MIDI” music.
Check out the
site, readsheetmusic.info/readingmusic.shtml, for a beginner's guide to
reading music if needed. I will cover a few concepts that may help when
entering music in the sketches. (Those of you with a musical background
can skip this paragraph). Musical sketches written for Arduino are
usually based on piano key frequencies. The keys on a piano are divided
into octaves, beginning with a C note and ending with the next C note in
succession (C,D,E,F,G,A,B,nextC). The piano keyboard contains 7
complete octaves, numbered 1 to 7 from left (lowest C) to right (highest
C). Notes are followed by the number of the octave in which they are
located, so C4 would be the beginning note in the fourth octave. C4 is
also called middle C since the fourth octave is also the middle octave
on the keyboard. One last point – the same note doubles in frequency
every time it moves up an octave. For example, A3 has a frequency of 220
Hz, A4 has a frequency of 440 Hz, and A5 a frequency of 880 Hz.
Notes
The following characters represent the 3 octaves of notes available for this project:
c3,
c#3, d3 d#3, e3, f3, f#3, g3, g#3, a3, a#3, b3, c, c#, d, d#, e, f, f#,
g, g#, a, a#, b, c5, c#5, d5, d#5, e5, f5, f#5, g5, g#5, a5, a#5, b5,
and c6.
These characters are entered into the programs exactly as
they are written here. I’ve used lower case letters and dropped the 4
from the notes of the middle octave to simplify data entry (I hope).
There are no flat notes because every flat note is the same as the sharp
of the lower note next to it. So if a melody has a B-flat, use the
equivalent a# instead.
Note Values (note duration)
The following note values are available in the sketches:
1, 1., 2, 2., 4, 4., 8, 8., and 16.
1
represents a whole note, 2 a half note, 4 a quarter note, 8 an eighth
note, and 16 a sixteenth note.The quarter note is the default for this
project because a majority of music is written in quarter time (3/4,
4/4, etc.), with a quarter note getting one beat. I set the duration of
one beat to 400 milliseconds because it sounded about right to me. This
can be changed by changing the value of the #define beat statement at
the beginning of each sketch. A quarter note will last 400 msec, a half
note 800 msec, an eighth note 200 msec. There are also dotted notes
available (1., 2., 4., and 8.) A dotted note has a note value one and a
half times that of the note. For example, a dotted quarter note is held
for a beat and a half, or in our case, 600 msec. Note values are entered
before the note whose value is to be changed.
Ex:
a<>g<>a<>g<>2<>c5<>4<>a<>g<>a<>g<>
(<> = ENTER)
In this example, the note sequence is
entered from the serial monitor using the ArduinoMusic sketch. The first
4 notes are quarter notes (the default), entering 2 changes c5 to a
half note, and 4 changes the last 4 notes back to quarter notes.
Rests
The sketches contain the following rest values:
r1, r2, r4, r4., r8, and r16.
Rests
are periods of silence in a musical score, and are analogous to note
values - r1 is a whole rest, r2 a half rest, and so forth. Entering r4
would result in one beat of silence; entering r16 would produce ¼ beat
of silence. There is one dotted rest (r4.).
Play the Morse code
file which uses a combination of note values and rests. The code spells
out the time-honored programmer's phrase, "HELLO WORLD".
Step 4: The Sketches
ArduinoMusic Sketch
The
ArduinoMusic sketch enables you to enter and play music using Arduino’s
serial monitor. Compile and upload the sketch, and open the serial
monitor. The following message will print, “Set serial monitor to
[Newline] and enter musical composition characters, one at a time.”.
Enter the music, one character at a time and press ENTER after typing
each character. The note buffer can hold up to 120 characters. Type
“play” and ENTER to play the melody after all characters have been
entered. You can play the music as many times as you like until the note
buffer is cleared. Press the Arduino reset button or close the serial
monitor to purge the note buffer.
So let's try an example. We'll
enter and play the top staff (double set of 5 lines containing the
notes) in the figure above. The staff contains 5 measures (time periods)
of notes which are delineated by vertical lines.The first note in the
music is a half note G4, so the first two characters to enter are 2
(change to half note) and g (note G4). Keep going till everything is
entered.
The full character sequence looks like this:
2<>g<>4.<>g<>8<>a<>4<>b<>g<>b<>d5<>2<>g5<>4.<>b5<>8<>a5<>4<>g5<>d5<>b<>g<>2<>c5<>4<>e5<>c5<>play<>
(<> = ENTER)
Play the
Scotland file to hear the result.
SD_Card_Music Sketch
The
serial monitor is great for developing compositions or entering simple
melodies on the fly, but it has its limitations. It is first and
foremost a code debugging tool – it has no text editing or data storage
capabilities. If a wrong note is entered, there’s no way to take it
back, and there’s no way to store a melody once it’s entered using the
ArduinoMusic sketch. The SD_Card_Music sketch addresses these issues.
Using
this sketch is a two part process. The first step is to write some
music and store it in a file on an SD card. You can use any simple text
editor to do this. I use Notepad since I have a Windows based system. To
write a melody, type each character and follow it with a comma (no
spaces).
Ex: c,d,e,f,g,a,b,c5,
When finished composing, store each melody on the SD card as a plain text file with the extension,
.txt. File names need to be 8 letters or less.
Now
for step 2. Insert the SD card in its module and make sure the module
is correctly configured with the Arduino board. Compile and upload the
SD_Card_Music sketch, then open the serial monitor. If everything is
hooked up correctly, the serial monitor will print the message, "SD
Initialization done...". Type in the name of the file you want to play
and include the .txt extension (ex.
flute.txt). If the filename has been entered correctly the following will happen:
The
serial monitor will display the message, "filename.txt exists", the
music will play, the serial monitor will print each note as it's played,
and after the music has finished the monitor will display the number of
characters in the file.