Bring all eight keys into play and build a fully functioning piano. Now play some tunes!

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 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,500); //...play a tone of 500 hertz on the speaker } else if(digitalRead(3)==LOW){ //if button 3 is sending a low signal... tone(10,600); //...play a tone of 600 hertz on the speaker } else if(digitalRead(4)==LOW){ //if button 4 is sending a low signal... tone(10,700); //...play a tone of 700 hertz on the speaker } else if(digitalRead(5)==LOW){ //if button 5 is sending a low signal... tone(10,800); //...play a tone of 800 hertz on the speaker } else if(digitalRead(6)==LOW){ //if button 6 is sending a low signal... tone(10,900); //...play a tone of 900 hertz on the speaker } else if(digitalRead(7)==LOW){ //if button 7 is sending a low signal... tone(10,1000); //...play a tone of 1000 hertz on the speaker } else if(digitalRead(8)==LOW){ //if button 8 is sending a low signal... tone(10,1100); //...play a tone of 1100 hertz on the speaker } else if(digitalRead(9)==LOW){ //if button 9 is sending a low signal... tone(10,1200); //...play a tone of 1200 hertz on the speaker } 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: www.letsstartcoding.com/bsdlicense
 

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!


Challenge

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: Looking for patterns

If you’ve done the last project, you’re probably seeing the patterns that continue into this project, too. Just like your Morse code message, understanding this program is about understanding that there are ‘building blocks’ in it that are repeated over and over. If you had 100 buttons, you could code them all to have a different tone by using the same pattern that you see in this project.

Remember that the computer doesn’t care about line breaks, indenting, or comments, but as you start to repeat lines of code over and over, those become important tools. If your code is a jumbled mess, it gets harder and harder to see the patterns and to modify them when you’re ready to make a change.


Quiz

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

1. How many 'if' and 'else if' statements can you have in a single program?




2. If you hold down button 3 and button 5 at the same time, which note will play?