code for arduino programming mary had a little lamb

|
All materials on our website are shared by users. If you have any questions about copyright issues, please report us to resolve them. We are always happy to assist you.
 7 views
of 5

Please download to get full document.

View again

Description
#include // Include a Liquid Crystal screen (Screen used to display words). LiquidCrystal lcd(12, 11, 5, 4, 3, 2); // LCD is connected to these pins const int buzzerPin = 10; // Connect the buzzer to pin 10 const int songLength = 158; // The song has this number of notes (158 total, including spaces) char notes[] = bagaHbbb Haaa HbDD JbagaHbbb KbaaLbag MbagaNbbb Naaa NbDD MbagaNbbb ObaaPbag DQbagaRbbb Raaa RbDD QbagaRbbb SbaabTag DUbagaVbbb Vaaa VbDD UbagaVbbb WbaabXag Y ;
Share
Tags
Transcript
  #include <LiquidCrystal.h> // Include a Liquid Crystal screen (Screen used to display words). LiquidCrystal lcd(12, 11, 5, 4, 3, 2); // LCD is connected to these pins const int buzzerPin = 10; // Connect the buzzer to pin 10 const int songLength = 158; // The song has this number of notes (158 total, including spaces) char notes[] = bagaHbbb Haaa HbDD JbagaHbbb KbaaLbag MbagaNbbb Naaa NbDD MbagaNbbb ObaaPbag DQbagaRbbb Raaa RbDD QbagaRbbb SbaabTag DUbagaVbbb Vaaa VbDD UbagaVbbb WbaabXag Y ; // The notes in the song, on a treble clef scale. // A space represents a rest, and capital letters H through X are used to initialize lyrics that correlate to a certain portion of the song. // The lowercase letters a through g represent notes, with set frequencies later in the code. int beats[] = {2,2,2,2,0,2,2,4,1,0,2,2,4,1,0,2,2,4,1,0,2,2,2,2,0,2,2,4,1,0,2,2,2,0,2,2,4,1,0,2,2,2,2,0,2,2,4,1,0,2,2,4,1,0,2,2,4,1,0,2,2,2,2,0,2,2,4,1,0,2,2,2,0,2,2,4,1,2,0,2,2,2,2,0,2,2,4,1,0,2,2,4,1,0,2,2,4,1,0,2,2,2,2,0,2,2,4,1,0,2,2,2,2,0,2,4,1,2,0,2,2,2,2,0,2,2,4,1,0,2,2,4,1,0,2,2,4,1,0,2,2,2,2,0,2,2,4,1,0,2,2,2,2,0,2,4,1,0,}; // The length of each note in the song. // It is on a scale of 4, where 1 represents a quarter-note, 2 represents a half note, etc. // The zeros correlate to the capital letters, because they are used to initialize lyrics and are not actual notes (they do not have frequencies). int tempo = 150; // The tempo is a command of how fast to play the song, in beats per second. (In this case, the tempo is 150 beats per second) void setup() // This will setup the functions necessary for the code to work, suchh as initializing the LCD and buzzer. { pinMode(buzzerPin, OUTPUT); // Configures the buzzer pin, and makes it into an output instead of an input. pinMode (13, OUTPUT) ;// Connect the LED to pin 13, and configues it as an output instead of an input lcd.begin(16, 2); //Initialize the 16x2 LCD lcd.clear(); //Clear any old data displayed on the LCD   lcd.print( Mary had a ); // Display a message on the LCD screen [In this case, it displays the beginning lyrics to the song, ( Mary had a )] } void loop() // This will run the following functions over and over again. { int i, duration; // Labeling the integer as i. for (i = 0; i < songLength; i++) // This function is used to index through the arrays { duration = beats[i] * tempo; // Length of note/rest in milliseconds if (notes[i] == ' ') // If the note is a rest, { delay(duration); // then pause for just a moment (the length of time specified earlier in the code) } else if (notes[i] == 'H') // If the note is the capital letter H, { lcd.clear(); // then clear the LCD lcd.print( little lamb ); // and write little lamb on the LCD screen digitalWrite (13, HIGH); // Turn on the LED } else if (notes[i] == 'J') // If the note is the capital letter J, { lcd.clear(); // then clear the LCD lcd.print ( Mary had a ); // and write Mary had a on the LCD screen digitalWrite (13, LOW);// Turn off the LED } else if (notes[i] == 'K') // If the note is the capital letter K, { lcd.clear(); // then clear the LCD lcd.print ( whos fleece was ); // and write whos fleece was digitalWrite (13, LOW);// Turn off the LED } else if (notes[i] == 'L') // If the note is the capital letter L, { lcd.clear(); // then clear the LCD lcd.print ( white as snow ); // and write white as snow on the LCD screen }   else if (notes[i] == 'M') // If the note is the capital letter M, { lcd.clear(); // then clear the LCD lcd.print ( Everywhere that ); // and write Everywhere that on the LCD screen } else if (notes[i] == 'N') // If the note is the capital letter N, { lcd.clear(); // then clear the LCD lcd.print ( Mary went ); // and write Mary went on the LCD screen } else if (notes[i] == 'O') // If the note is the capital letter O, { lcd.clear(); // then clear the LCD lcd. print ( the lamb was ); // and write the lamb was on the LCD screen digitalWrite (13, HIGH); // Turn on the LED } else if (notes[i] == 'P') // If the note is the capital letter P, { lcd.clear(); // then clear the LCD lcd.print ( sure to go. It ); // and write sure to go. It on the LCD screen digitalWrite (13, LOW);// Turn off the LED } else if (notes[i] == 'Q') // If the note is the capital letter Q, { lcd.clear(); // then clear the LCD lcd.print ( followed her to ); // and write followed her to on the LCD screen } else if (notes[i] == 'R') // If the note is the capital letter R, { lcd.clear(); // then clear the LCD lcd.print ( school one day ); // and write school one day on the LCD screen } else if (notes[i] == 'S')// If the note is the capital letter S, { lcd.clear(); // then clear the LCD lcd.print ( which was ); // and write which was on the LCD screen lcd. setCursor (0,1); // Reposition the cursor to row 0, column 1 (bottom left), lcd. print ( against ); // and write against on the LCD screen } else if (notes[i] == 'T')// If the note is the capital letter T, { lcd.clear(); // then clear the LCD lcd.print ( the rules ); // and write the rules on the LCD screen }   else if (notes[i] == 'U') // If the note is the capital letter U, { lcd.clear(); // then clear the LCD lcd.print ( It made the );// and write It made the on the LCD screen lcd. setCursor (0,1); // Reposition the cursor to row 0, column 1 (bottom left), lcd.print ( children ); // and write children on the LCD screen } else if (notes[i] == 'V') // If the note is the capital letter V, { lcd.clear(); // then clear the LCD lcd.print ( laugh and play ); // and write laugh and play on the LCD screen } else if (notes[i] == 'W') // If the note is the capital letter W, { lcd.clear(); // then clear the LCD lcd.print ( To see a lamb ); // and write To see a lamb on the LCD screen digitalWrite (13, HIGH); // Turn on the LED } else if (notes[i] == 'X') // If the note is the capital letter X, { lcd.clear(); // then clear the LCD lcd.print ( in school. ); // and write in school on the LCD screen digitalWrite (13, LOW);// Turn off the LED } else if (notes[i] == 'Y') // If the note is the capital letter Y, { lcd.clear(); // then clear the LCD lcd.print ( The End :) ); // and write The End :) on the LCD screen } else // Otherwise, play the note per usual { tone(buzzerPin, frequency(notes[i]), duration // The length of time of the note has been specified as a tone, and a ); delay(duration); // delay will wait for tone to finish } delay(tempo/10); // There will be a very brief pause between notes } while(true){ // This function makes the song only play once, by pausing forever. }
Related Search
We Need Your Support
Thank you for visiting our website and your interest in our free products and services. We are nonprofit website to share and download documents. To the running of this website, we need your help to support us.

Thanks to everyone for your continued support.

No, Thanks