Sounding Off
Part 2
Build A Music-Controlled Arduino Tunebot
by Gordon McComb
In the classic movie, Close Encounters
of the Third Kind, the (hopefully)
friendly aliens start communicating
with us lowly humans with a series of
five musical notes. We assume these
five notes mean “sure, we come in
peace,” however, since they never
made a sequel to the film, for all we
know they were really saying “we
prefer earthlings medium rare.”
Brain-eating space invaders or not, music has long served as a form of language. Music transcends
generations and cultures – most of us would have difficulty
reading a book written during the time of James I of
England — in Latin, no less. Yet, we can all follow the music
of the time. It may not be rock and roll, but you can still
dance to harpsichords and lutes. Dig them crazy minstrels!
So, too, your robot can communicate using music. Last
month, we looked at producing simple musical tones with
just the Arduino and a variety of low cost sound coprocessor chips. Trouble is, the music quality of these
sources is anything but Hi-Fi. With a low cost synthesizer
board, however, you can turn your robot into an orchestra
on wheels. Play single notes, chords, sound effects, even
FIGURE 1. The Tunebot is built on an ArdBot platform,
or most any Arduino-based robot will do.
54 SERVO 04.2012
Discuss this article in the SERVO Magazine
forums at http://forum.servomagazine.com.
musical invitations to dinner with extraterrestrials.
In this installment of Sounding Off, you’ll learn how to
add a repertoire of realistic sounding instruments to your
robot by using MIDI. Though I’ll concentrate on
programming the MIDI sound using an Arduino, the same
techniques apply to any microcontroller you’d like to use. I’ll
call the musically gifted robot described here as Tunebot;
see Figure 1 for what it looks like.
Before we begin, last month I said we’d cover both MP3
and MIDI sound making. Alas, the finished article was way
too long, so I’ll concentrate on MIDI sound this time, and save
MP3 for the next installment. Fair enough? Good! Let’s begin.
Much Ado About MIDI
MIDI stands for Musical Instrument Digital Interface.
It’s a standard method for controlling electronic instruments
– it does other jobs, too, but music is what we’re interested
in here. The MIDI specification covers the data transmission
itself, the electrical connection, even the hardware used to
link everything together.
For a self-contained robot, we’re mainly interested in
the data talking part. MIDI speaks by sending short
messages over an asynchronous serial connection.
• The data sender is referred to as a controller. A common
MIDI controller is an electronic keyboard but there are many
other kinds; for the Tunebot, the controller is an Arduino.
• The data receiver is referred to by various names such as
sound module, synthesizer, or sound bank. Its job is to
listen for commands sent by the controller and turn them
into musical notes. The sound module is connected to an
amplifier and speaker so you can hear the music.
Most messages are only two or three bytes long. Each
message starts with an eight-bit command (or status) byte,
followed by one or more seven-bit data (or parameter) bytes. The
combination of command and data byte(s) of a single message
is an event. Figure 2 shows a simplified example of a three-byte
MIDI message. By starting every message with an eight-bit byte
and then only using seven-bit data bytes, the sound module
can more easily keep sync between itself and the controller.
As a programming practice, command bytes are usually
entered in code in hexadecimal (hex) format. On the
Arduino, hex values are preceded with an 0x prefix. For
example, 0xB0 is the same as decimal 176. Data bytes are
entered as decimal or hex values — whichever method you
prefer. I’ll provide some examples shortly.