Page 182 - Hacking Roomba
P. 182
Chapter 8 — Making Roomba Sing 163
FIGURE 8-6: RoombaMidi as part of a virtual studio
Translating MIDI Notes into Roomba Actions
One part of RoombaCommPanel not mentioned earlier is the playMidiNote() method. It
is shown in Listing 8-5. Structurally it is very similar to the keyPressed() method of the
Processing sketches in this and the previous chapter. The method accepts two parameters: a
MIDI note number and a note velocity. In MIDI, note velocity is basically how loud the note
should be. Because Roomba can’t vary note loudness, in RoombaMidi the note velocity is
mapped to note duration: a high velocity makes for a longer note. Although MIDI defines an
event for both note-on and note-off messages, note-on with a zero velocity is interpreted as
note-off, too. The playMidiNote() method utilizes that fact to make implementation easier.
The first if() clause handles the case where the note falls within the range playable by the
Roomba piezo beeper. The next if() clause turns the vacuum on or off if C0 is played. The
one after that blinks all the LEDs if C#0 is played. The final two if() clauses spin Roomba
left or right, for MIDI notes E0 or F0, respectively. The speed of the spin and the brightness
of the Power LED are determined by the MIDI velocity.