Piano DJ Booth

Bring it all together to create a sound effects DJ booth. What kinds of electronic music will you create? Can you modify any of the effects to make them better?

Code

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.

// Each button on Piano activates a different sound effect void setup() { pinMode(10, OUTPUT); //speaker pinMode(2, INPUT_PULLUP); //button pinMode(3, INPUT_PULLUP); //button pinMode(4, INPUT_PULLUP); //button pinMode(5, INPUT_PULLUP); //button pinMode(6, INPUT_PULLUP); //button pinMode(7, INPUT_PULLUP); //button pinMode(8, INPUT_PULLUP); //button pinMode(9, INPUT_PULLUP); //button } void loop() { //rising pitch for (int i = 40; digitalRead(2)==LOW; i++) {//play rising tones as long as button 2 is pressed tone(10, i); delay(3); } //random tones while button 3 is pressed if(digitalRead(3)==LOW) { tone(10, random(1,1201)); //Play any tone between 0 and 1200 delay(50); //a new tone every 50 milliseconds } //falling pitch for (int i = 1300; digitalRead(4)==LOW; i--) {//play falling tones as long as button 4 is pressed tone(10, i); delay(3); } //three pitches rising together for(int i=100; digitalRead(5)==LOW;i++){ //as long as button 5 is pressed... tone(10,i); //play a tone equal to i- this will rise from 100 to 399 delay(10); tone(10,i*2); //play a tone of i*2, range of 200 to 799 delay(10); tone(10,i*4); //play a tone of i*4, range of 400 to 1599 delay(10); } int lowTone = 10; //starting place for a rising tone int highTone = 1800; //starting place for a sinking tone //Low pitch rises as high pitch falls while(digitalRead(6)==LOW){ //While button 6 is pressed, ignore the rest of the void loop tone(10,lowTone); //Play the 'lowTone' variable value, which starts at 10 delay(50); tone(10,highTone); //Play highTone, which starts at 1800 delay(50); highTone-=10;// subtract 10 from highTone lowTone+=10; // add 10 to lowTone //reset the tones if(highTone < 10){ //Keeps highTone from going below 0 highTone = 1500; } if(lowTone > 1800){ //Keeps lowTone from going out of earshot lowTone = 10; } } //Create a steady low beat if(digitalRead(7)==LOW){ tone(10,50); delay(250); noTone(10); delay(250); } //A beat that speeds up as long as button 8 is pressed for(int i=100; digitalRead(8)==LOW; i--){ tone(10,200); delay(i); noTone(10); delay(i); } //A high-pitched beat if(digitalRead(9)==LOW){ tone(10,950); delay(150); noTone(10); delay(150); } noTone(10); //Stops any tones from any buttons at the end of each loop. } // (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!

Challenges

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!


Concepts

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: While loop

The ‘while’ loop here has a special purpose: ignore the rest of the void loop so its variables (lowTone and highTone) don’t get reset. In this special case, you want those variables to reset every time you release the ‘while’ loop button, so that you can continue getting the sound effect over and over. But you don’t want to reset too often, or your variables will never change within the ‘while’ loop. This loop can be tricky, but it’s useful in this program.

New Concept: Building blocks, revisited

This program has lots of components: loops, variables and conditions. But the program can also be broken down into more easily understood groups.

The void loop contains:

  • 1 ‘while’ loop. This is new, but it’s working here very similarly to an ‘if’ statement that just ignores the rest of the void loop.

  • 4 ‘for’ loops. Each ‘for’ loop contains 4 parts: the variable, the condition, the variable change, and the actions. The actions of each ‘for’ loop are some of the most basic commands you learned: tone and delay.

  • 5 ‘if’ statements. Each ‘if’ statement has a condition to check and some actions made up of tones and delays.

  • 6 variables (if you count each ‘i’ variable in the ‘for’ loop as a separate variable, which they are). The variables are being used in place of tone values and delay values.

If you have an understanding of how each of those pieces is working, then you can grasp this entire program, even though it’s almost 100 lines long! Programming is about learning how to break down your problems into understandable pieces, translate those pieces to computer code, and then put the pieces back together to build your solution.

Quiz

If you're having trouble coming up with an answer to a quiz question, try to run an experimental program or look at the example code to help you find the answer.

1. What's a good way to understand a large program?