Page 177 - Hacking Roomba
P. 177

158       Part II — Fun Things to Do





                             The core of RoombaTune in Listing 8-3 is the keyPressed() method. The switch(key){}
                             statement maps the computer keyboard to note number offset. Then if a note has been hit, the
                             note number offset is converted to the appropriate note number based on the current octave
                             setting and is played via the new playNote() command.
                             The RoombaTune sketch in Listing 8-3 is very minimal and not very Processing-like. Note
                             that it doesn’t do any of the normal things in setup() like set the frame rate or sketch size. It
                             also has a blank draw() method. RoombaTune turns the computer keyboard into a musical
                             keyboard to play Roomba. One simple and fun change you could do would be to draw lines or
                             circles based on a note number. RoombaTune then becomes a music-activated screensaver.
                             To add even more musical control, you can add more functions to the unused keys of the com-
                             puter keyboard to add turning on and off the vacuum motors, drive motors, and LEDs.


                               Listing 8-3: RoombaTune Live Instrument Processing Sketch

                               String roombacommPort = “/dev/cu.KeySerial1”;  // or COM3, etc.
                               void setup() {
                                 if( !roombacomm.connect(roombacommPort) ) {
                                   println(“couldn’t connect. goodbye.”);  System.exit(1);
                                 }
                                 println(“Roomba startup”);
                                 roombacomm.startup();
                                 roombacomm.control();
                                 roombacomm.pause(50);
                               }
                               void draw() {
                               }
                               void keyPressed() {
                                 int note=-1; // -1 means no note yet
                                 switch( key ) {
                                 // pseudo-keyboard to play notes on
                                 case ‘a’: note = 0; break;
                                 case ‘w’: note = 1; break;
                                 case ‘s’: note = 2; break;
                                 case ‘e’: note = 3; break;
                                 case ‘d’: note = 4; break;
                                 case ‘f’: note = 5; break;
                                 case ‘t’: note = 6; break;
                                 case ‘g’: note = 7; break;
                                 case ‘y’: note = 8; break;
                                 case ‘h’: note = 9; break;
                                 case ‘u’: note =10; break;
                                 case ‘j’: note =11; break;
                                 case ‘k’: note =12; break;
                                 case ‘o’: note =13; break;
                                 case ‘l’: note =14; break;
                                 case ‘z’: octave--; break;  // change octaves
                                 case ‘x’: octave++; break;  // change octaves
                                 }
                                 // we actually hit a note key
   172   173   174   175   176   177   178   179   180   181   182