Arduino obstacle avoiding control car

my problem is the sensitivity of the sensor when detecting distance . it gets fast then it cant detect the obstacle thus it gets bump quickly

#include <SoftwareSerial.h>
SoftwareSerial BT_Serial(2, 3); // RX, TX
//motor pins
#define enA 10
#define in1 9 
#define in2 8 
#define in3 7 
#define in4 6 
#define enB 5 

#define servo A4
#define echo A2    //Echo pin
#define trigger A3 //Trigger pin

int distance_L, distance_F, distance_R;
long distance;
int set = 60; //set distance 20 to 60 

int bt_data; // variable to receive data from the serial port 
int Speed = 90;  
int mode=0;

void setup(){ // put your setup code here, to run once

pinMode(echo, INPUT );
pinMode(trigger, OUTPUT);   
pinMode(enA, OUTPUT);  
pinMode(in1, OUTPUT); 
pinMode(in2, OUTPUT);  
pinMode(in3, OUTPUT);    
pinMode(in4, OUTPUT); 
pinMode(enB, OUTPUT);

Serial.begin(9600); 
BT_Serial.begin(9600);                

pinMode(servo, OUTPUT);

 for (int angle = 70; angle <= 140; angle += 5)  {
   servoPulse(servo, angle);  }
 for (int angle = 140; angle >= 0; angle -= 5)  {
   servoPulse(servo, angle);  }

 for (int angle = 0; angle <= 70; angle += 5)  {
   servoPulse(servo, angle);  }
delay(500);
}

void loop(){  
//reads data and saves it in state    
if(BT_Serial.available() > 0){  
bt_data = BT_Serial.read(); 
Serial.println(bt_data);     
if(bt_data > 20){Speed = bt_data;}      
}

     if(bt_data == 8){mode=0; Stop();}    //Manual
else if(bt_data ==10){mode=2; Speed=90;} //Automatic Obstacle Avoiding 

analogWrite(enA, Speed); // duty cycle 0 to 255 Enable Pin A for Motor1 Speed 
analogWrite(enB, Speed); // duty cycle 0 to 255 Enable Pin B for Motor2 Speed 

if(mode==0){     
//                          Key Control 
     if(bt_data == 1){forward();}  // if the bt_data is '1' the DC motor will go forward
else if(bt_data == 2){backward();}  // if the bt_data is '2' the motor will Reverse
else if(bt_data == 3){turnLeft();}  // if the bt_data is '3' the motor will turn left
else if(bt_data == 4){turnRight();} // if the bt_data is '4' the motor will turn right
else if(bt_data == 5){Stop();}     // if the bt_data '5' the motor will Stop

//                          Voice Control 
else if(bt_data == 6){turnLeft();  delay(400);  bt_data = 5;}
else if(bt_data == 7){turnRight(); delay(400);  bt_data = 5;}
}

if(mode==2){    
//                          Auto Control
 distance_F = Ultrasonic();
 Serial.print("S=");Serial.println(distance_F);
  if (distance_F > set){forward();}
    else{
      backward(); 
      Stop();
      Check_side();}
}

delay(10);
}

void servoPulse (int pin, int angle){
int pwm = (angle*11) + 500;      //convert angle to microseconds
 digitalWrite(pin, HIGH);
 delayMicroseconds(pwm);
 digitalWrite(pin, LOW);
 delay(50);                     //refresh the cycle of servo
}

long Ultrasonic(){
  digitalWrite(trigger, LOW);
  delayMicroseconds(2);
  digitalWrite(trigger, HIGH);
  delayMicroseconds(10);
  distance = pulseIn (echo, HIGH);
  return distance / 29 / 2;
}

void Distance(){
  if (distance_L > distance_R){
  backward();
  delay(350);
  turnLeft();
  delay(350);
 // Stop();
  //delay(350);
  }
  else if (distance_R > distance_L){
  backward();
  delay(350);
  turnRight();
  delay(350);
 // Stop();
 // delay(350);
  }
  else{
  backward();
  delay(300);
  //turnRight();
  //turnLeft();
  //delay(500);
  }
}

void Check_side(){
   
    Stop();
    delay(100);
  //   backward();
  //  delay(100);
 for (int angle = 70; angle <= 140; angle += 5)  {
   servoPulse(servo, angle);  }
    delay(300);
    distance_L = Ultrasonic();
    delay(100);
  for (int angle = 140; angle >= 0; angle -= 5)  {
   servoPulse(servo, angle);  }
    delay(500);
    distance_R = Ultrasonic();
    delay(100);
 for (int angle = 0; angle <= 70; angle += 5)  {
   servoPulse(servo, angle);  }
    delay(300);
    Distance();
}

void turnRight(){  
digitalWrite(in1, HIGH); //Right Motor forword Pin 
digitalWrite(in2, LOW);  //Right Motor backword Pin 
digitalWrite(in3, LOW);  //Left Motor backword Pin 
digitalWrite(in4, HIGH); //Left Motor forword Pin 
}

void turnLeft(){ 
digitalWrite(in1, LOW);  //Right Motor forword Pin 
digitalWrite(in2, HIGH); //Right Motor backword Pin 
digitalWrite(in3, HIGH); //Left Motor backword Pin 
digitalWrite(in4, LOW);  //Left Motor forword Pin 
}

void forward(){ 
digitalWrite(in1, LOW);  //Right Motor forword Pin 
digitalWrite(in2, HIGH); //Right Motor backword Pin  
digitalWrite(in3, LOW);  //Left Motor backword Pin 
digitalWrite(in4, HIGH); //Left Motor forword Pin 
}

void backward(){ 
digitalWrite(in1, HIGH); //Right Motor forword Pin 
digitalWrite(in2, LOW);  //Right Motor backword Pin 
digitalWrite(in3, HIGH); //Left Motor backword Pin 
digitalWrite(in4, LOW);  //Left Motor forword Pin 
}

void Stop(){ 
digitalWrite(in1, LOW); //Right Motor forword Pin 
digitalWrite(in2, LOW); //Right Motor backword Pin 
digitalWrite(in3, LOW); //Left Motor backword Pin 
digitalWrite(in4, LOW); //Left Motor forword Pin 
}

That's because you have lots of delay()'s in your code that lock up the controller so it can't respond to stuff happening in the real world.

Redesign your code so it relies less on delay(), and only use delay() for very brief intervals that don't harm your application; this is likely limited to <20ms or so.

Does the updated code have the same problem?

Probably, yes, but it's your project!

Look up on how you can replace 'delay' with a millis()-based approach.
https://www.google.com/search?q=arduino+delay+without+millis

Since you did not answer I assume all is now working

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.