noises. For us to create a recognizable tune, we need
to control the musical notes (tones) and the duration
between the notes (beat). For simple tunes, we can live
with eight tones (a music octave) each having a specific
frequency. Each of these tones has a letter ‘note’ assigned
to it by musicians as in Figure 6.
We will keep this as simple as possible and generate
these notes using the Arduino library delayMicroseconds
function. (A microsecond is 1/1,000,000 second — yes:
one millionth of a second — you have heard that
computers are fast, haven’t you?)
To generate the ‘c’ note, we create an output
waveform (see Figure 7) that turns on and off with a
frequency of 261 cycles per second. Each of these on/off
cycles occurs in 1/261 of a second or 0.003831 seconds.
Since we are dealing with microseconds, we multiply this
by 1,000,000 to get 3,831 microseconds per cycle. Since
we need to cycle the pin (turn the pin on and off) in that
time, we turn it on for 3831/2 = 1,915 microseconds
(throwing away the fractional part) and off for 1,915
microseconds giving us a total of 3,830. We lost 1 due
to our not wanting to use fractions, but who is going to
miss a microsecond?
In the Tunes program, we use a play Tone function that
takes the tone and the duration as parameters. A loop
repeats the on/off cycle for the note parameter for a
length of time in the duration parameter. It might, for
instance, turn the speaker on for 1,915 uS and off for
1,915 uS repeating for a full second to give a rather long
‘c’ note. The play Tone function is called by the playNote
function that has the job of reading through the tune array
to get the next note/duration combination.
Each tune is played by an individual function that
contains two arrays: one for the tune notes and one for
the tune beat. It calculates the duration from the beats
and sends that duration along with the tune note to the
playNote function. The playNote function reads through
an array of note names and uses that name position in
that array to get the number for the microseconds needed
to play that note. It then calls play Tone with the tone
microseconds and the duration as parameters.
The play Tone uses those parameters to turn the
pin connected to the piezo element on and off, thus
generating exquisite music like none heard since the last
pterodactyl blundered into a fern tree.
Tunes Source Code
The Tunes source code is shown here in TAW
abridged from the full code that is available along with
the ACW version in Workshop13.zip. It is shortened since
there is a lot of repetition in how the tunes are played.
Twinkle, Twerdle, Euro Siren, and Beep Beep are in the zip
file. AND a note to more experienced programmers: Yes,
I know this isn’t the ‘best’ way to do this, but this is
instructional code for novices. I have some comments
with the zipped code about better ways and I will
SMILEY’S WORKSHOP ☺
show those ways in a future workshop.
/* TAW Tunes
Joe Pardue May 13, 2009
based on Arduino example code Melody
www.arduino.cc/en/Tutorial/Melody
(cleft) 2005 D. Cuartielles for K3
*/
// define numbers for tunes
#define Twinkle 0
#define Happy_Birthday 1
#define Euro_Siren 2
#define Twerdle 3
#define Beep_Beep 4
// create and intialize global variables
int speakerPin = 9; // pin to drive the piezo
element
int tune = 0; // tune to play
void setup() {
Serial.begin(9600);
pinMode(speakerPin, OUTPUT);
// greetings
Serial.println(“TAW Tunes”);
Serial.println(“Enter 0 for Twinkle Twinkle
Little Star”);
Serial.println(“Enter 1 for Happy Birthday”);
Serial.println(“Enter 2 for Euro Siren”);
Serial.println(“Enter 3 for Twerdle Alarm”);
Serial.println(“Enter 4 for Beep Alarm”);
}
void loop() {
// check if data has been sent from the
// computer
if (Serial.available()) {
cmdParse(); // if true, get the data and
parse it
}
■ FIGURE 7. ‘C’
note waveform.
August 2009 83