Writing three tones to a speaker is easy when there are built-in functions to help. The tone() function works by entering the pin that you want to send the tone to, then the frequency of the tone, measured in hertz.
Read the Code Walkthrough Text (click to open)
Code Concept: Code Structure
How code is structured varies based on language and coding environment. In the programs you'll write with Let's Start Coding, there are two code structures that will always exist: void setup() and void loop().
Technically, both of these are functions that you define each time you write a program. Neither of these functions requires any arguments, so nothing goes into the parentheses next to each function's name.
The statements between the opening and closing curly braces of the functions are their contents. They are also what separate these two functions from each other. For example, you must close the void setup() before the code can run the void loop().
The void setup() function almost always contains the pinMode() functions that set up your project. It's very handy to have them in one place. You don't need to tell Maker Board that a pin is an OUTPUT during each loop of the code- you can just do it once in the void setup(). You can also put in a delay or signal in the void setup() section that will run only once when the Maker Board receives power.
The void loop() function usually contains the majority of your code and it repeats over and over until there is no more power to Maker Board. The Maker Board will run the loop as fast as possible, which is up to millions of times per second! Remember that when you have mysterious troubleshooting problems- the code is running much faster than you can track it. The loop is also bounded by opening and closing curly braces. Don't forget to close your loop or you'll get a strange error!
A final note: for many of our programs, the void setup() and void loop() lines and their corresponding closing curly braces are the only things completely un-indented in the program. Why? Because it's easy to get confused when everything is indented to the same level. Imagine a book without any paragraphs or chapters!
Make a habit of indenting your statements within the void setup and void loop- it will save you trouble later.
New Concept: Multiple Functions in a Program
Although it moves quickly, a computer still 'reads' your program from top to bottom. It makes a difference what comes first in a program. For example, writing tone(A5,200); and then immediately after writing tone(A5,300); will leave you hearing a tone of 300 hertz. Why? Because the second function (tone(A5,300);) overwrote the first function immediately.
Keep this effect in mind. Try to read your code from top to bottom like the computer does and you may find silly errors hidden in a program.
1. What symbols (called syntax) begin and end the void setup function?
2. What syntax begins and ends the void loop function?
3. If you type:
how long will you hear the tone of 500 hertz?