Mary Had a Little Lamb - Code Song
The classic “Mary Had a Little Lamb” song written in code. We recommend that you’ve followed the projects up to at least speaker project 8 so you’ll have an understanding of how this song is coded with variables. Start here and tinker, modify, and update to make your own version or your own completely new song!
// Play 'Mary Had a Little Lamb' with variables for notes, tone duration, and rest length
//Create the notes in the song as variables to speed up the coding process
int C = 523;
int D = 587;
int E = 659;
int G = 784;
int note = 500; //the duration of the tone that is playing
int rest = 20; //the duration of the rest between the same notes in the song
void setup(){
pinMode(10,OUTPUT); //Set the speaker as an OUTPUT
}
void loop(){
tone(10,E); //E
delay(note);
tone(10,D); //D
delay(note);
tone(10,C); //C
delay(note);
tone(10,D); //D
delay(note);
tone(10,E); //E
delay(note);
noTone(10); //Pause between the same 2 notes so it sounds more like pressing the key again
delay(rest);
tone(10,E); //E
delay(note);
noTone(10); //Pause between the same 2 notes so it sounds more like pressing the key again
delay(rest);
tone(10,E); //E
delay(note);
tone(10,D); //D
delay(note);
noTone(10); //Pause between the same 2 notes so it sounds more like pressing the key again
delay(rest);
tone(10,D); //D
delay(note);
noTone(10); //Pause between the same 2 notes so it sounds more like pressing the key again
delay(rest);
tone(10,D); //D
delay(note);
tone(10,E); //E
delay(note);
tone(10,G); //G
delay(note);
noTone(10); //A pause between the same 2 notes makes it sound more like pressing the key again
delay(rest);
tone(10,G); //G
delay(note);
tone(10,E); //E
delay(note);
tone(10,D); //D
delay(note);
tone(10,C); //C
delay(note);
tone(10,D); //D
delay(note);
tone(10,E); //E
delay(note);
noTone(10); //Pause between the same 2 notes so it sounds more like pressing the key again
delay(rest);
tone(10,E); //E
delay(note);
noTone(10); //Pause between the same 2 notes so it sounds more like pressing the key again
delay(rest);
tone(10,E); //E
delay(note);
noTone(10); //Pause between the same 2 notes so it sounds more like pressing the key again
delay(rest);
tone(10,E); //E
delay(note);
tone(10,D); //D
delay(note);
noTone(10); //Pause between the same 2 notes so it sounds more like pressing the key again
delay(rest);
tone(10,D); //D
delay(note);
tone(10,E); //E
delay(note);
tone(10,D); //D
delay(note);
tone(10,C); //C
delay(note);
noTone(10);
delay(5000); // Wait for 5 seconds before starting again
}
// (c) 2018 Let's Start Coding. License: www.letsstartcoding.com/bsdlicense
Heads up! You need our Chrome App to run this code.
Success!
Compile errors:
Serial port monitor:
Input: