Figure 7-2
An
int
Advanced I/O
There are some useful little functions that you can use to
make your life easier when
performing various input/output tasks.
Generating Tones
The
tone
function allows you to generate a square-wave signal (see
Figure 7-3
) on one of
the digital output pins. The most common reason to do this is to generate an audible tone
using a loudspeaker or buzzer.
The function takes either two or three arguments. The first argument is always the pin
number on which the tone is to be generated, the second argument is the frequency of the
tone in hertz (Hz), and the optional final argument is the duration of the tone. If no
duration is specified, then the tone will
continue playing indefinitely, as is the case in
sketch 7-03.
This is why we have put the
tone
function call in
setup
rather than in the
loop
function.
Figure 7-3
A square-wave signal
To stop a tone that is playing, you use the function
Dostları ilə paylaş: