Next, you'll modify the "Hello World" app so your robot makes a "beep" sound with its speaker whenever the D13 LED blinks.
You'll need to create a global variable to store the pin number of the speaker (buzzer) that should be connected to I/O pin 9 on the RedBot's circuit board.
Add this code statement before the setup()
function:
Your code should now have two separate code statements before the setup()
function to declare a global variable for the LED and a global variable for the speaker.
Like the LED light, the speaker is also an output because your app will send signals to the speaker to produce sound. So you'll need to set the pin mode for the speaker to be an output.
Add this code statement within the setup()
function:
Your code should now have two separate code statements within the setup()
function to set the pin mode for the LED and set the pin mode for the speaker.
The speaker can only play one tone (sound) at a time. The tone()
method is used to produce a sound of a specific frequency (pitch).
Add this code statement (as a separate line of code) within the loop()
function (after the first digitalWrite()
statement, which turns on the LED):
The tone()
method requires two parameters inside its parentheses (in this order):
The I/O pin number of the speaker, which can be the actual pin number (such as: 9
, etc.) or a variable that stores a pin number. In this case, the variable speaker
is listed (which has a value equal to 9
).
The frequency for the tone, which can be an integer value (whole number) or a variable that stores an integer. The frequency value can be between 20-20000 hertz. Lower numbers will have a lower pitch, while higher numbers will have a higher pitch. In this case, the frequency will be 2000
hertz.
VOLUME: There isn't a way to change the volume of a tone produced by the speaker. However, you will notice that certain frequencies will naturally seem louder to your ears.
When you use the tone()
method in your app code to produce a sound, the speaker will keep playing the sound until you use a separate method in the code to stop the sound.
Let's stop the speaker sound when the LED is turned off.
Add this code statement (as a separate line of code) within the loop()
function (after the second digitalWrite()
statement, which turns off the LED):
The tone()
method requires just one parameter inside its parentheses:
The I/O pin number, which can be the actual pin number (such as: 9
, etc.) or a variable that stores a pin number. In this case, the variable speaker
is listed (which has a value equal to 9
).
Modify the first delay()
method within the loop()
function, so the delay will be 200
milliseconds. This will allow the LED and speaker to blink and beep for only 0.2 seconds.
Modify the second delay()
method within the loop()
function, so the delay will be 1500
milliseconds. This will create a 1.5 second pause between each blink/beep.
If your robot is still connected to your computer and powered on, you should be able to upload the modified app by clicking the Upload icon (looks like a right arrow) at the top of the code editor panel. The code editor will automatically save and verify the app before uploading it to your robot.
Otherwise, if you disconnected your robot from your computer, be sure to:
Connect Robot to Computer
Turn on Robot Power
Select Correct Board and Port
Upload App to Robot
Once the modified app is uploaded to your robot, confirm that it works correctly. The robot's LED and speaker should briefly blink and beep (in sync) in a repeating pattern (with 1.5 second pause between each blink/beep).