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 send 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 (Hzwhich 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 is one-thousand-times-per-second, abbreviated 1 kHz.