FIGURE 9. The Parallax line follower
module contains eight modulated
infrared emitters and IR receivers.
Tunebot uses it instead as a piano
keyboard — playing the keyboard
operates the robot.
can use a constant like
CTRL_INSTRUMENT which
mnemonically helps you to
remember what the value does.
Constants are defined in an
Arduino sketch using either of two
methods:
const int SOME_VALUE = 5;
or
#define SOME_VALUE 5
noteOn, or otherwise the notes will just congeal into one
another. (This continues until the MIDI chip runs out of
the internal resource space.)
The first argument of the noteOn/noteOff methods is
the channel. I’m using Channel 0, but there’s nothing
stopping you from using another channel. The second
argument is the pitch; in this case, it’s middle C on the
piano. The third argument is the velocity. I’m using the
highest value possible: 127.
Both do the same thing: They
define a symbol that evaluates to a
number. When the sketch is
compiled, the Arduino substitutes each instance of
SOME_VALUE with the number 5. Constants are similar to
variables in that both let you reference a value by name.
Unlike variables, however, constant definitions don’t take
up memory in the Arduino. Take notice that when using the
#define statement, there’s no semi-colon at the end of the line.
Listing 2 demonstrates the same functionality as the
previous sketch, but uses constants to refer to the
command and data bytes. While the sketch is longer, it
takes up the same amount of program space inside the
wee innards of the Arduino.
Using Self-Descriptive
Constants
Playing Multiple Notes
Programming MIDI is made easier by using constants
instead of raw hexadecimal or decimal values. Rather than
trying to remember that 0xC0 switches instruments, you
The VS10xx chips are polyphonic, meaning they can
play more than one note at a time (conversely, monophonic
means one note at a time). You can play multiple notes on
the same channel or on a
different channel. If the notes
are from the same instrument,
it’s easier to use the same
channel. Simply repeat the
noteOn statement for each
pitch you wish to play.
Conversely, if you wish to
play notes from different
instruments, you’ll need to
specify an instrument on each
channel. Then, call noteOn for
each note, referencing the
channel you wish to use.
FIGURE 10. A lightweight
capsule speaker is connected to
the MIDI shield by way of the
interface circuit in Figure 8.
60 SERVO 04.2012