Getting Started

(lily) #1

Chapter 8: C Pointers and Arrays


tion

is the
should be played. Play_Tune gets the

to the Duration variable and the Timer1
its. When called again by Timer0, if the
ed and the function exits leaving the tone
d to 0, Play_Tune gets the next set of
Duration and the timer and starts the next tone. If the Duration
es that the tune has been played through,
xt byte and if that byte is 1, it starts the tune over, if 0 it ends
une. Clever, eh?

ray – Fur Elise
e[] PROGMEM=

, 8,xd2, 8,e2, 8,b1, 8,d2, 8,c2, 4,a1,
8 e1, 8,a1, 4,b1, 8,p, 8,e1, 8,xg1, 8,b1, 4,c2,
, 8,xd2, 8,e2, 8,xd2, 8,e2, 8,b1, 8,d2,
8,c1, 8,e1, 8,a1, 4,b1, 8,p, 8,e1, 8,c2,
8,b1, 4,a1,
0, 1

to make sound

e large black square on the back of the Butterfly. It
at deforms when electricity is applied to it (the
ation can be made at audio frequencies allowing
nd waves in the air. Our piezo-element is connected to
the OC1A pin that can be configured as an output for
Timer1 waveform generator. We will configure the Timer1 waveform
ill use PWM to generate tones.

Setting the dura


The duration and the frequency are paired values in the table. The duration
length of time that the following tone
duration
and tone from the table and loads them in
top count. It starts the tone and then ex
Duration is not 0, Duration is decrement
playing. When Duration is decremente
values for the
value read from the table is 0, this indicat
so it checks the ne
the t


An example song ar


const int FurElis
{
3,
8,e2, 8,xd2, 8,e2
8,p, 8,c1, ,
8,p, 8,e1, ,e8 2
8,c2, 4,a1, 8,p,

};

Using the Piezo-element


The piezo-element is th
contains a sheet of material th
piezo electric effect). This deform
the element to produce sou
PortB pin 5, which is also
the
generator so that it w

Free download pdf