Rising Variable Tone

Use a variable to change your tone every 3 milliseconds (3/1000th of a second!) and see how high of a tone you can hear!

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.

//Use a variable to increase the pitch on Speaker every time the loop runs int pitch = 40; //create an integer variable named 'pitch'. Start its value at 40 void setup(){ pinMode(10,OUTPUT); //set the speaker as an OUTPUT } void loop(){ tone(10,pitch); //Play 'pitch' on the speaker. On the first loop, it will equal 40 delay(3); //delay 3 milliseconds with a pitch playing pitch = pitch + 10 ; //increase pitch variable by 10 before restarting the loop } // (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: Variables that change, part two

This project is another example of using a variable in place of a number that changes each time the loop runs. Start thinking about how you can turn other ‘hard-coded’ numbers into variables and the creative ways they might change as your code runs. You can do any math to a variable that you can do to a number, including addition, subtraction, multiplication, and division.

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 is the value of 'pitch' after the loop has run 5 times?