Piano Siren: Tone Looping

Use a ‘for’ loop to control where your tone starts, how high it will play, and how fast it gets there. Then double down with a second “falling” ‘for’ loop.

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.

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: Short-term variables & scope

When you write ‘i =…’ in the ‘for’ loop, you’re creating a new variable. That variable is used for one purpose: keeping track of how many times the ‘for’ loop has run. Because it has such a limited purpose, it’s typical to just use ‘i’ as the name, instead of typing out ‘counter’ or ‘forLoopVariable’ or something that might be more descriptive, but harder to type out.

In this program, you are creating the ‘i’ variable twice, once for each ‘for’ loop. Because the variable is created inside the ‘for’ loop, it only exists inside that ‘for’ loop and it gets recreated each time the ‘for’ loops run. This concept that the variable can ‘exist’ some places but not others within the same program is called the variable’s scope.

Quiz

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 four parts of a 'for' loop, in order of how they're typed? Hint: Use your purple cards!