Obstacle Avoiding Car Please Help

Hello, I made a Self Driving and Obstacle Avoiding Car using Arduino UNO, L298N, Motors, Servo MG90S and Ultrasonic Sensor. The Car moves forward, once the obstacle comes the servo only looks the direction right and not left. Then after a long gap it moves forward and again everything repeats. I'm a grade 3 student. I had made the car before but at that time it worked. I am making this for a project in my school and need to submit this by tuesday... so kindly help me! Here's the code:
'''
#include <Servo.h> //Servo motor library. This is standard library
#include <NewPing.h> //Ultrasonic sensor function library. You must install this library

//our L298N control pins
const int LeftMotorForward = 7;
const int LeftMotorBackward = 6;
const int RightMotorForward = 4;
const int RightMotorBackward = 5;

//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); //sensor function
Servo servo_motor; //our servo name

void setup(){

pinMode(RightMotorForward, OUTPUT);
pinMode(LeftMotorForward, OUTPUT);
pinMode(LeftMotorBackward, OUTPUT);
pinMode(RightMotorBackward, OUTPUT);

servo_motor.attach(11); //our servo pin

servo_motor.write(85);
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 (distance <= 20){
moveStop();
delay(300);
moveBackward();
delay(400);
moveStop();
delay(300);
distanceRight = lookRight();
delay(200);
distanceLeft = lookLeft();
delay(300);

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

}
else{
moveForward();
}
distance = readPing();
}

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

int lookLeft(){
servo_motor.write(170);
delay(500);
int distance = readPing();
delay(100);
servo_motor.write(85);
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);

digitalWrite(LeftMotorForward, HIGH);
digitalWrite(RightMotorForward, HIGH);

digitalWrite(LeftMotorBackward, LOW);
digitalWrite(RightMotorBackward, LOW);
}
'''

Please edit your post to add code tags (select the code and press the "</>" post editor button).

More hints on getting help are in the "How to get the best out of this forum" post, at the head of very forum topic.

1 Like
#include <Servo.h>    //Servo motor library. This is standard library
#include <NewPing.h>  //Ultrasonic sensor function library. You must install this library

//our L298N control pins
const int LeftMotorForward = 7;
const int LeftMotorBackward = 6;
const int RightMotorForward = 4;
const int RightMotorBackward = 5;

//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);  //sensor function
Servo servo_motor;                                    //our servo name


void setup() {

  pinMode(RightMotorForward, OUTPUT);
  pinMode(LeftMotorForward, OUTPUT);
  pinMode(LeftMotorBackward, OUTPUT);
  pinMode(RightMotorBackward, OUTPUT);

  servo_motor.attach(11);  //our servo pin

  servo_motor.write(85);
  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 (distance <= 20) {
    moveStop();
    delay(300);
    moveBackward();
    delay(400);
    moveStop();
    delay(300);
    distanceRight = lookRight();
    delay(200);
    distanceLeft = lookLeft();
    delay(300);

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

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

int lookLeft() {
  servo_motor.write(170);
  delay(500);
  int distance = readPing();
  delay(100);
  servo_motor.write(85);
  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);

  digitalWrite(LeftMotorForward, HIGH);
  digitalWrite(RightMotorForward, HIGH);

  digitalWrite(LeftMotorBackward, LOW);
  digitalWrite(RightMotorBackward, LOW);
}

What exactly is different this time or not working? Does it not move at all, only go forward, etc.?

You have this:
if (distance >= distanceLeft)
But not this:
if (distance >= distanceRight)

The car is going forward then it comes back then the servo turns only right and back in its original position and then it does not move to the left though its mentioned in the code. After a long gap it moves forward and its stuck in the loop forever.

I have no doubt that that is because of all the delays.

I suggest that you read up on the use of millis() to write non-blocking code, probably in combination with a finite state machine to implement the steps (states) of your servo move.

1 Like

Can you give me the correct code since I am new to Arduino IDE

This is your code in void loop() which seems to only test left and turn right. The only way to turn left is if distance is <= distanceLeft(). Write a distanceRight mirror to this code, so it can turn left.

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

Can you give me the correct code since I am new to Arduino

With a 5v power supply its working but with a 9v one its not.

Like this?

#include <Servo.h>    //Servo motor library. This is standard library
#include <NewPing.h>  //Ultrasonic sensor function library. You must install this library

//our L298N control pins
const int LeftMotorForward = 7;
const int LeftMotorBackward = 6;
const int RightMotorForward = 4;
const int RightMotorBackward = 5;

//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);  //sensor function
Servo servo_motor;                                    //our servo name


void setup() {

  pinMode(RightMotorForward, OUTPUT);
  pinMode(LeftMotorForward, OUTPUT);
  pinMode(LeftMotorBackward, OUTPUT);
  pinMode(RightMotorBackward, OUTPUT);

  servo_motor.attach(11);  //our servo pin

  servo_motor.write(85);
  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 (distance <= 20) {
    moveStop();
    delay(300);
    moveBackward();
    delay(400);
    moveStop();
    delay(300);
    distanceRight = lookRight();
    delay(200);
    distanceLeft = lookLeft();
    delay(300);

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

    if (distance >= distanceRight){
  turnRight();
  moveStop();
}
else{
  turnLeft();
  moveStop();
}
  } else {
    moveForward();
  }
  distance = readPing();
}

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

int lookLeft() {
  servo_motor.write(170);
  delay(500);
  int distance = readPing();
  delay(100);
  servo_motor.write(85);
  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);

  digitalWrite(LeftMotorForward, HIGH);
  digitalWrite(RightMotorForward, HIGH);

  digitalWrite(LeftMotorBackward, LOW);
  digitalWrite(RightMotorBackward, LOW);
}

Yes, you added a "distanceRight" test, now look at the "distanceLeft" test and "mirror" it, that is to say, where "distanceLeft" does a "Right", make the "distanceRight" do a "Left"... like this...

    if (distance >= distanceRight) { // the code added to mirror distanceLeft test
      turnRight(); // this was left over from distanceLeft test
      turnLeft(); // changed Right to Left to mirror the distanceLeft test 
      moveStop();
    }
    else {
      turnLeft(); // left over from distanceLeft test
      turnRight(); // changed Left to Right to mirror the distanceLeft test
      moveStop();
    }

Your code is still full of delay() statements (see post #7). Use them sparingly, like for the sonar (ping) and actual time for the servos and motors to do their work.

How are the stepper motors working? I don't see a library, steps, setup, et c. in your code. I think you have five servo motors connected (one for the "lookLeft/lookRight" part, and four 360-degree servo motors to do the "move"...

Would you post a drawing (doesn't have to be perfect, a hand drawing is good) of your project? Thank you.</>

The reason I crossed the above out is... I finally realized you are using a DC motor (I was thinking this whole time you had a stepper motor, sorry).

On your L298N, there are ENA and ENB jumpers. I think your ENA jumper (for LEFT motor) is missing, which might the problem with your right-turn.
ln298n

Hello @Samar2013,

I have "your code" working in a simulator with some edits. Your code still needs work to make it "turn left" (you will see a brief "movement" but should make a full movement). I used LEDs for DC motors. RED is LEFT (port), GREEN is RIGHT (starboard), BLUE will be ENABLE/DISABLE or PWM.

To use the sim, press "play" to start then click the sonar and slide the value to 20. At that point, collision is possible so it "looks left, looks right" then turns, stops, turns, stops, and pings again. If the slide value is 2 or less, all motors stop.

There is a bool DEBUG = 1 line that sends output to the Serial Monitor ... where you can see the decision making for obstacle < 20, obstacle < 2, obstacle > 20, left turn, right turn, stop, backward, and a random "distanceLeft" and "distanceRight" in order to make it turn different directions .

Change that to 0 to stop Serial Prints.

Only the right motor is working and not the left one. The jumpers are also there.

If you are using the code from the simulation (above), then the problem is with the power for the motor, the wiring to the motor, or the motor.

Try changing one of those things at a time to find the problem.

Hi,
I would suggest at this stage you forget your code, put it aside for the moment, and write some test code to JUST make your motors perform.

You are in troubleshooting mode and need to produce bits of code to test ALL your hardware.

Thanks.. Tom.. :smiley: :+1: :coffee: :australia:

1 Like

thank you for guiding....

Post a photograph of your project showing right motor, right motor wiring, right motor power, left motor, left motor wiring and left motor power.