Self Moving Cart With Bluetooth

Hi, i am having a little problem with my code. So I now got the motors and sensors to activate after it is connected to the bluetooth. But once connected, the motors spazes, meaning it budges a little then stop. When the the front sensors and back sensors are in use, the motors respond correspondingly, but if left alone, they spaz. Also, now trying to find a way to reset the arduino for when the sensors aren't in use for a certain time.
here is my code and i hope there is someone that can help me. And Thank YOU!

#include <SoftwareSerial.h> 
#define RxD 6
#define TxD 7 
SoftwareSerial blueToothSerial(RxD,TxD);
const int Motor2Pin1 = 10;//pin 2 on IC L293DIC
const int Motor2Pin2 = 11;//pin 7 on IC L293DIC
const int Motor1Pin1 = 8;
const int Motor1Pin2 = 9;
#define trigPin 3 //trig pin from Back Sensor 
#define echoPin 2 //echo pin from Back Sensor
#define echoPin1 4
#define trigPin1 5

 
void setup() {                
  Serial.begin (9600);
  pinMode(RxD, INPUT);
  pinMode(TxD, OUTPUT);
  setupBlueToothConnection();
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(echoPin1, INPUT);
  pinMode(trigPin1, OUTPUT);
  pinMode(Motor2Pin1, OUTPUT);   
  pinMode(Motor2Pin2, OUTPUT); 
  pinMode(Motor1Pin1, OUTPUT);
  pinMode(Motor1Pin2, OUTPUT);
}


void loop() {
  int duration, distance, du1, di1;
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(1000);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration/2) / 29.1;
  Serial.print( " cm ");
  Serial.println(distance);
  digitalWrite(trigPin1, HIGH);
  delayMicroseconds(1000);
  digitalWrite(trigPin1,LOW);
  du1 = pulseIn(echoPin1, HIGH);
  di1 = (du1/2) / 29.1;
 
  if(blueToothSerial.available())
  {
      blueToothSerial.println("You are connected");
   }
  
  else {
    loop();
  }
  Stop();
   if(distance < 15 && du1 >20){
    GoForward();
    if (di1 < 20){
      Stop();
    }
    
  }
  else {
    Stop();
  }
}

void GoForward(){
  digitalWrite(Motor2Pin2, HIGH);
  digitalWrite(Motor2Pin1, HIGH);
  digitalWrite(Motor1Pin1, HIGH);
  digitalWrite(Motor1Pin2, HIGH);
}

void Stop(){
  digitalWrite(Motor2Pin1, LOW);
  digitalWrite(Motor2Pin2, LOW);
  digitalWrite(Motor1Pin1, LOW);
  digitalWrite(Motor1Pin2, LOW);
}

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 
  delay(2000); // This delay is required.
  blueToothSerial.flush();
}

"I'm stuck somewhere" doesn't really help us. Where are you stuck, and with what? I see your using the old method of reading a distance. Look into using the NewPing library, it will help you a lot and it will be easier for us to read your code. You also don't need that 500 millisecond delay in your loop, but if you do then also look into the Blink without delay example sketch.

i am stuck at a part when i finish connecting to the bluetooth, the sensors don't work leading to the motors not working. Other than the delay removal, is there anything on the code that you know i should change so i can make this all work together?

You can debug your code using the serial monitor and see what values you are getting for distance and distance1. I highly recommend you change your code to use the NewPing library, it is far more accurate and it makes getting data from multiple sensor very easy.

i see. Thanks for your feedback, I am extremely new to arduino though and i have this project due in a week =[ (kinda pushed it till the end lol). But! i got the sensors to work together thanks to your idea. So, Thank you again! lol Now i gotta do my bluetooth. Would you happen to know how to lower the range for a bluetooth shield?

I don't think that's possible, to lower the range. But then again I don't know what BT shield you have.

Right, I'm sorry i should have specified that in the description. I am using a SeeedStudio BTS.

The range does not appear to be able to change.

yeah, i was researching about it, but nothing popped up. would you happen to have an idea of how to reset the bluetooth?

would you happen to have an idea of how to reset the bluetooth?

Sorry, I dont know how and the spec sheet doesn't say either.

Would you happen to know how to lower the range for a bluetooth shield?

What do you mean by "lower the range"? Decrease the distance that the device broadcasts/receives?

  if(blueToothSerial.read() == 'a'){
    blueToothSerial.println("You are connected");
     }

Even if there isn't anything to read, read anyway. Why?

  while (blueToothSerial.available()){
  Serial.println(distance);
  Serial.print(" cm ");
  delay(500);

Why is the output of data dependent on there being unread serial data? Why are you delay()ing? Why does the use of the distance data depend on the presence of unread serial data?

Some of your code is nicely organized - for example having a separate function for setupBluetoothConnection(). I reckon if you split the code in loop() into separate functions for each activity the logic of the system will be more obvious. It looks like there are separate activities to read distances, to read the bluetooth connection and to command the motors.

The long delay()s can't help. Replace them with the Blink Without Delay technique.

...R

hmm. okay so i changed it up now. The bluetooth connection on the phone says failed connection but the bluetooth shield shows that it is connected. Having said this, once "connected" it works nicely; except for motor spaz. Is there something i can add on that can reset the arduino if the cart is not moving for a certain amount of time?

Deeayen:
hmm. okay so i changed it up now. The bluetooth connection on the phone says failed connection but the bluetooth shield shows that it is connected. Having said this, once "connected" it works nicely; except for motor spaz. Is there something i can add on that can reset the arduino if the cart is not moving for a certain amount of time?

So where is the changed code?

What is causing the cart to not move? If the Arduino is working but not receiving the correct instructions you can add code to deal with that? If the Arduino has crashed (gone into an endless loop) you will need to use the watch-dog timer to deal with the problem. From the limited info available my guess is that it has not crashed.

...R