Shop OBEX P1 Docs P2 Docs Learn Events
Android Controlled Bot — Parallax Forums

Android Controlled Bot

CJBCJB Posts: 1
edited 2013-12-03 14:25 in Robotics
The objective of my project is to create a bot which has the brain of an Arduino and is controlled by Android via Bluetooth. The hardware I am using is as follows:
-RN42 Bluetooth Module by Parallax
-Arduino Uno
-Parallax BOEShield for Arduino robot kit


Bluetooth - Arduino Connections:
GND - GND
VCC - 5V
TX - RX (pin 4, SoftwareSerial)
RX - TX (pin 5, SoftwareSerial)
*tied together CTS and RTS pins on Bluetooth module since they are not in use


I used SoftwareSerial to open the Bluetooth port because of the problems associated with using Arduino RX(0)/TX(1) pins.
#include <Servo.h>
#include <SoftwareSerial.h>


Servo servoLeft;       // Declare left and right servos
Servo servoRight;


SoftwareSerial btSerial(4, 5);    //(RX, TX)


int incomingByte =0;      // a variable to read incoming serial data into


void setup() 
{
  // initialize serial communication:
  Serial.begin(9600);
  
  servoLeft.attach(13);
  servoRight.attach(12);
  
  btSerial.begin(115200);        // Default Baud Rate of RN42 Bluetooth module
  delay(320);                     // IMPORTANT DELAY! (Minimum ~276ms)
  btSerial.print("$$$");         // Enter command mode
  delay(15);                      // IMPORTANT DELAY! (Minimum ~10ms)
  btSerial.println("U,9600,N");  // Temporarily Change the baudrate to 9600, no parity
  btSerial.begin(9600);
}


void loop() 
{
  // see if there's incoming serial data:
  if (btSerial.available() > 0) 
  {
    // read the oldest byte in the serial buffer:
    incomingByte = btSerial.read();
   
  }
    
    // if incoming byte is a f, go forward:
    if (incomingByte == '1') 
    {
      btSerial.println("forward");
      //maneuver(200, 200, 500);
      servoLeft.writeMicroseconds(1700);
      servoRight.writeMicroseconds(1300);
      delay(1000);      
    }
    
    // if incoming byte is a D, go backwards:
    else if (incomingByte == '2') 
    {
      //Serial.println("backward");
      maneuver(-50, -50, 500);
      delay(1000);
    }
 
    // if incoming byte is a r, turn right:    
    else if (incomingByte == '3') 
    {
      //Serial.println("right");
      maneuver(-50, 50, 500);
      delay(500);
    }
    
    // if incoming byte is a l, turn left:    
    else if (incomingByte == '4') 
    {
      //Serial.println("left");
      maneuver(50, -50, 500); 
      delay(500);
    }
    
    // if imcoming byte is a S, stop:
    else if (incomingByte == '5')
    {
      //Serial.println("stop");
    }


} // End loop()


void maneuver(int speedLeft, int speedRight, int msTime)
{
  // speedLeft, speedRight ranges: Backward  Linear  Stop  Linear   Forward
  //                               -200      -100......0......100       200
  servoLeft.writeMicroseconds(1500 + speedLeft);   // Set left servo speed
  servoRight.writeMicroseconds(1500 - speedRight); // Set right servo speed
  if(msTime==-1)                                   // if msTime = -1
  {                                  
    servoLeft.detach();                            // Stop servo signals
    servoRight.detach();   
  }
  delay(msTime);                                   // Delay for msTime
}

This code has been adapted from the tutorial.

With this code, the servos jerk when a signal is sent to it. For instance, when the up button is pushed, the Data LED lights up, but the robot does not move. Pushing the button repeatedly, the servos start to jerk a little, like it is attempting to move.

Is there a problem with the data that is being sent to the Arduino from the Bluetooth Serial?

I also tested to see if the batteries was the problem. I added the maneuver outside of the if statements, the robot began to move as soon as power was applied to it.

Comments

  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2013-12-03 14:25
    Unfortunately I am not a C programmer so I am not clear what some of your code is doing. But, if I had to guess I would say that it is not doing what the servos need it to be doing. Servo need to be refreshed every ~20 ms with a pulse value between 1 ms and 2 ms. It looks to me like some of your pulse values are timed for 500 microseconds, which is too long. Assuming delay is in microseconds as well that is too short of a delay and the servos are being over-driven.

    Assuming my guesses are right about what the code is doing that is most likely your problem. If I am off-base then please elaborate and I will attempt to help you as best as I can.
Sign In or Register to comment.