Skip to content

Instantly share code, notes, and snippets.

@sbma44
Created December 20, 2013 21:15
Show Gist options
  • Save sbma44/8061648 to your computer and use it in GitHub Desktop.
Save sbma44/8061648 to your computer and use it in GitHub Desktop.
Roomba hello world
/*
* RoombaBumpTurn
* --------------
* Implement the RoombaComm BumpTurn program in Arduino
* A simple algorithm that allows the Roomba to drive around
* and avoid obstacles.
*
* Arduino pin 0 (RX) is connected to Roomba TXD
* Arduino pin 1 (TX) is connected to Roomba RXD
* Arduino pin 2 is conencted to Roomba DD
*
* Updated 20 November 2006
* - changed Serial.prints() to use single print(v,BYTE) calls instead of
* character arrays until Arduino settles on a style of raw byte arrays
*
* Created 1 August 2006
* copyleft 2006 Tod E. Kurt <tod@todbot.com>
* http://hackingroomba.com/
*/
int rxPin = 0;
int txPin = 1;
int ddPin = 2;
int ledPin = 13;
char sensorbytes[10];
#define bumpright (sensorbytes[0] & 0x01)
#define bumpleft (sensorbytes[0] & 0x02)
void setup() {
// pinMode(txPin, OUTPUT);
pinMode(ddPin, OUTPUT); // sets the pins as output
pinMode(ledPin, OUTPUT); // sets the pins as output
Serial.begin(57600);
digitalWrite(ledPin, HIGH); // say we're alive
// wake up the robot
digitalWrite(ddPin, HIGH);
delay(100);
digitalWrite(ddPin, LOW);
delay(500);
digitalWrite(ddPin, HIGH);
// wait 2 seconds, flashing....
for(int i=0;i<20;i++){
digitalWrite(ledPin, LOW);
delay(50);
digitalWrite(ledPin, HIGH);
delay(50);
}
digitalWrite(ledPin, LOW);
// set up ROI to receive commands
Serial.write(128); // START
delay(50);
Serial.write(130); // CONTROL
delay(50);
// wait 2 seconds, flashing....
for(int i=0;i<20;i++){
digitalWrite(ledPin, LOW);
delay(50);
digitalWrite(ledPin, HIGH);
delay(50);
}
digitalWrite(ledPin, LOW);
}
void loop() {
digitalWrite(ledPin, HIGH); // say we're starting loop
// spinRight();
goForward2();
delay(500);
digitalWrite(ledPin, LOW); // say we're starting loop
// spinLeft();
goBackward2();
delay(500);
}
void spinLeft() {
Serial.write(137 ); // DRIVE
Serial.write(0x00); // 0x00c8 == 200
Serial.write(0xc8);
Serial.write(0x00);
Serial.write(0x01); // 0x0001 == spin left
}
void spinRight() {
Serial.write(137); // DRIVE
Serial.write(0x00); // 0x00c8 == 200
Serial.write(0xc8);
Serial.write(0xff);
Serial.write(0xff); // 0xffff == -1 == spin right
}
void goForward() {
Serial.write(137); // DRIVE
Serial.write(0x00); // 0x00c8 == 200
Serial.write(0xc8);
Serial.write(0x80);
Serial.write(0x00);
}
void goBackward() {
Serial.write(137); // DRIVE
Serial.write(0xff); // 0xff38 == -200
Serial.write(0x38);
Serial.write(0x80);
Serial.write(0x00);
}
void goForward2() {
Serial.print(char(137)); // DRIVE
Serial.print(char(0x00)); // 0x00c8 == 200
Serial.print(char(0xc8));
Serial.print(char(0x80));
Serial.print(char(0x00));
}
void goBackward2() {
Serial.print(char(137)); // DRIVE
Serial.print(char(0xff)); // 0xff38 == -200
Serial.print(char(0x38));
Serial.print(char(0x80));
Serial.print(char(0x00));
}
void updateSensors() {
Serial.write(142);
Serial.write(1); // sensor packet 1, 10 bytes
delay(100); // wait for sensors
char i = 0;
while(Serial.available()) {
int c = Serial.read();
if( c==-1 ) {
for( int i=0; i<5; i ++ ) { // say we had an error via the LED
digitalWrite(ledPin, HIGH);
delay(50);
digitalWrite(ledPin, LOW);
delay(50);
}
}
sensorbytes[i++] = c;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment