Random Note Piano Key

Unleash a random stream of notes when you press down one button on the piano.


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.

// Play a stream of random tones when button 6 on the Piano is pressed void setup(){ pinMode(10,OUTPUT); //speaker set to OUTPUT pinMode(6,INPUT_PULLUP); //button that will activate tones } void loop(){ if(digitalRead(6)==LOW){ //If button 6 is pressed, sending a LOW signal... tone(10,random(40,800)); //... play a tone ranging from 40 to 799 on the speaker delay(100); //Wait 100 milliseconds before continuing through the void loop } else{ //in all other cases (when button 6 is not pressed)... noTone(10); //send a noTone command to stop any tones from playing } } // (c) 2018 Let's Start Coding. License: www.letsstartcoding.com/bsdlicense

Walkthrough Videos

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: Combining concepts

As you learn more and more about code, you’ll find new ways to use concepts you’re already familiar with. In this case, you’ve already used the ‘random’ function, but now you have the power of ‘if’ statements to combine it with!

A curious coder is a good coder, so think about ways that you can integrate your latest skills with some of the concepts you’re already familiar with. You will be surprised how many fun combinations you can create!


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

1. What are the two arguments that the random function requires?