Page 308 - Hacking Roomba
P. 308

Chapter 13 — Giving Roomba a New Brain and Senses                    289




                               Listing 13-3 Continued

                                 pinMode(ddPin,  OUTPUT);    // sets the pin as output
                                 pinMode(ledPin, OUTPUT);    // sets the pin as output
                                 Serial.begin(57600);
                                 digitalWrite(ledPin, HIGH); // say we’re alive
                                 // wake up the robot
                                 digitalWrite(ddPin, LOW);
                                 delay(100);
                                 digitalWrite(ddPin, HIGH);
                                 delay(2000);
                                 // set up ROI to receive commands
                                 Serial.print(128, BYTE);    // START
                                 delay(50);
                                 Serial.print(130, BYTE);    // CONTROL
                                 delay(50);
                                 digitalWrite(ledPin, LOW);  // say we’ve finished setup
                               }

                               void loop() {
                                 digitalWrite(ledPin, HIGH); // say we’re starting loop
                                 updateSensors();
                                 digitalWrite(ledPin, LOW);  // say we’re after updateSensors
                                 if( bumpleft ) {
                                   spinRight();
                                   delay(1000);  // spinning @ 200 for 1 sec == ~90 degrees
                                 }
                                 else if( bumpright ) {
                                   spinLeft();
                                   delay(1000);
                                 }
                                 goForward();
                               }
                               void goForward() {
                                 char c[] = {137, 0x00,0xc8, 0x80,0x00};    // 0x00c8 == 200
                                 Serial.print( c );
                               }
                               void goBackward() {
                                 char c[] = {137, 0xff,0x38, 0x80,0x00};    // 0xff38 == -200
                                 Serial.print( c );
                               }
                               void spinLeft() {
                                 char c[] = {137, 0x00,0xc8, 0x00,0x01};
                                 Serial.print( c );
                               }
                               void spinRight() {
                                 char c[] = {137, 0x00,0xc8, 0xff,0xff};
                                 Serial.print( c );
                               }

                                                                                              Continued
   303   304   305   306   307   308   309   310   311   312   313