0% found this document useful (0 votes)
651 views

Code For Arduino Programming Mary Had A Little Lamb

This code uses an Arduino to play the song "Mary Had a Little Lamb" by generating tones on a buzzer pin and displaying lyrics on an LCD screen. It includes arrays that specify the notes, beats, and lyrics of the song. The setup function initializes the buzzer pin and LCD screen. The main loop iterates through the notes, playing tones for the correct duration on the buzzer or displaying lyrics on the LCD based on the capital letters in the notes array. Frequency values for each musical note are stored in separate arrays.

Uploaded by

api-375178431
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
651 views

Code For Arduino Programming Mary Had A Little Lamb

This code uses an Arduino to play the song "Mary Had a Little Lamb" by generating tones on a buzzer pin and displaying lyrics on an LCD screen. It includes arrays that specify the notes, beats, and lyrics of the song. The setup function initializes the buzzer pin and LCD screen. The main loop iterates through the notes, playing tones for the correct duration on the buzzer or displaying lyrics on the LCD based on the capital letters in the notes array. Frequency values for each musical note are stored in separate arrays.

Uploaded by

api-375178431
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

#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.
}
}

int frequency(char note) //Setting the frequencies of the different notes.


{
int i; // Labeling the integer "i"
const int numNotes = 9; // The number of unique notes throughout the song, in this case, there
are only nine unique notes.
char names[numNotes] = { // The notes we will be storing are
'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C','D'}; // a "low c", a "low d", an "e", a "f", a "g", an "a", a "b", a "high
c" and a "high d"
int frequencies[numNotes] = { // The frequencies that correlate to the notes are
262, 294, 330, 349, 392, 440, 494, 523, 590 }; // 262 for a "low c", 294 for a "low d", 330 for
an "e", 349 for a "f", 392 for a "g", 440 for an "a", 494 for a "b", 523 for a "high c" and 590 for a
"high d"

// The computer searches through the letters in the array, and if


// it finds it, it will return the frequency for that note.

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


{
if (names[i] == note) // If this is a note specified,
{
return(frequencies[i]); // then play the corresponding frequency and exit the function.
}
}
return(0); // The computer looked through everything and didn't find it, but still needs to return
a value, so it returns 0.
}

You might also like