Half Siren: Tone Loops

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!

Code

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.

// Create a rising tone on the Speaker void setup(){ pinMode(10,OUTPUT); //Set the speaker as an OUTPUT } void loop(){ for(int i=40; i<800; i++){ //start at 40 hertz and play a new tone up to 800 hertz tone(10,i); //play the tone that is equal to the variable 'i' delay(3); //delay 3 milliseconds before starting the 'for' loop again } } // (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: 'for' loop / limited looping

Up to this point, your void loops have been stopped by only two things- uploading new code or unplugging the Code Speaker. This might have been extremely annoying on the last project as the pitch variable climbed closer and closer to a dog whistle!

In this program, you see how you can limit loops with the 'for' loop. These will give you more control over how your code runs.

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!