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?
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.
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!
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.