Play 3 Tones to Speaker

Play 3 Tones to Speaker Hookup: 1 speaker in pin A5

Topics Covered:

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.

Code


/* * Send 3 rising tones to a speaker */ void setup(){ pinMode(A5,OUTPUT); //Set up your speaker on pin A5 & the other pin in GND //Send a tone of 250 hertz, then 450, then 850, then stop tone(A5,250); //Send a tone of 250 hertz delay(1000); //Wait one second tone(A5,450); //Send a tone of 450 hertz delay(1000); //Wait one second tone(A5,850); //Send a tone of 850 hertz delay(1000); //Wait one second noTone(A5); //Stop the tone delay(1000); //Wait one second } void loop(){ } // (c) 2018 Let's Start Coding. License: www.letsstartcoding.com/bsdlicense
 

Challenges

How many can you complete? Change the code and hardware according to the challenges below. Upload your code to see the effect when you're finished. Complete a challenge? Check it off the list!

Videos


Read the Code Walkthrough Text (click to open)

Concepts

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.

Quiz

If you're having trouble, try to run a program that can help you find the answer.

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:
tone(A5,500);
noTone(A5);

how long will you hear the tone of 500 hertz?