|
Write a comment |
Articles |
DHTML Gallery |
.NET |
MyBlog |
About Me |
FAQ
|
|
How To Play Your Favorite Melody
with Arduino![]() My Freeduino board playing a melody! In this tutorial, I will discuss how make Arduino play your favorite melody. There is a nice tutorial which illustrate the basic process here. But to play your favourite melody you will have to get the combination of notes, durations of individual notes and pauses as precise as possible. In this tutorial, I will discuss how to develop a sketch for Arduino to play custom melodies and way to test the sketch on your PC before writing it to Arduino board. Things you will need:
Finding notes for your melody/tune cdf dfg fga gab abc# (plays piano keys in this fashion: 123,234,345,456,567)
Writing your sketch
Creating PC program to test note compilation
//Play the following notes
//cdf dfg fga gab abc#
int[] melody = new int[] { NOTE_C4,0,NOTE_D4,0,NOTE_F4,0,
NOTE_D4,0,NOTE_F4,0,NOTE_G4,0,
NOTE_F4,0,NOTE_G4,0, NOTE_A4,0,
NOTE_G4,0,NOTE_A4,0, NOTE_B4,0,
NOTE_A4,0,NOTE_B4,0, NOTE_C5,0};
int[] noteDurations = new int[] { 8,8,8,8,8,4,
8,8,8,8,8,4,
8,8,8,8,8,4,
8,8,8,8,8,4,
8,8,8,8,8,4};
for (int thisNote = 0; thisNote < 30; thisNote++)
{
int noteDuration = 1000 / noteDurations[thisNote];
if (melody[thisNote] > 0)
Console.Beep(melody[thisNote], noteDuration);
else
System.Threading.Thread.Sleep(noteDuration);
}
Now once, you have perfected your compilation, you can simply copy the array variables along with their values back into your Arduino sketch and just compile it. Given below are snippet of C code after pasting the array values in the Arduino IDE:
int melody[] = { NOTE_C4,0,NOTE_D4,0,NOTE_F4,0,
NOTE_D4,0,NOTE_F4,0,NOTE_G4,0,
NOTE_F4,0,NOTE_G4,0, NOTE_A4,0,
NOTE_G4,0,NOTE_A4,0, NOTE_B4,0,
NOTE_A4,0,NOTE_B4,0, NOTE_C5,0};
// note durations: 4 = quarter note, 8 = eighth note, etc.:
int noteDurations[] = { 8,8,8,8,8,4,
8,8,8,8,8,4,
8,8,8,8,8,4,
8,8,8,8,8,4,
8,8,8,8,8,4};
Also don't forget to change the constant representing array length to correct value in this line of your sketch :
for (int thisNote = 0; thisNote < 30; thisNote++) {
Download the full sketch here. Play the video (below) to listen to the PC and Arduino playing the same melody:
Finally |
| Copyright (c) 2007-2011 Ashish Patil . Please read FAQ for more details. |