Juanrohanfinal

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

#include <LiquidCrystal.

h> //Including the LCD // Include the LCD


LiquidCrystal lcd(12,11,5,4,3,2); //The pins the LCD is in

const int buzzerPin = 9; //The pin the buzzer is in is 9

const int songLength = 338;


//This is how long the song is in seconds
char notes[] = "GFEDbgCDEFDd C g edc cdecqc_ C g EDCCDEFDbC C g GFE cdecqc_
C g GFECDEFDbC ttycedc_ttycefgigfefgcgfedefefg
ttycedc_ttycefgigfgiCgecdedq_qc EFGGFEDED CDEEDCbCD ipCCDDEFEpEFWWSWFWF
EFGGFEDED CDEEDCbCD ipCCDDEFEpCCCCCCT C g edc cdecqc_ C g EDCCDEFDbC C g GFE
cdecqc_ C g GFECDEFDbC "; // a space represents a rest
//These are the notes used to play the song

int beats[] = {4, 2, 2, 2, 2, 4, 2, 2, 2, 2, 8, 6, 2, 2, 2, 2, 2, 2, 2, 2, 2,


2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 2, 2, 2, 2, 4, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 2, 2, 2,
2, 4, 4, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 4, 3, 1, 2, 1, 1, 2, 2, 4,
3, 1, 2, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 4, 3, 1,
2, 1, 1, 2, 2, 2, 2, 3, 1, 1, 1, 1, 1, 4, 4, 3, 1, 1, 1, 1, 1, 3, 1, 3, 1, 3,
1, 1, 1, 1, 1, 3, 1, 3, 1, 3, 1, 1, 1, 1, 1, 3, 1, 2, 2, 3, 1, 1, 1, 1, 1, 3,
1, 3, 1, 3, 1, 1, 1, 1, 1, 3, 1, 3, 1, 3, 1, 1, 1, 1, 1, 3, 1, 3, 1, 3, 1, 1,
1, 1, 1, 3, 1, 2, 2, 3, 1, 1, 1, 1, 1, 6, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 2, 2, 2, 2, 4, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 2, 2, 2, 2, 4,
4};
//This is the rhythm pattern the notes follow. Together, these two things
make up the song.

int tempo = 160;


//This is the speed of the song

void setup() //setup function


{

lcd.begin(16, 2); // Begin at 16, 2


lcd.clear(); // Clear the screen on the LCD
lcd.print("Now Playing:"); // Print Now Playing

pinMode(buzzerPin, OUTPUT);
//buzzerPin is the output
}
void loop()
{

int i, duration; //duration with the variable integer

for (i = 0; i < songLength; i++) // step through the song


{
duration = beats[i] * tempo; //i is the counter? and the duration is
represented by the beats*(i) * tempo
lcd.setCursor(0,1); // Set the cursor to the second line and first
digit
lcd.print("Ftn Of Drms: "); // Print Ftn of Dreams
lcd.print(millis()/1000);
if (notes[i] == ' ') // Consider ' ' a rest and delay duration
with it
{
delay(duration); // Pause
}
else // If not, play the note
{
tone(buzzerPin, frequency(notes[i]), duration);
delay(duration); // delay the song for an amount of time
}
delay(tempo/10); // This gives a pause between notes
}

while(true){}
// This determines if you want to stop the song after one play or not
}

int frequency(char note)


{
// This takes the note and returnes the hertz to play that note

int i; // integer i
const int numNotes = 24; // number of notes stored

char names[] = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C', 'D', 'E', 'F',
'G', 'A', 'B', '!', '_', 'q', 't', 'y', 'i', 'p', 'W', 'S', 'T'};
//these are the notes which are associated with letters

int frequencies[] = {262, 294, 311, 349, 392, 440, 466, 523, 587, 622, 698,
784, 880, 988, 1047, 196, 233, 175, 208, 415, 466, 740, 831, 494};
// These arrays hold the characters and their corresponding hertz. For
example, 'c' is 262 and 'T' is 494.
// The code will get the letter and search for the hertz and return it

for (i = 0; i < numNotes; i++) // The song is not going to stop until all
of the notes are played
{
if (names[i] == note) // If the name is equal to the note then
return the frequency
{
return(frequencies[i]); //return the frequencies with the counter
}
}
return(0); // return nothing
}

You might also like