Hi,
I have been using my arduino kit for about a week now, learning it as I go along, I've recently taken to making my own songs with the buzzer, I have already programmed about 10 songs, all of which work perfectly. However, my latest effort to re-create the "Hello" song from Close Encounters of the 3rd kind has failed! You see the first 3 notes "D5, E5 and C5" play perfectly, however it completely skips the last two notes and I have no idea why, some help would be useful, thanks!
const int buzzerPin = 9;
const int songLength = 9;
char notes[] = "d e c C g";
int beats[] = {2,1,2,1,2,1,2,1,4};
int tempo = 360;
void setup()
{
pinMode(buzzerPin, OUTPUT);
}
void loop()
{
int i, duration;
for (i = 0; i < songLength; i++)
{
duration = beats[i] * tempo;
if (notes[i] == ' ')
{
delay(duration);
}
else
{
tone(buzzerPin, frequency(notes[i]), duration);
delay(duration);
}
}
}
int frequency(char note)
{
int i;
const int numNotes = 3;
char names[] = { 'd', 'e', 'c', 'C', 'g' };
int frequencies[] = { 587, 659, 523, 262, 392 };
for (i = 0; i < numNotes; i++)
{
if (names[i] == note)
{
return(frequencies[i]);
}
}
return(0);
}