Вы находитесь на странице: 1из 7

const int buzzerPin = 13;

//Piano buzzer is linked to pin 13

const int songLength = 97;


//Total number of notes and rests in the song

char notes[] = "EEE cEGgcg e ab BagEGAfG Ecdbcg e ab BagEGAfG Ecdbcg e ab BagEGAfG


Ecdbcg e ab BagEGAfG Ecdb";
//The notes and the rests in the song in order

int beats[] = {1.5,2,1.5,


1.5
,1.5,2,4,4,3,1.5
,1.5
,2
,2
,1.5,2
,1.5
,1.5,1.5,2,1.5,1.5,1.5,2,1.5
,1.5
,1.5,2,1.5,1.5,3,3
,1.5
,2
,2
,1.5,2
,1.5,1.5,1.5,2,1.5,1.5,1.5,2,1.5
,1.5
,1.5,2,1.5,1.5,3,3
,1.5
,2
,2
,1.5,2
,1.5,1.5,1.5,2,1.5,1.5,1.5,2,1.5
,1.5
,1.5,2,1.5,1.5,3,3
,1.5
,2
,2
,1.5,2
,1.5,1.5,1.5,2,1.5,1.5,1.5,2,1.5
,1.5
,1.5,2,1.5,1.5,3};
//The number of beats of each of the notes and rests,
//corresponding to the above char notes

int tempo = 150;


//tempo of the song, the higher it is the slower the song
//the lower the number, the faster the song plays

void setup() //says whether each pin is an output or an input


{
pinMode(buzzerPin, OUTPUT);//buzzer pin is an output
pinMode(1, OUTPUT);
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
pinMode(11, OUTPUT);
}
//Says that these pins are in use, and that they are outputs and not inputs

void loop() //This is what is being looped and used again


{

int i, duration;
//defining varible i
for (i = 0; i < songLength; i++) // Go through the song
{
duration = beats[i] * tempo; // the duration is the numeber of beats

if (notes[i] == ' ') // We are talking about the spaces


{
delay(duration); // pause for the beats
}

else if(notes[i] == 'e')//If the note is 'e'


{digitalWrite(1, HIGH); //Turn the LED connected to pin 1 on
tone (buzzerPin, frequency(notes[i]), duration);//'e' is frequency from below, and stays for the
duration of that note
digitalWrite(1, LOW);//After note is done, turn off the LED
delay(duration);}//Keeps the note have the duration of the beats
else if(notes[i] == 'g')//If the not is 'g'
{digitalWrite(2, HIGH);//Turn on LED connected to pin 2
tone (buzzerPin, frequency(notes[i]), duration);//what frequency of 'g' is
digitalWrite(2, LOW);//after note is done turn off LED
delay(duration);}//Stay on for the beats
else if(notes[i] == 'a')//if the note is a
{digitalWrite(3, HIGH);//turn the LED in pin 3 on
tone (buzzerPin, frequency(notes[i]), duration);//If buzzer makes that sound
digitalWrite(3, LOW);delay(duration);}//After the note is done, turn LED off
else if(notes[i] == 'B')//If the note is B
{digitalWrite(4, HIGH);//Turn the LED linked to pin 4 on
tone (buzzerPin, frequency(notes[i]), duration);//If the tone of that note comes to the buzzer
digitalWrite(4, LOW);//After note is done turn off the LED
delay(duration);}//Play for number of beats
else if(notes[i] == 'b')//if note is 'b'
{digitalWrite(5, HIGH);//Turn on LED in pin 5
tone (buzzerPin, frequency(notes[i]), duration);// this frequency comes from buzzer
digitalWrite(5, LOW);//After done turn off LED
delay(duration);}//Play until beats are finished
else if(notes[i] == 'c')//If the note is 'c'
{digitalWrite(6, HIGH);//Turn on LED in pin 6
tone (buzzerPin, frequency(notes[i]), duration);//Frequecny for ' c' plays
digitalWrite(6, LOW);//Turn off after done with note
delay(duration);}//On for the number of beats
else if(notes[i] == 'd')//if the note is 'd'
{digitalWrite(7, HIGH);//Turn on LED in pin 7 on
tone (buzzerPin, frequency(notes[i]), duration);//Play that frequecny
digitalWrite(7, LOW);//Turn off after done
delay(duration);}//On for number of beats
else if(notes[i] == 'E')// If it is not 'E'
{digitalWrite(8, HIGH);//turn on LED
tone (buzzerPin, frequency(notes[i]), duration);//on for beats duration
digitalWrite(8, LOW);//turn off after done
delay(duration);}//On for the beats
else if(notes[i] == 'f')//if the note is 'f'
{digitalWrite(9, HIGH);//turn on LED connected to pin 9
tone (buzzerPin, frequency(notes[i]), duration);//The tone will be corresponding frequecny
digitalWrite(9, LOW);//Turn off LED after done
delay(duration);}//How long the LED is on for duration
else if(notes[i] == 'G')//If the note is 'G'
{digitalWrite(10, HIGH);//Turn on the LED
tone (buzzerPin, frequency(notes[i]), duration);//The buzzer will made corresponding tone
digitalWrite(10, LOW);//Turn of the LED after note is done
delay(duration);}//On off the number of beats
else if(notes[i] == 'A')//if the note is 'A'
{digitalWrite(11, HIGH);//Turn on the LED
tone (buzzerPin, frequency(notes[i]), duration);//The note will make that corresponding
frequecny
digitalWrite(11, LOW);//Turn off the LED after its done
delay(duration);}//On for the beats
else // If it is not space, play different note
{
tone(buzzerPin, frequency(notes[i]), duration);//the tone of rest
delay(duration); // Don't play anything until beats are complete
}
delay(tempo/10); // the pause between the notes

}
}

int frequency(char note) //Here we will define the notes

{
int i;//Varible i

const int numNotes = 12; // How many notes we are using

char names[] = { 'a', 'A', 'b', 'B', 'c', 'd', 'e', 'E', 'f', 'g', 'G'};
//Name of the note
int frequencies[] = {440, 880, 493, 466, 523, 587, 329, 659, 698, 391, 783};
//Frequencies of the notes corresponding with the char notes

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


{
if (names[i] == note) // If it is that note
{
return(frequencies[i]); // If so play the corresponding frequency
}
}
return(0); // If you don't find it go back to 0
}

Вам также может понравиться