
38
Firstly,we use a simple procedure to understand how to use the buzzer, and its sound principle. And to
drive a buzzer like singing sound,we need make the buzzer issued frequency and duration of the different
sound . Cycle is equal to the reciprocal of the frequency, so you can know the time by frequency, and then
by calling the delay function or timer to achieve.Similarly the sound duration can also be achieved through
the delay function. So the key factor to make the buzzer sing is to know how much time to prolong! Play
music with Arduino, you just need to understand the two concepts of "tone" and "beat" .
The tone means the frequency at which a note should be sung.
The beat means how long a note should be sung.
The commonly used method is "look-up table method", this method is complex where you have to find
the corresponding frequency of each note (according to the note, the frequency comparison), and then
according to the formula converted to the corresponding time (take half cycle), and then through the delay
function implementation. Finally by programming to achieve.
The whole process is like this:
Firstly, according to the score of Happy Birthday song, convert each tone to the corresponding
frequency.
For example: picture 4-3-3 is the note frequency conversion table, picture 4-3-4 is the Happy Birthday
song score.
void
setup
()
{
pinMode
(
9
,
OUTPUT
);
}
void
loop
()
{
for
(
int
i
=
200
;
i
<=
800
;
i
++)
// 200HZ ~ 800HZ
{
tone
(
9
,
i
);
}
delay
(
1000
);
//Max Frequency hold 1s
for
(
int
i
=
800
;
i
>=
200
;
i
--)
// 800HZ ~ 200HZ
{
tone
(
9
,
i
);
delay
(
10
);
}
}