Simple Arduino Robot Control

One of the simplest bits of functionality that is useful for small-scale robotics is the ability to control robots over the serial port, which can easily be made wireless using Pololu Wixels, XBee, bluetooth, or any other wireless serial connection. Since the Arduino Serial Monitor only sends out messages rather slowly, and it’s usually nice to have a fast response from computer to robot, I wrote a short Java application that handles the communication for you.

Download here:

Robot Controller
Source code is released into the public domain, but if you use it, I’d appreciate it if you cite me somewhere (Robert Ying).

You’ll also need to have the appropriate rxtx native library in the same folder as the jar file when running it—you can download that here

Corresponding Arduino Code:

void setup() {
  Serial.begin(115200);
}

// method that sets motor speeds from -255 to 255
// corresponding to full reverse and full forward
void setLeftMotor(int speed);
void setRightMotor(int speed);

byte nextByte() {
  unsigned long start = millis();
  while (millis() - start < 100) {
    if (Serial.available()) {
      return Serial.read();
    }
  }
  return 0x00;
]

void loop() {
  while (Serial.available()) {
    switch(Serial.read()) {
      case 0xC1: setLeftMotor(2 * nextByte()); break;
      case 0xC2: setLeftMotor(-2 * nextByte()); break;
      case 0xC5: setRightMotor(2 * nextByte()); break;
      case 0xC6: setRightMotor(-2 * nextByte()); break;
      default: setLeftMotor(0); setRightMotor(0); break;
    }
  }
}

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>