If You’re Happy and You Know It - Code Song
The classic “If You’re Happy and You Know It” 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 'If You're Happy and You Know It' with variables for notes, tone duration, and rest length
//Create the notes in the song as variables to speed up the coding process
int A = 440;
int B = 494;
int C = 523;
int D = 587;
int E = 659;
int F = 699;
int G = 784;
int highA = 880; //This A note is used once at the end of the second line
int note = 300; //the duration of the tone that is playing
int rest = 20; //the duration of the rest between the same notes in the song
int lineRest = note*3; //the rest between lines of the music
void setup(){
pinMode(10,OUTPUT); //Set the speaker as an OUTPUT
}
void loop(){
//First Line
tone(10,C);
delay(note);
noTone(10); //Pause between the same 2 notes so it sounds more like pressing the key again
delay(rest);
tone(10,C);
delay(note);
//Six 'F' notes
tone(10,F);
delay(note);
noTone(10); //Pause between the same 2 notes so it sounds more like pressing the key again
delay(rest);
tone(10,F);
delay(note);
noTone(10); //Pause between the same 2 notes so it sounds more like pressing the key again
delay(rest);
tone(10,F);
delay(note);
noTone(10); //Pause between the same 2 notes so it sounds more like pressing the key again
delay(rest);
tone(10,F);
delay(note);
noTone(10); //Pause between the same 2 notes so it sounds more like pressing the key again
delay(rest);
tone(10,F);
delay(note);
noTone(10); //Pause between the same 2 notes so it sounds more like pressing the key again
delay(rest);
tone(10,F);
delay(note);
tone(10,E);
delay(note);
tone(10,F);
delay(note);
tone(10,G);
delay(note);
noTone(10);
delay(lineRest);
//Second Line
tone(10,C);
delay(note);
noTone(10); //Pause between the same 2 notes so it sounds more like pressing the key again
delay(rest);
tone(10,C);
delay(note);
//Six G notes
tone(10,G);
delay(note);
noTone(10); //Pause between the same 2 notes so it sounds more like pressing the key again
delay(rest);
tone(10,G);
delay(note);
noTone(10); //Pause between the same 2 notes so it sounds more like pressing the key again
delay(rest);
tone(10,G);
delay(note);
noTone(10); //Pause between the same 2 notes so it sounds more like pressing the key again
delay(rest);
tone(10,G);
delay(note);
noTone(10); //Pause between the same 2 notes so it sounds more like pressing the key again
delay(rest);
tone(10,G);
delay(note);
noTone(10); //Pause between the same 2 notes so it sounds more like pressing the key again
delay(rest);
tone(10,G);
delay(note);
tone(10,F);
delay(note);
tone(10,G);
delay(note);
tone(10,highA);
delay(note);
noTone(10);
delay(lineRest);
//Third Line
tone(10,A);
delay(note);
noTone(10); //Pause between the same 2 notes so it sounds more like pressing the key again
delay(rest);
tone(10,A);
delay(note);
//Four B notes
tone(10,B);
delay(note);
noTone(10); //Pause between the same 2 notes so it sounds more like pressing the key again
delay(rest);
tone(10,B);
delay(note);
noTone(10); //Pause between the same 2 notes so it sounds more like pressing the key again
delay(rest);
tone(10,B);
delay(note);
noTone(10); //Pause between the same 2 notes so it sounds more like pressing the key again
delay(rest);
tone(10,B);
delay(note);
tone(10,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);
delay(note);
noTone(10);
delay(lineRest);
//Fourth Line
tone(10,B);
delay(note);
noTone(10); //Pause between the same 2 notes so it sounds more like pressing the key again
delay(rest);
tone(10,B);
delay(note);
noTone(10); //Pause between the same 2 notes so it sounds more like pressing the key again
delay(rest);
//Three A notes
tone(10,A);
delay(note);
noTone(10); //Pause between the same 2 notes so it sounds more like pressing the key again
delay(rest);
tone(10,A);
delay(note);
noTone(10); //Pause between the same 2 notes makes it sound more like pressing the key again
delay(rest);
tone(10,A);
delay(note);
tone(10,G);
delay(note);
tone(10,F);
delay(note);
noTone(10);
delay(rest);
tone(10,F);
delay(note);
noTone(10);
delay(lineRest);
//Fifth Line
tone(10,A);
delay(note);
noTone(10); //Pause between the same 2 notes so it sounds more like pressing the key again
delay(rest);
tone(10,A);
delay(note);
tone(10,G);
delay(note);
noTone(10); //Pause between the same 2 notes so it sounds more like pressing the key again
delay(rest);
tone(10,G);
delay(note);
noTone(10); //Pause between the same 2 notes so it sounds more like pressing the key again
delay(rest);
tone(10,G);
delay(note);
tone(10,F);
delay(note);
tone(10,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);
delay(note);
noTone(10); //Pause between the same 2 notes so it sounds more like pressing the key again
delay(rest);
tone(10,D);
delay(note);
tone(10,E);
delay(note);
tone(10,F);
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: