Kid Ride On RC Car to be controlled via Bluetooth.

Hello there,
I hope you all doing well and had chance to rest and work on your projects during holidays.
I finally got Arduino kit with few shields and sensors last week and started learning. I got to the point where I have to combine two codes in order to get something that to my understanding is still complex. Right now I am stuck and I hope someone can help me get through this obstacle.
Let me explain first what the project is about. My son who is 15 months old has little ride on car with Remote control. I would like to use Arduino with blue tooth and motor shield to control it instead using remote control that came with car. I added LED lights, and extra bateries so it can run up to 5h at this point.
Now before I take on project that size in terms of voltage/amperage, I wanted to learn how to do it on small RC car. I have purchased Wireless Bluetooth RF Transceiver Module serial RS232 TTL HC-06 for Arduino, and have couple of L293D H drivers. Following the schematic and using the code from site http://www.ardumotive.com/arduino-car
I managed to build this little car on Picture 3 which is working fine for now using Android app from previously mentioned site, and even app from google play store: https://play.google.com/store/apps/details?id=braulio.calle.bluetoothRCcontroller
Now, what I am trying to do is to replace all these wires including L293d Driver on Half-Size Breadboard with nice Adafruit Motor Shield for Arduino v2. This is the newer version from the Adafruit site: http://www.adafruit.com/products/1438
My challenge is to understand how should I modify the code in order to use this shield without losing ability to control the car using same apps via Bluetooth? I am in process of learning Arduino programming, but I think I am still miles away to understand how to resolve this issue. Could anyone point me in right direction?

Once I conquer this part, I probably will need help with choosing proper H-bridge shield for the Ride On RC car that runs on 12V 14AH battery system. The H Bridge for the big RC Ride On Car will need to be able to handle 3 motors, two for the movement and one for direction. I will truly appreciate any suggestion.
Thank you in Advance,
Slavisha

Here is the code that i have been using for small RC car. Code is downloaded from:
http://www.ardumotive.com/arduino-car

/*

//L293 Connection
const int motorA1 = 5; // Pin 2 of L293
const int motorA2 = 6; // Pin 7 of L293
const int motorB1 = 10; // Pin 10 of L293
const int motorB2 = 9; // Pin 14 of L293
//Leds connected to Arduino UNO Pin 12
const int lights = 12;
//Buzzer / Speaker to Arduino UNO Pin 3
const int buzzer = 3 ;
//Bluetooth (HC-06 JY-MCU) State pin on pin 2 of Arduino
const int BTState = 2;
//Calculate Battery Level
const float maxBattery = 8.0;// Change value to your max battery voltage level!
int perVolt; // Percentage variable
float voltage = 0.0; // Read battery voltage
int level;
// Use it to make a delay… without delay() function!
long previousMillis = -100010;// -100010=-10sec. to read the first value. If you use 0 then you will take the first value after 10sec.
long interval = 100010; // interval at which to read battery voltage, change it if you want! (101000=10sec)
unsigned long currentMillis; //unsigned long currentMillis;
//Useful Variables
int i=0;
int j=0;
int state;
int vSpeed=200; // Default speed, from 0 to 255

void setup() {
// Set pins as outputs:
pinMode(motorA1, OUTPUT);
pinMode(motorA2, OUTPUT);
pinMode(motorB1, OUTPUT);
pinMode(motorB2, OUTPUT);
pinMode(lights, OUTPUT);
pinMode(BTState, INPUT);
// Initialize serial communication at 9600 bits per second:
Serial.begin(9600);
}

void loop() {
//Stop car when connection lost or bluetooth disconnected
if(digitalRead(BTState)==LOW) { state=‘S’; }

//Save income data to variable ‘state’
if(Serial.available() > 0){
state = Serial.read();
}

//Change speed if state is equal from 0 to 4. Values must be from 0 to 255 (PWM)
if (state == ‘0’){
vSpeed=0;}
else if (state == ‘1’){
vSpeed=100;}
else if (state == ‘2’){
vSpeed=180;}
else if (state == ‘3’){
vSpeed=200;}
else if (state == ‘4’){
vSpeed=255;}

/Forward*****/
//If state is equal with letter ‘F’, car will go forward!
if (state == ‘F’) {
analogWrite(motorA1, vSpeed); analogWrite(motorA2, 0);
analogWrite(motorB1, 0); analogWrite(motorB2, 0);
}
/Forward Left**/
//If state is equal with letter ‘G’, car will go forward left
else if (state == ‘G’) {
analogWrite(motorA1, vSpeed); analogWrite(motorA2, 0);
analogWrite(motorB1, 200); analogWrite(motorB2, 0);
}
/Forward Right**/
//If state is equal with letter ‘I’, car will go forward right
else if (state == ‘I’) {
analogWrite(motorA1, vSpeed); analogWrite(motorA2, 0);
analogWrite(motorB1, 0); analogWrite(motorB2, 200);
}
/Backward*****/
//If state is equal with letter ‘B’, car will go backward
else if (state == ‘B’) {
analogWrite(motorA1, 0); analogWrite(motorA2, vSpeed);
analogWrite(motorB1, 0); analogWrite(motorB2, 0);
}
/Backward Left**/
//If state is equal with letter ‘H’, car will go backward left
else if (state == ‘H’) {
analogWrite(motorA1, 0); analogWrite(motorA2, vSpeed);
analogWrite(motorB1, 200); analogWrite(motorB2, 0);
}
/Backward Right**/
//If state is equal with letter ‘J’, car will go backward right
else if (state == ‘J’) {
analogWrite(motorA1, 0); analogWrite(motorA2, vSpeed);
analogWrite(motorB1, 0); analogWrite(motorB2, 200);
}
/Left**/
//If state is equal with letter ‘L’, wheels will turn left
else if (state == ‘L’) {
analogWrite(motorA1, 0); analogWrite(motorA2, 0);
analogWrite(motorB1, 200); analogWrite(motorB2, 0);
}
/Right**/
//If state is equal with letter ‘R’, wheels will turn right
else if (state == ‘R’) {
analogWrite(motorA1, 0); analogWrite(motorA2, 0);
analogWrite(motorB1, 0); analogWrite(motorB2, 200);
}
/Lights*****/
//If state is equal with letter ‘W’, turn leds on or of off
else if (state == ‘W’) {
if (i==0){
digitalWrite(lights, HIGH);
i=1;
}
else if (i==1){
digitalWrite(lights, LOW);
i=0;
}
state=‘n’;
}
/Horn sound*****/
//If state is equal with letter ‘V’, play (or stop) horn sound
else if (state == ‘V’){
if (j==0){
tone(buzzer, 1000);//Speaker on
j=1;
}
else if (j==1){
noTone(buzzer); //Speaker off
j=0;
}
state=‘n’;
}
/Stop*****/
//If state is equal with letter ‘S’, stop the car
else if (state == ‘S’){
analogWrite(motorA1, 0); analogWrite(motorA2, 0);
analogWrite(motorB1, 0); analogWrite(motorB2, 0);
}
/Battery******/
//Read battery voltage every 10sec.
currentMillis = millis();
if(currentMillis - (previousMillis) > (interval)) {
previousMillis = currentMillis;
//Read voltage from analog pin A0 and make calibration:
voltage = (analogRead(A0)*5.015 / 1024.0)11.132;
//Calculate percentage…
perVolt = (voltage
100)/ maxBattery;
if (perVolt<=75) { level=0; }
else if (perVolt>75 && perVolt<=80) { level=1; } // Battery level
else if (perVolt>80 && perVolt<=85) { level=2; } //Min ------------------------ Max
else if (perVolt>85 && perVolt<=90) { level=3; } // | 0 | 1 | 2 | 3 | 4 | 5 | >
else if (perVolt>90 && perVolt<=95) { level=4; } // ------------------------
else if (perVolt>95) { level=5; }
Serial.println(level);
}

}

First, repost of code so we can read it:

/*
 * Created by Vasilakis Michalis // 12-12-2014 ver.1
 * Project: Control RC Car via Bluetooth with Android Smartphone
 * http://www.ardumotive.com/arduino-car
 * More information at www.ardumotive.com
 */
 
//L293 Connection   
  const int motorA1  = 5;  // Pin  2 of L293
  const int motorA2  = 6;  // Pin  7 of L293
  const int motorB1  = 10; // Pin 10 of L293
  const int motorB2  = 9;  // Pin 14 of L293
//Leds connected to Arduino UNO Pin 12
  const int lights  = 12;
//Buzzer / Speaker to Arduino UNO Pin 3
  const int buzzer = 3 ;   
//Bluetooth (HC-06 JY-MCU) State pin on pin 2 of Arduino
  const int BTState = 2;
//Calculate Battery Level
  const float maxBattery = 8.0;// Change value to your max battery voltage level!
  int perVolt;                 // Percentage variable
  float voltage = 0.0;         // Read battery voltage
  int level;
// Use it to make a delay... without delay() function!
  long previousMillis = -1000*10;// -1000*10=-10sec. to read the first value. If you use 0 then you will take the first value after 10sec. 
  long interval = 1000*10;       // interval at which to read battery voltage, change it if you want! (10*1000=10sec)
  unsigned long currentMillis;   //unsigned long currentMillis;
//Useful Variables
  int i=0;
  int j=0;
  int state;
  int vSpeed=200;     // Default speed, from 0 to 255

void setup() {
    // Set pins as outputs:
    pinMode(motorA1, OUTPUT);
    pinMode(motorA2, OUTPUT);
    pinMode(motorB1, OUTPUT);
    pinMode(motorB2, OUTPUT);
    pinMode(lights, OUTPUT);
    pinMode(BTState, INPUT);   
    // Initialize serial communication at 9600 bits per second:
    Serial.begin(9600);
}
 
void loop() {
  //Stop car when connection lost or bluetooth disconnected
     if(digitalRead(BTState)==LOW) { state='S'; }

  //Save income data to variable 'state'
    if(Serial.available() > 0){     
      state = Serial.read();   
    }
 
  //Change speed if state is equal from 0 to 4. Values must be from 0 to 255 (PWM)
    if (state == '0') { vSpeed=0; }
    else if (state == '1') { vSpeed=100; }
    else if (state == '2') { vSpeed=180; }
    else if (state == '3') { vSpeed=200; }
    else if (state == '4') { vSpeed=255; }
     
  /***********************Forward****************************/
  //If state is equal with letter 'F', car will go forward!
    if (state == 'F') {
       analogWrite(motorA1, vSpeed); analogWrite(motorA2, 0);
        analogWrite(motorB1, 0);      analogWrite(motorB2, 0);
    }
  /**********************Forward Left************************/
  //If state is equal with letter 'G', car will go forward left
    else if (state == 'G') {
       analogWrite(motorA1, vSpeed); analogWrite(motorA2, 0); 
        analogWrite(motorB1, 200);    analogWrite(motorB2, 0);
    }
  /**********************Forward Right************************/
  //If state is equal with letter 'I', car will go forward right
    else if (state == 'I') {
         analogWrite(motorA1, vSpeed); analogWrite(motorA2, 0);
        analogWrite(motorB1, 0);      analogWrite(motorB2, 200);
    }
  /***********************Backward****************************/
  //If state is equal with letter 'B', car will go backward
    else if (state == 'B') {
       analogWrite(motorA1, 0);   analogWrite(motorA2, vSpeed);
        analogWrite(motorB1, 0);   analogWrite(motorB2, 0);
    }
  /**********************Backward Left************************/
  //If state is equal with letter 'H', car will go backward left
    else if (state == 'H') {
       analogWrite(motorA1, 0);   analogWrite(motorA2, vSpeed);
        analogWrite(motorB1, 200); analogWrite(motorB2, 0);
    }
  /**********************Backward Right************************/
  //If state is equal with letter 'J', car will go backward right
    else if (state == 'J') {
       analogWrite(motorA1, 0);   analogWrite(motorA2, vSpeed);
        analogWrite(motorB1, 0);   analogWrite(motorB2, 200);
    }
  /***************************Left*****************************/
  //If state is equal with letter 'L', wheels will turn left
    else if (state == 'L') {
       analogWrite(motorA1, 0);   analogWrite(motorA2, 0);
        analogWrite(motorB1, 200); analogWrite(motorB2, 0);
    }
  /***************************Right*****************************/
  //If state is equal with letter 'R', wheels will turn right
    else if (state == 'R') {
       analogWrite(motorA1, 0);   analogWrite(motorA2, 0);
        analogWrite(motorB1, 0);   analogWrite(motorB2, 200);       
    }
  /************************Lights*****************************/
  //If state is equal with letter 'W', turn leds on or of off
    else if (state == 'W') {
      if (i==0){ 
         digitalWrite(lights, HIGH);
         i=1;
      }
      else if (i==1){
         digitalWrite(lights, LOW);
         i=0;
      }
      state='n';
    }
  /**********************Horn sound***************************/
  //If state is equal with letter 'V', play (or stop) horn sound
    else if (state == 'V'){
      if (j==0){ 
         tone(buzzer, 1000);//Speaker on
         j=1;
      }
      else if (j==1){
         noTone(buzzer);    //Speaker off
         j=0;
      }
      state='n'; 
    }
  /************************Stop*****************************/
  //If state is equal with letter 'S', stop the car
    else if (state == 'S'){
        analogWrite(motorA1, 0);  analogWrite(motorA2, 0);
        analogWrite(motorB1, 0);  analogWrite(motorB2, 0);
    }
  /***********************Battery*****************************/
  //Read battery voltage every 10sec.
    currentMillis = millis();
    if(currentMillis - (previousMillis) > (interval)) {
       previousMillis = currentMillis;
       //Read voltage from analog pin A0 and make calibration:
       voltage = (analogRead(A0)*5.015 / 1024.0)*11.132;
       //Calculate percentage...
       perVolt = (voltage*100)/ maxBattery;
       if      (perVolt<=75)               { level=0; }
       else if (perVolt>75 && perVolt<=80) { level=1; }    //        Battery level
       else if (perVolt>80 && perVolt<=85) { level=2; }    //Min ------------------------   Max
       else if (perVolt>85 && perVolt<=90) { level=3; }    //    | 0 | 1 | 2 | 3 | 4 | 5 | >
       else if (perVolt>90 && perVolt<=95) { level=4; }    //    ------------------------
       else if (perVolt>95)                { level=5; }   
       Serial.println(level);   
    }
   
}

NEXT please choose a style when coding. This code has at least three (3) styles.
I cleaned up part of the code to make it more readable.

== Breaking up the Code ==
The code can be broken into several sections. You should decide how.

However, there are natural breaks:

  • Turn, Forward, Backward, etc.
  • Speed
  • Lights
  • Horn
  • Voltage (battery level)

There other things that is pure logic is the ‘state’. This appears to be the way the code is keeping the finite state machine. That is, the variable ‘state’ is an important variable and you should consider it for your design.

Let me know if this helps.
Jesse

Before you implement this I'd suggest you test what range you can get from the bluetooth. Under absolutely ideal conditions you can't expect more than 10 meters, in a noisy environement (which is inevitable with motors involved), I'd think you'd be lucky to get 2.

Hello Jess, Thank you for your response. I am at very beginning of coding so I am still miles away from being able to code such sketch myself. I found it on that website i mentioned. For past 10 days i have spent hours watching tutorials and reading the book on coding in C for arduino in order to get understanding of this language, its functionality and applicability. To be honest, i am still not capable of editing this code in order to accomplish the goal. But i do appreciate your advice and i am going to try to research more about coding styles and the logic of state you mentioned. Again, thank you so much. Slav

KenF: Before you implement this I'd suggest you test what range you can get from the bluetooth. Under absolutely ideal conditions you can't expect more than 10 meters, in a noisy environement (which is inevitable with motors involved), I'd think you'd be lucky to get 2.

Hi Ken, Thank you for bringing this up. I was concerned about this too at the beginning however i found few things to resolve this issue. One, there are Bluetooth shields like this one http://www.tohobby.com/arduino-ultra-long-range-bluetooth-module-max-200m.html

and there is also the hack with soldering small antenna to Bluetooth i have in order to increase the range. I am probably going to try buy one and hack one just because i love experimenting. :-) Best, Slav

Slavisha: Hi Ken, Thank you for bringing this up. I was concerned about this too at the beginning however i found few things to resolve this issue. One, there are Bluetooth shields like this one http://www.tohobby.com/arduino-ultra-long-range-bluetooth-module-max-200m.html

and there is also the hack with soldering small antenna to Bluetooth i have in order to increase the range. I am probably going to try buy one and hack one just because i love experimenting. :-) Best, Slav

OK I hope it works out for you, but I know how frustrating it can be when you have to drop an entire project, simply because the infrastructure cannot support it.

The big RC car my son has already has controller that goes about 300ft which is more than what i really need. I never really drive him more than 10-20m. So if i can get only 10m i will be happy. If you dont mind me asking, what have you tried to work on so far? And i am sorry that it didnt work out but i am interested to see what people do. BTW, on your Avatar, is that Nikola Tesla? :-)