Replace the hertz values for your piano with well-named variable to make it easier to update and modify your notes.

The code in the editor below already works Just plug in your Code Piano and press upload to see what it does! Tinker with it and make changes to see what each line of code controls.

/* Create a full range of notes on your piano using all of the buttons * Use variables to stand in for number values to make your program easier to read * You can find the conversion of hertz to notes online or on your purple cards */ int A = 440; //An A note is equal to 440 hertz. Now 'A' will stand in for 440 in your loop int B = 494; int C = 523; int D = 587; int E = 659; int F = 699; int G = 784; int highA = 880; void setup(){ pinMode(10,OUTPUT); //Set speaker as an OUTPUT pinMode(2,INPUT_PULLUP); //Button 2 as an INPUT_PULLUP pinMode(3,INPUT_PULLUP); //Button 3 as an INPUT_PULLUP pinMode(4,INPUT_PULLUP); //Button 4 as an INPUT_PULLUP pinMode(5,INPUT_PULLUP); //Button 5 as an INPUT_PULLUP pinMode(6,INPUT_PULLUP); //Button 6 as an INPUT_PULLUP pinMode(7,INPUT_PULLUP); //Button 7 as an INPUT_PULLUP pinMode(8,INPUT_PULLUP); //Button 8 as an INPUT_PULLUP pinMode(9,INPUT_PULLUP); //Button 9 as an INPUT_PULLUP } void loop(){ if(digitalRead(2)==LOW){ //if button 2 is sending a low signal... tone(10,A); // a tone of 440 hertz on the speaker } else if(digitalRead(3)==LOW){ //if button 3 is sending a low signal... tone(10,B); // a tone of 494 hertz on the speaker } else if(digitalRead(4)==LOW){ tone(10,C); } else if(digitalRead(5)==LOW){ tone(10,D); } else if(digitalRead(6)==LOW){ tone(10,E); } else if(digitalRead(7)==LOW){ tone(10,F); } else if(digitalRead(8)==LOW){ tone(10,G); } else if(digitalRead(9)==LOW){ tone(10,highA); } else{ //in all other cases (buttons not sending a low signal)... noTone(10); //send a noTone command to the speaker } } // (c) 2018 Let's Start Coding. License:

Watch the videos for line-by-line explanation of how the example program works. Then you'll be ready to make some changes of your own!


Can you complete the challenge? Change the code in your code editor above. Upload your code to see the effect when you're finished. Complete a challenge? Check it off the list!

These are the new code concepts covered in this example program. To become a great coder, read through these concepts to learn new vocabulary.

New Concept: Variables replace fixed number values

By using variables throughout your loop in this project, you made it much easier to read the code now and modify it later. You almost don’t need comments to understand what is happening in the void loop. You can imagine how much easier it would be to write an automatically playing tune when you just need to identify the notes in the song and you can code more naturally with those variables.

Although all we’ve really done is rename our hertz values with a letter, this step makes a big difference when it comes to clarifying the code. Good coders are always looking for ways to make things simple and streamlined so that sneaky problems don’t show up later.


If you're having trouble, try to run an experimental program or look at the example code to help you find the answer.

1. When picking a variable name, it's a good idea to pick a name that is....

2. Based on this example program, how would you define 'variable'?