We have a fairly completed project where we wanted to do a type of RC car. It will be an Arduino with a motor shield and a bluetooth shield that is controlled wirelessly via bluetooth on an Android phone. We have control of the RC car and can steer and such, but have some bugs we haven't been able to figure out. The motors seem to have a momentary pause and they don't go at the correct PWM signal that we are sending. This seems to be correlated to the rate at which we send our signal from our Android phone. The faster we send the less we see the PWM signal to the motors mess up, so we attempted to constantly send the bluetooth signal as often as possible on the Android phone. Sending as fast as possible caused the signal to buffer, so that the Arduino wouldn't react to our changes in steering for a second or two, and would get worse over time, up to like 5 second delay. We can't find a good amount to delay for our bluetooth signal to be sent while not slowing down our controls.
My ideas on what is wrong with our project are the following:
Mainly: The Arduino has some sort of interrupt on blueToothSerial.read() that is stopping the PWM.
Secondly: The Android or Arduino is buffering the signal on one of the devices causing the delay.
We can turn in what we have so far, but I would really like to figure out how to fix this problem. Any ideas are appreciated.
Here is the Arduino code so far.
#include <SoftwareSerial.h>
#define RxD 6
#define TxD 7
SoftwareSerial blueToothSerial(RxD,TxD);
//motor A connection constants
const int
PWM_A = 3,
DIR_A = 12,
BRAKE_A = 9;
//motor B connection constants
const int
PWM_B = 11,
DIR_B = 13,
BRAKE_B = 8;
//control constants
const byte PARITY = B10000000;
const byte MOTOR_SEL = B01100000;
//00100000 right motor on
//01000000 left motor on
const byte MOTOR_BACK = B00010000; //forward 0 backward is 1 //signed bit
const byte MOTOR_SPEED = B00001111;
int Direction;
char recvByte;
char recvByteTemp;
int SpeedA;
int SpeedB;
void setup()
{
//bluetooth
Serial.begin(9600);
pinMode(RxD, INPUT);
pinMode(TxD, OUTPUT);
setupBlueToothConnection();
//motors
pinMode(BRAKE_A, OUTPUT); //Brake pin on channel A
pinMode(DIR_A, OUTPUT); //Direction pin on channel A
pinMode(BRAKE_B, OUTPUT); //Brake pin on channel B
pinMode(DIR_B, OUTPUT); //Direction pin on channel B /*
pinMode(PWM_A, OUTPUT);
pinMode(PWM_B, OUTPUT);
digitalWrite(BRAKE_A, LOW);
digitalWrite(BRAKE_B, LOW); //Setting the brake for the right motor off
recvByte = B00000000;
// temp = true;
}
void loop() {
//char recvByte;
//checking to see if data sent was sent from bluetooth
recvByteTemp = blueToothSerial.read();
if( recvByteTemp != B00000000 ){
recvByte = recvByteTemp;
}
//Serial.print(recvByte);
//Serial.print("<-- RecvByte\n");
if((recvByte & MOTOR_BACK) == MOTOR_BACK){
Direction = LOW;
}
else{
Direction = HIGH;
}
//sets appropriate motor
switch(recvByte & MOTOR_SEL)
{
//want the right motor on
//Right motor is hooked up to A
case B00100000:
//setting Direction
SpeedA = (recvByte & MOTOR_SPEED) * 17;
Serial.print(SpeedA);
Serial.print("<-- SPeedA\n");
digitalWrite(DIR_A, Direction);
//setting speed
analogWrite(PWM_A, SpeedA);
break;
//We want Left Motor on
//Its hooked up to B
case B01000000:
//Setting Direction
SpeedB = (recvByte & MOTOR_SPEED) * 17;
Serial.print(SpeedB);
Serial.print("<-- SpeedB\n");
digitalWrite(DIR_B, Direction);
//Setting speed
analogWrite(PWM_B, SpeedB);
break;
default:
analogWrite(PWM_B, 0);
analogWrite(PWM_A, 0);
digitalWrite(DIR_B, 0);
digitalWrite(DIR_A, 0);
}
}
void setupBlueToothConnection()
{
blueToothSerial.begin(38400); //Set BluetoothBee BaudRate to default baud rate 38400
blueToothSerial.print("\r\n+STWMOD=0\r\n"); //set the bluetooth work in slave mode
blueToothSerial.print("\r\n+STNA=SeeedBTSlave\r\n"); //set the bluetooth name as "SeeedBTSlave"
blueToothSerial.print("\r\n+STOAUT=1\r\n"); // Permit Paired device to connect me
blueToothSerial.print("\r\n+STAUTO=0\r\n"); // Auto-connection should be forbidden here
delay(2000); // This delay is required.
blueToothSerial.print("\r\n+INQ=1\r\n"); //make the slave bluetooth inquirable
Serial.println("The slave bluetooth is inquirable!");
delay(2000); // This delay is required.
blueToothSerial.flush();
}
P.S. I figured this was more appropriate in the Project Guidance than the other subforums, let me know if I chose incorrectly.