Use back-to-back ‘for’ loops to create a sound effect you can control to sound like a laser blaster, siren, alarm, or anything else you can code up!
The code in the editor below already works. Just plug in your Code Speaker 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: 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 two ‘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.
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.