Ultra Sonic sensor with Bluetooth not working as it suppose to work.

hello there !!!

please help me to solve my problem i’m using arduino uno i made a car using one dc motor l298n motor driver ultraSonic and a Bluetooth hc-05. The problem is when i send command from android to Bluetooth
dc motor run as it suppose to run but what i want is that when the distance is less then 20cm motor should stop but it is not stopping,i’m sharing my code please help me i’m new at this!!!

It should work when the condition became false even distance less then 20cm motor should stop but actually its not stopping help!!!

#include<SoftwareSerial.h>
#include <VarSpeedServo.h>

VarSpeedServo frountWheel;

#define TxD 2
#define RxD 3


SoftwareSerial bluetoothSerial(TxD, RxD);

char c;
int Speed=100;
int spcontroll=10;      //DC ( PWM ) pin for speed controll analogWrtie
int r1=12,r2=13;        //DC rotion controll pins back and forwerd

boolean b;

const int trig = 6;
const int echo = 5;            //ultra sonic pin to recive the waves

long duration;
int distance;
int safetyDistance;


void setup() {
frountWheel.attach(9);
pinMode(spcontroll,OUTPUT);
pinMode(r1,OUTPUT);
pinMode(r2,OUTPUT);

pinMode(trig,OUTPUT);
pinMode(echo,INPUT);
frountWheel.write(50,255,true);

bluetoothSerial.begin(9600);
Serial.begin(9600);
}

void loop() {



    // Clears the trig pin
digitalWrite(trig, LOW);
delayMicroseconds(2);
digitalWrite(trig, HIGH);
delayMicroseconds(10);
digitalWrite(trig, LOW);

// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echo, HIGH);
distance= duration*0.034/2;
safetyDistance = distance;

if(bluetoothSerial.available()){   
  
  c = bluetoothSerial.read();
  
  Serial.println(c);
 
if(safetyDistance<=20){
  analogWrite(spcontroll,1);
}
if(c=='a'&&safetyDistance>21){
   digitalWrite(r1,HIGH);
        digitalWrite(r2,LOW);
        analogWrite(spcontroll,255);
        
        
}

if(c=='b'){

      digitalWrite(r1,LOW);
      digitalWrite(r2,HIGH);
      analogWrite(spcontroll,255);
     // delay(20);

}
if(c=='e'){
analogWrite(spcontroll,0);
//  delay(20);
}
if(c=='c'){

frountWheel.write(50,80,true);

frountWheel.write(70,80,true);
delay(500);
frountWheel.write(50,80,true);

}
if(c=='d'){
frountWheel.write(50,80,true);
frountWheel.write(30,80,true);
delay(500);
frountWheel.write(50,80,true);

}


delay(100);    
}
Serial.print("Distance in CM: ");
Serial.println(safetyDistance);
Serial.println(c);

}

Thank you!!

Please edit your post and use the code tags button </> to format your code properly.

You probably don’t want to be using delay() in your code; even if it seems OK now, as you keeping adding to your code, you will eventually run in to problems. Look at the “blink without delay” example in the IDE to see how it can be avoided.

Without any knowledge of what VarSpeedServo does, I’m guessing that the reason your sketch doesn’t stop is because your if (safetyDistance <= 20) is inside the if (bluetoothSerial.available()) block. In other words, if there are no characters pending in bluetoothSerial, the distance will not be checked.

You could fix it perhaps like this:

  c = 0;
  if (bluetoothSerial.available()) {
    c = bluetoothSerial.read();
  }

Suggestion:

if(safetyDistance<=20){
 analogWrite(spcontroll,1);
}
else if(c=='a'&&safetyDistance>21){
  digitalWrite(r1,HIGH);
       digitalWrite(r2,LOW);
       analogWrite(spcontroll,255);
       
       
}

else if(c=='b'){

      digitalWrite(r1,LOW);
      digitalWrite(r2,HIGH);
      analogWrite(spcontroll,255);
     // delay(20);

}
else if(c=='e'){
analogWrite(spcontroll,0);
//  delay(20);
}
else if(c=='c'){

frountWheel.write(50,80,true);

frountWheel.write(70,80,true);
delay(500);
frountWheel.write(50,80,true);

}
else if(c=='d'){
frountWheel.write(50,80,true);
frountWheel.write(30,80,true);
delay(500);
frountWheel.write(50,80,true);

}

Use Else and it might work. The further ifs were overwriting the first result

Check my code for something similar to this https://github.com/marwanj/Arduino-Projects

arduarn

you are maybe write i will try the way you are saying by the way thank you for replay!!!

i did my best to do it but still same issue, please help

Sure, but you need to post your updated code so we can see what you have done.