Bluetooth and Motor Shield RC car

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.

I meant to post links to the shields we are using:

What does Serial.print(SpeedA); print out, from 0 to full speed?

@HazardsMind
It would print from 0 to 255. As that is the number we send to the analogwrite() for the PWM signal. It is in multiples of 17, as we are only using the last 4 bits to represent speed in the recvByte.

The recvByte is just a character, but we bit mask it to get the data we want out. The MSB is for parity, should we want to enable that. The next 2 bits are for selecting which motor to control (up to 4 motors max). The next 1 bit is for direction (forward or backwards), and the last 4 bits are for speed, 0 to 255.

Well, when does it occur? At around full speed?

worse over time, up to like 5 second delay

Question, do you know if your clearing what you send after you send it? This here sounds like your memory is filling up and is slowing down either the android or the arduino. And being that your not adding anything to an array or String here, the problem must lie with the android.

What I suggest is to uncomment your serial prints and see exactly where and when this problem occurs. Just send the data to the arduino, you dont need to have the motors attached, and monitor what your sending and processing.

It looks like the 'default' case was causing the motors to zero out when nothing new was sent. I am in the process of rewriting most of the code, so far it looks promising. I didn't notice the default case until I started rewriting the code, the original was written by a teammate/classmate. Once this project is complete I'll post some documentation on how and what we did for others to have some reference.

I guess I never came back to this. Classic internet. Well let me see what I can find from a year ago:

Phase 1:

Phase 2:

Phase 3:

Tutorial + project info:

Source Code and Such: