You are on page 1of 3

/*

*/

const int buzzerPin = 9;

//Sets the pin in which the buzzer is located.

// Sets up the song length, which is the total number of notes and spaces

const int songLength = 20;

// Displays the notes in the song in order.


// A space represents a rest (no tone)

char notes[] = "d ccc ggg BGfdc "; // a space represents a rest

// The beats in each of the notes and rests.


// A "1" represents an eighth-note, 2 a quarter-note, etc.

int beats[] = {2,2,2,1,1,2,2,2,1,1,2,2,4,1,1,1,1,1,2,2,8};

// The tempo is how fast to play the song.


// To make the song play faster, decrease the value of the tempo.

int tempo = 91;


int buzzerpin = 9;

void setup()
{
pinMode(13, OUTPUT);
pinMode(12, OUTPUT);
pinMode(11, OUTPUT);
pinMode(10, OUTPUT);
pinMode(7, OUTPUT);
pinMode(8, OUTPUT);
pinMode(buzzerpin, OUTPUT);
}

void loop()
{
int i, duration;
for (i = 0; i < songLength; i++) // step through the song arrays
{
duration = beats[i] * tempo; // length of note or rest in milliseconds

if (notes[i] == ' ') // Asks if beat is a rest.


{
delay(duration); //Pauses
}
if (notes[i] == 'd'){ // If the note d is played
digitalWrite(13, HIGH); // Turns light on pin 13 on
tone(buzzerpin, frequency(notes[i]), duration); //Takes the frequency coming out of the buzzer
and turns on a light depending on the note frequency
delay(duration); // Delays the light
digitalWrite(13, LOW); //Turns light on pin 13 off
}
if (notes[i] == 'c'){
digitalWrite(12, HIGH);
tone(buzzerpin, frequency(notes[i]), duration);
delay(duration);
digitalWrite(12, LOW);
}
if (notes[i] == 'B'){
digitalWrite(11, HIGH);
tone(buzzerpin, frequency(notes[i]), duration);
delay(duration);
digitalWrite(11, LOW);
}
if (notes[i] == 'G'){
digitalWrite(10, HIGH);
tone(buzzerpin, frequency(notes[i]), duration);
delay(duration);
digitalWrite(10, LOW);
}
if (notes[i] == 'f'){
digitalWrite(7, HIGH);
tone(buzzerpin, frequency(notes[i]), duration);
delay(duration);
digitalWrite(7, LOW);
}
if (notes[i] == 'g'){
digitalWrite(8, HIGH);
tone(buzzerpin, frequency(notes[i]), duration);
delay(duration);
digitalWrite(8, LOW);
}

else // if not, play the note


{
tone(buzzerPin, frequency(notes[i]), duration);
delay(duration); // wait for the tone to finish playing
}
delay(tempo/10); // makes a short pause between the notes
}

int frequency(char note)


{
// Turns a musical note into a frequency.

int i;
const int numNotes = 6; // number of different notes in the song.

// The following arrays show what the frequency is for each note.

char names[] = {'d', 'c', 'g', 'B', 'G', 'f'};


int frequencies[] = {587, 523, 392, 932, 784, 698};

// Now we'll search through the letters in the array, and if


// found, returns the frequency for that note.

for (i = 0; i < numNotes; i++) // Step through the notes


{
if (names[i] == note) // Asks if it is the correct note.
{
return(frequencies[i]); // If so, it returns the frequency.
}
}
return(0); // No values are left, so a value of 0 is returned.
}

You might also like