Page 174 - Hacking Roomba
P. 174
Chapter 8 — Making Roomba Sing 155
C# D# F# G# A# C# D#
C D E F G A B C D E
Octave 1 3 6 8 10
Offset 0 2 4 5 7 9 11
49 51 54 56 58
Octave 4
48 50 52 53 55 57 59
61 63 66 68 70
Octave 5
60 62 64 65 67 69 71
A4 = 440 Hz
73 75 78 80 82
Octave 6
72 74 76 77 79 81 83
FIGURE 8-3: Some musical note numbers for use in the SONG command
PLAY Command
Once a song is defined, it can be played with the PLAY command. If you try to play a song
that hasn’t been defined, Roomba does nothing. In contrast to the SONG command, PLAY
is a simpler command, taking a single argument, the song number 1-16.
Songs play immediately upon reception of the PLAY command. Sending a PLAY command
while a song is in progress stops the first one from playing and starts the second song.
Creating a Song in RoombaComm
Say you want to define and play a song for song slot #1. The song is to be the three notes of a
C-major chord (C-E-G) in the 4th octave, and each note should last half a second. You could
do it by hand using RoombaComm.send() like so:
byte songcmd[] = {(byte)SONG, 1, 3, 48,32, 52,32, 55,32 };
roombacomm.send( songcmd );
byte playcmd[] = {(byte)PLAY, 1};
roombacomm.send( playcmd );
This is a little ungainly. It’s also dangerous because the song length must be calculated accurately
or the ROI will read too many or too few bytes and possibly end up interpreting your other
commands as song notes. The above can be made a little better through some new methods
added to RoombaComm, as in Listing 8-1.