bluetooth car with ultrasonic

My problem is this:
I'm building a car is controlled via Bluetooth.
The car works.
Adding distance sensor HC04 car.
Now I want that when the car approaches a certain range of cm so the car would go back.
I checked if it works, it works for me part-time.
This means that the car is not connected to bluetooth operation is carried out perfectly I approach the sensor distance and the car was going backwards.
But when the car is connected to a Bluetooth smartphone then test the distance is only when I click on a button in the app.

#include <SoftwareSerial.h>
#define trigPin 2
#define echoPin 3

char dataIn = 'S';
int pinLeft = 10;
int pinRight = 11;
int pinForward = 5;
int pinBack = 6;
int pinfrontLights = 8;
int pinbackLights = 7;
int duration, distance;
char determinant;
char det;

void setup()

{

Serial.begin(9600);
Serial.println( "MosheHadadCar");
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(pinLeft, OUTPUT);
pinMode(pinRight, OUTPUT);
pinMode(pinForward, OUTPUT);
pinMode(pinBack, OUTPUT);
pinMode(pinfrontLights, OUTPUT);
pinMode(pinbackLights, OUTPUT);

}

void loop() {

det = check();

while (det == 'F') //forward
{

digitalWrite(pinForward, LOW);
digitalWrite(pinBack, HIGH);
Serial.println("forward");

det = check();
}
while (det == 'B') //Back
{

digitalWrite(pinForward, HIGH);
digitalWrite(pinBack, LOW);
Serial.println("Back");

det = check();
}

while (det == 'L') //left
{

digitalWrite(pinLeft, LOW);
digitalWrite(pinRight, HIGH);

Serial.println("left");
det = check();
}
while (det == 'R') //right
{

digitalWrite(pinLeft, HIGH);
digitalWrite(pinRight, LOW);

Serial.println("right");

det = check();
}

while (det == 'I') //forward right
{
digitalWrite(pinLeft, HIGH);
digitalWrite(pinRight, LOW);
digitalWrite(pinForward, LOW);
digitalWrite(pinBack, HIGH);
Serial.println("forward right");

det = check();
}
while (det == 'J') //back right
{
digitalWrite(pinLeft, HIGH);
digitalWrite(pinRight, LOW);
digitalWrite(pinForward, HIGH);
digitalWrite(pinBack, LOW);
Serial.println("back right");

det = check();
}
while (det == 'G') //forward left
{
digitalWrite(pinLeft, LOW);
digitalWrite(pinRight, HIGH);
digitalWrite(pinForward, LOW);
digitalWrite(pinBack, HIGH);
Serial.println("forward left");
det = check();
}
while (det == 'H') //back left
{
digitalWrite(pinLeft, LOW); \

digitalWrite(pinRight, HIGH);
digitalWrite(pinForward, HIGH);
digitalWrite(pinBack, LOW);
Serial.println("back left");

det = check();
}
while (det == 'S') // stop
{

digitalWrite(pinLeft, HIGH);
digitalWrite(pinRight, HIGH);
digitalWrite(pinForward, HIGH);
digitalWrite(pinBack, HIGH);
det = check();
}
while (det == 'w') // front Lights off
{
digitalWrite(pinfrontLights, LOW);
Serial.println("front Lights off");

det = check();
}
while (det == 'W') // front Lights on
{
digitalWrite(pinfrontLights, HIGH);
Serial.println("front Lights on");

det = check();
}
while (det == 'u') // back Lights off
{
digitalWrite(pinbackLights, LOW);
Serial.println("back Lights off");

det = check();
}
while (det == 'U') // back Lights on
{
digitalWrite(pinbackLights, HIGH);
Serial.println("back Lights on");

det = check();
}

digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration / 2) / 29.1;
if (distance <= 15 ) {
Serial.println("back");
digitalWrite(pinForward, HIGH);
digitalWrite(pinBack, LOW);
Serial.println("Back");
delay(1500);
digitalWrite(pinLeft,HIGH);
digitalWrite(pinRight,HIGH);
digitalWrite(pinForward,HIGH);
digitalWrite(pinBack,HIGH);

}
else {
Serial.print(distance);
Serial.println(" cm");
}

}

int check()
{
if (Serial.available() > 0) //checks the availability of serial communication.
{
dataIn = Serial.read(); //checks the incoming data 'dataIn'..
if (dataIn == 'F')
{
determinant = 'F';
}
else if (dataIn == 'B')
{
determinant = 'B';
}
else if (dataIn == 'L')
{
determinant = 'L';
}
else if (dataIn == 'R')
{
determinant = 'R';
}
else if (dataIn == 'I')
{
determinant = 'I';
}
else if (dataIn == 'J')
{
determinant = 'J';
}
else if (dataIn == 'G')
{
determinant = 'G';
}
else if (dataIn == 'H')
{
determinant = 'H';
}
else if (dataIn == 'S')
{
determinant = 'S';
}
else if (dataIn == 'U')
{
determinant = 'U';
}
else if (dataIn == 'u')
{
determinant = 'u';
}
else if (dataIn == 'W')
{
determinant = 'W';
}

else if (dataIn == 'w')
{
determinant = 'w';
}
}
return determinant;

}

please help me..

check() is silly. If there is serial data, determinant equals the value that arrived. There is no need to use forty-leven if statements to accomplish that.

I do not understand why you are using while statements in loop. The only way to exit the while loops is to send serial data. A simple if statement would be better, I think. Checking serial data and checking the ping sensor should happen first in loop(), and then you should decide what to do. The ping sensor should override the serial input, I would think. If the user sends the command to go forward, and there is an obstacle in the way, the bot shouldn't go forward, should it?

moshehad123:
please help me..

Do NOT bump a thread after only 40 minutes!

PaulS:
check() is silly. If there is serial data, determinant equals the value that arrived. There is no need to use forty-leven if statements to accomplish that.

I do not understand why you are using while statements in loop. The only way to exit the while loops is to send serial data. A simple if statement would be better, I think. Checking serial data and checking the ping sensor should happen first in loop(), and then you should decide what to do. The ping sensor should override the serial input, I would think. If the user sends the command to go forward, and there is an obstacle in the way, the bot shouldn't go forward, should it?

I did not understand your answer what I should do.
And the purpose of the sensor distance is when the user sends a command to the bluetooth For example, a user driving forward and then an obstacle sensor detects the obstacle and automatically driving back.

On any given pass through loop(), what do you need to know? You need to know what the user wants the robot to do. You need to know whether that is possible, based on the distance.

So, get those two pieces of information FIRST. Then, make decisions based on data that obtained on this iteration of loop(). That means that you do NOT use while loops in loop().

PaulS:
On any given pass through loop(), what do you need to know? You need to know what the user wants the robot to do. You need to know whether that is possible, based on the distance.

So, get those two pieces of information FIRST. Then, make decisions based on data that obtained on this iteration of loop(). That means that you do NOT use while loops in loop().

So you did not understand what the problem is. The car is working well, is driving every direction I want through the app. This distance sensor that makes me a problem. Once Bluetooth is not connected to the sensor works so well. But as soon as I connect the Bluetooth smartphone then all pressing it checks the distance, and I want it connected to bluetooth then check the distance sensor distance regardless smartphone app buttons.

So you did not understand what the problem is.

Yes, I did. The ping sensor MUST override the users needs. You keep insisting that it is secondary.

In any case, I can't help any more.

I think what Paul is trying to say is that if you structure your program in a more logical and thought out manner then you should be able to see where the issues are more easily.

Break things down into steps and test each step independently. Standard troubleshooting.

PaulS:
Yes, I did. The ping sensor MUST override the users needs. You keep insisting that it is secondary.

In any case, I can't help any more.

npkamen:
I think what Paul is trying to say is that if you structure your program in a more logical and thought out manner then you should be able to see where the issues are more easily.

Break things down into steps and test each step independently. Standard troubleshooting.

Ok , i see.
How do I get distance sensor override the users needs?

moshehad123:
Ok , i see.
How do I get distance sensor override the users needs?

Like Paul suggested. Start with reading your inputs, decide which one to obey, then perform your action.