Use a ‘for’ loop to control where your tone starts, how high it will play, and how fast it gets there. Sounds like a siren!
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!
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: Loops within loops
In this project, you gained more control over how your code runs. In past projects, you had the void loop running an infinite number of times. Now, you can write a loop within the loop, which gives you more control over how the code will run.
New Concept: Speed, revisited
Your ‘for’ loop runs 760 times in this program (800-40). That means the time it takes to complete is 760 * the number of milliseconds delay within the ‘for’ loop. By changing the delay from 3 milliseconds to 6 milliseconds, you’re actually making the change from 2280 milliseconds (7603) to 4560 milliseconds (760*6). Even those tiny differences can have a big impact when you’re running a loop hundreds or thousands of times.