Page 215 - Hacking Roomba
P. 215
196 Part II — Fun Things to Do
Listing 10-2 shows the core of a Processing sketch called RoombaTheremin. As before, the
draw() method calls parseRoombaSensors() and draws a growing vertical line showing
pitch change. Figure 10-6 shows what the sketch looks like after running for a while and per-
forming with it. Figure 10-7 shows the typical way you hold and use the robot when playing it
with RoombaTheremin.
Listing 10-2: RoombaTheremin
int pitch = 70;
int dur = 6;
int pshift = 0;
void draw() {
parseRoombaSensors();
updateRoombaState();
stroke(0); strokeWeight(7);
int x = pitch * 4;
y = (y+1) % height; // keep going down, but loop at height
if( y==0 ) { background(180); } // clear screen if back at
top
line( x,y, x,y+2);
}
void parseRoombaSensors() {
if( roombacomm.cliffLeft() ) pshift++;
if( roombacomm.cliffFrontLeft() ) pshift++;
if( roombacomm.cliffRight() ) pshift -- ;
if( roombacomm.cliffFrontRight() ) pshift -- ;
if( roombacomm.wheelDropLeft() ) pshift++;
if( roombacomm.wheelDropRight() ) pshift -- ;
if( roombacomm.wheelDropLeft() &&
roombacomm.wheelDropRight() &&
roombacomm.wheelDropCenter() ) {
; // all wheels down, do nothing
}
else { // otherwise, play
pitch += pshift; pshift=0;
roombacomm.playNote(pitch, dur);
}
}