Nov 13, 2011

Sunday night with Bluetooth

What do you do after installing your Christmas lights? Build a robot of course!


This projects started with a modified version of the Boe-Bot on which I added big dirt wheels scavenged from a dead RC car. The simple structure on top is made with my usual LEGO spider-legs system supporting the Arduino and its proto shield. I'm using the Sparfun proto shield since it has a build-in connector for the BlueSmirf Bluetooth breakout board and.. it's red. I also use two power supplies to separate the servos from the rest of the electronics.

Software wise, I used the Sena BTerm app to do some direct communication with the Bluetooth device. This app was very practical since it enabled me to only code on the Arduino side. This made prototyping very fast. The only real work began when I had to make the real Android app using Bluetooth. For that I hacked Google's BluetoothChat demo and after a short time I was using buttons and sliders to communicate with my Arduino. Why was that work? Eclipse...


A great advantage of using an Android device as the communication platform is the real-time outputs. This is very nice when debugging since you don't have to be tether with the laptop.

Edit 1: I later created an Arduino to Arduino link with bluetooth.

Edit 2: Here is similar code a later used for the minirccar



#include  
int leadPin = 3;
//Servo myservo;  // create servo object to control a servo
int servoDir = 90;
int delayTime = 100;
int incomingByte = 0;
int motorState = LOW;
int servoPin = 8;
char gBtMsg[256]; 
void BtReceive(void){
  bool keepReading = true;
  int index = 0;

  gBtMsg[0] = '\0';

  while(keepReading){
    keepReading = false;
 
    if (Serial.available() > 0) {
      // read the incoming byte:
      incomingByte = Serial.read();
      if(incomingByte != 13){
        gBtMsg[index++] = incomingByte;
        keepReading = true;
      }
    }
  }

  gBtMsg[index] = '\0';
void BtSend(char* i_pBtCmd, bool i_ln = true){
  if(i_ln){
    Serial.println(i_pBtCmd);
  } else{
    Serial.print(i_pBtCmd);
  }

  delay(100);
  BtReceive();
void setup()
{
  pinMode(leadPin, OUTPUT);
  pinMode(servoPin,OUTPUT);

  Serial.begin(115200);
  BtSend("$$$", false);
  BtSend("SM,0");
  BtSend("---");
}

void loop()
{
  bool doSomthing = false;
  if (Serial.available() > 0){
    incomingByte = Serial.read();
    doSomthing = true;
  }
  switch(incomingByte){
  case '1':
    servoDir = 1360; //left
    break;
  case '2':
    servoDir = 1580;//90;
    break;
  case '3':
    servoDir = 1800; //right
    break;
  case '4':
    motorState = HIGH;
    break;
  case 'd':
    motorState = LOW;
    break;
   default:
     doSomthing = false;
  }
  for (int temp = 0; temp <= 2 && (doSomthing || (motorState == HIGH)); temp++)
  {
    digitalWrite(servoPin,HIGH);
    delayMicroseconds(servoDir);
    digitalWrite(servoPin,LOW);
    delay(25);
  }

  digitalWrite(leadPin, motorState);

  delay(delayTime);
}