This is an Arduino project that uses the native tone generating ability of the Arduino to make music. You can use the sketch provided to start composing music of your own! Make it play 'Happy Birthday' for a friend. You'll see how to make music with your Arduino in the steps that follow.
For more information on Arduino's tone command see
http://arduino.cc/en/Tutorial/tone
The Arduino sketch provided here uses some advanced string methods - you will see them explained in the code comments. Here we acknowledge the help of our programming advisor, Andrew Wendt - thanks again.
Be an Arduino toneTune Composer: it will be fun.
This sketch will turn your Arduino into a mini music player.
See Step 5 How it Works for an explanation of the program.
Here is a demonstration of what you can do with the Arduino and toneTune. Take this as a starting point, and enter notes that play familiar melodies or something that you make up on your own.
You can get sound output from your Arduino in various ways. For the purposes of this project we will use one of these simple methods.
http://makezine.com/projects/make-34/monobox-powered-speaker-2/
There are various ways of representing musical notes in computer programs. Our goal was to devise or adopt a notation that would be the easiest for someone to use when entering a string of notes for the Arduino to play. We examined Ringtone Text Transfer Language (RTTTL), Music Macro Language (MML), Midi - and after some consideration developed our own, which you will see below. (If you think some modification would be more convenient for entering notes, please leave a comment with this Instructable.)
This project uses the following convention to represent a musical note.
3c#4.
Third octave, C sharp, quarter note, dotted. (Dotted notes play for 1.5 x the normal duration.)
r8
An eighth rest.
Octaves
The range of octaves is 1 to 7.
Accidentals
Sharps and flats are entered as in MML: either '#' or '+' represents a sharp, '-' represents a flat.
When you enter notes, it is not necessary to repeat the octave or duration if it has not changed from the previous note.
That is, you can enter 3c4 3d4 3g4 r4 for three quarter notes and a quarter rest in the third octave, or if you find it easier you could enter 3c4 d g r and get the same result.
The part of the sketch that contains the musical notes is located near the end of the sketch, in the setup() function. This is the part you will change to enter your own toneTune. Substitute your musical notes for the ones you find there and you will be composing Arduino music of your own.
The Arduino tone command produces a sound when it is given a sound frequency and a duration. Here is the Arduino reference page on the tone command:
http://arduino.cc/en/Reference/Tone
tone (pin, frequency, duration) produces a sound. In this project we have used digital pin 9 for the tone output. Duration is in milliseconds, so we have selected a standard duration of 1500 milliseconds to represent a whole note. You could change this if you prefer a different musical tempo.
How the sketch converts a string of musical notes in this format -"4f#2 g8 a16 " - to musical frequencies:
This section of the setup() function contains the musical notes and rests:
playMusic(String( // Tone Tune for Arduino
"5f4 3f8 e f "
"5f16 r e r f r "
"4f16 r e r f r8 "
));
(That is not all of the notes in the demonstration tune - it's just a sample of the notes in the first three lines of the toneTune tune.)
Comments on this part of the sketch:
A further example: these are the notes used in the version of 'Happy Birthday' that you hear in the demonstration video (Step 2 of this Instructable)
"4d8 d# d d# e4 d g f# "
"2f16 r 6f r 3f r 6f r "
"4d8 d# d d# e4 d a g "
"2f16 r 6a r 3as r 6g r "
"4g8 g# g g# 5g4 e "
"5c8 c# c c# 4b+4 d "
"3c16 r d r c r d r 4d "
"5g16 r g r e r e r c r 2c r "
"5d r 3d r 5c r 6c r 7c "