Car Avoiding Obstacles with Bluetooth sensor

Hi Everyone.
I'm trying to finish my project on car avoiding obstacles with Bluetooth sensor.
as you can see in my code everything is working fine but since I added the Bluetooth sensor the ultrasonic sensor does not work anymore.
inside the loop function it does recognize only the moveForward(); function which is in else condition, the if the condition does not work anymore.

Thank you for your help

code:

#include <Servo.h>         
#include <NewPing.h>  

//L298N motor control pins
const int LeftMotorForward = 6;
const int LeftMotorBackward = 7;
const int RightMotorForward = 5;
const int RightMotorBackward = 4;
char Incoming_value = 1;

//sensor pins
#define trig_pin A1 //analog input 1
#define echo_pin A2 //analog input 2

#define maximum_distance 200
boolean goesForward = false;
int distance = 100;

NewPing sonar(trig_pin, echo_pin, maximum_distance);
Servo servo_motor; 

void setup(){

  Serial.begin(9600); 
  pinMode(RightMotorForward, OUTPUT);
  pinMode(LeftMotorForward, OUTPUT);
  pinMode(LeftMotorBackward, OUTPUT);
  pinMode(RightMotorBackward, OUTPUT);
  
  servo_motor.attach(9); //our servo pin

  servo_motor.write(115);
  delay(2000);
  distance = readPing();
  delay(100);
  distance = readPing();
  delay(100);
  distance = readPing();
  delay(100);
  distance = readPing();
  delay(100);
}

void loop(){

  int distanceRight = 0;
  int distanceLeft = 0;
  delay(50);

  if(Serial.available() > 0)  
  {
    Incoming_value = Serial.read();      
   
    if(Incoming_value == '1')            
      {
        
  if (distance <= 20){
    moveStop();
    delay(300);
    moveBackward();
    delay(400);
    moveStop();
    delay(300);
    distanceRight = lookRight();
    delay(300);
    distanceLeft = lookLeft();
    delay(300);

    if (distance >= distanceLeft){
      turnRight();
      moveStop();
    }
    else{
      turnLeft();
      moveStop();
    }
  }
  else{
    
    moveForward();
    
  }
    distance = readPing();
        } 
    else if(Incoming_value == '0')
      moveStop();
  }     

}

int lookRight(){  
  servo_motor.write(50);
  delay(500);
  int distance = readPing();
  delay(100);
  servo_motor.write(115);
  return distance;
}

int lookLeft(){
  servo_motor.write(170);
  delay(500);
  int distance = readPing();
  delay(100);
  servo_motor.write(115);
  return distance;
  delay(100);
}

int readPing(){
  delay(70);
  int cm = sonar.ping_cm();
  if (cm==0){
    cm=250;
  }
  return cm;
}

void moveStop(){
  
  digitalWrite(RightMotorForward, LOW);
  digitalWrite(LeftMotorForward, LOW);
  digitalWrite(RightMotorBackward, LOW);
  digitalWrite(LeftMotorBackward, LOW);
}

void moveForward(){

  if(!goesForward){

    goesForward=true;
    
    digitalWrite(LeftMotorForward, HIGH);
    digitalWrite(RightMotorForward, HIGH);
  
    digitalWrite(LeftMotorBackward, LOW);
    digitalWrite(RightMotorBackward, LOW); 
  }
}

void moveBackward(){

  goesForward=false;

  digitalWrite(LeftMotorBackward, HIGH);
  digitalWrite(RightMotorBackward, HIGH);
  
  digitalWrite(LeftMotorForward, LOW);
  digitalWrite(RightMotorForward, LOW);
  
}

void turnRight(){

  digitalWrite(LeftMotorForward, HIGH);
  digitalWrite(RightMotorBackward, HIGH);
  
  digitalWrite(LeftMotorBackward, LOW);
  digitalWrite(RightMotorForward, LOW);
  
  delay(500);
  
  digitalWrite(LeftMotorForward, HIGH);
  digitalWrite(RightMotorForward, HIGH);
  
  digitalWrite(LeftMotorBackward, LOW);
  digitalWrite(RightMotorBackward, LOW);
 
  
  
}

void turnLeft(){

  digitalWrite(LeftMotorBackward, HIGH);
  digitalWrite(RightMotorForward, HIGH);
  
  digitalWrite(LeftMotorForward, LOW);
  digitalWrite(RightMotorBackward, LOW);

  delay(500);
  

}

Welcome to the forum!

First, please read and follow thin instructions in the How to Use the Forum post, and add code tags.

1 Like

Please read the first topic telling how to get the best from this forum.
Autoformatting code and using code tags gives You more points.
The more information You provide, asked for in that topic, the more helpers and the better help You get.
Using shortcuts to post a question quickly only kicks back.

You likely have some kind of collision, two devices use the same system resource.
Let's wait a little and hope for some system skilled helpers to step in.

More than once powering and wiring has been an issue so a wiring diagram, not a Fritzing, is important.

1 Like

Thank you for the advice :blush:

Every helper wants to help and get the response that "now it works".
The tone can be rough but the heart is there.

This if statement seems to be a problem. When is distance ever less than or equal to 20? Print out values to see. What happens if you read a serial character other than '0' or '1'? (Check the line ending setting on the serial monitor).

if (Incoming_value == '1')
    {

      if (distance <= 20) {
        moveStop();
        delay(300);
        moveBackward();
        delay(400);
        moveStop();
        delay(300);
        distanceRight = lookRight();
        delay(300);
        distanceLeft = lookLeft();
        delay(300);

        if (distance >= distanceLeft) {
          turnRight();
          moveStop();
        }
        else {
          turnLeft();
          moveStop();
        }
      }
      else {

        moveForward();

      }
      distance = readPing();
    }