Datasheet

Assemble and Test your BOE Shield-Bot • Chapter 3
Robotics with the BOE Shield-Bot 91
Programming the Start-Reset Indicator
The next example sketch tests the piezospeaker using calls to the Arduino’s tone function.
True to its name, this function sends signals to speakers to make them play tones.
There are two options for calling the
tone function. One allows you to specify the pin
and
frequency (pitch) of the tone. The other allows you to specify pin, frequency,
and
duration (in milliseconds). We’ll be using the second option since we don’t need the
tone to go on indefinitely.
tone(pin, frequency)
tone(pin, frequency, duration)
This piezospeaker is designed to play 4.5 kHz tones for smoke alarms, but it can also play a
variety of audible tones and usually sounds best in the 1 kHz to 3.5 kHz range. The start-
alert tone we’ll use is:
tone(4, 3000, 1000);
delay(1000);
That will make pin 4 send a series of high/low signals repeating at 3 kHz (3000 times per
second). The tone will last for 1000 ms, which is 1 second. The
tone function continues in
the background while the sketch moves on to the next command. We don’t want the servos
to start moving until the tone is done playing, so the
tone command is followed
by
delay(1000) to let the tone finish before the sketch can move on to servo control.
Frequency can be measured in hertz (Hz) which is the number of times a signal repeats itself in
one second. The human ear is able to detect frequencies in a range from very low pitch (20 Hz) to
very high pitch (20 kHz or 20,000 Hz). One kilohertz (kHz) is one-thousand-times-per-second.
Example Sketch: StartResetIndicator
This example sketch makes a beep when it starts running, then it goes on to send the Serial
Monitor messages every half-second. These messages will continue indefinitely because
they are in the
loop function. If the power to the Arduino is interrupted, the sketch will start
at the beginning again, and you will hear the beep.
Reconnect power to your board.
Create and save the StartResetIndicator sketch, and run it on the Arduino.
If you did not hear a tone, check your wiring and code for errors and try again.
If you did hear an audible tone, open the Serial Monitor (this may cause a reset
too). Then, push the reset button on the BOE Shield.