const int buzzer = 3;
const int ledPin = 13;
const int songlength = 18;
char notes[] = "cdfda ag cdfdg gf";
int beats[] = {1, 1, 1, 1, 1, 1, 4, 4, 2, 1, 1, 1, 1, 1, 1, 1, 4, 4};
int tempo = 150;
void setup()
{
pinMode(buzzer, OUTPUT);
pinMode(ledPin, OUTPUT);
}
void loop()
{
int i, duration;
for (i = 0; i < songlength; i++) {
duration = beats[i] * tempo;
if (notes[i] == ' ') {
delay(duration);
digitalWrite(ledPin, LOW);
} else {
digitalWrite(ledPin, HIGH);
tone(buzzer, frequency(notes[i]), duration);
delay(duration);
}
delay(tempo );
}
}
int frequency(char note) {
int i;
const int numNotes = 8;
char names[] = {'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C'};
int frequencies[] = {262, 294, 330, 349, 392, 440, 494, 523};
for (i = 0; i < numNotes; i++) {
if (names[i] == note) {
return frequencies[i]; // Yes! Return the frequency
}
}
return 0;
}