Obstacle Avoidance Mode

In this mode, the car will move forward. If it detects an object in front of it, the car will stop, turn left and begin to move forward again until it detects another object.

Install additional module

This program requires an addition module to be added to your Arduino IDE.

Please download the AFMotor.zip module and add it to your Arduino IDE

To Add the AFMotor to your Arduino IDE: Select Sketch -> Include Library -> Add ZIP Library, then select the downloaded file,

Code

#include <AFMotor.h>
#define Trig 12
#define Echo 13
#define ENA 5
#define ENB 6
#define IN1 3
#define IN2 4
#define IN3 2
#define IN4 7
float cm; //Distance variable
float temp; //

void setup() {
  Serial.begin(9600);
  pinMode(Trig, OUTPUT);
  pinMode(Echo, INPUT);

  pinMode(IN1, OUTPUT);
  pinMode(IN2, OUTPUT);
  pinMode(IN3, OUTPUT);
  pinMode(IN4, OUTPUT);

  pinMode(ENA, OUTPUT);
  pinMode(ENB, OUTPUT);
}

void loop() {
  digitalWrite(Trig, LOW);
  delayMicroseconds(2);
  digitalWrite(Trig, HIGH);
  delayMicroseconds(10);
  digitalWrite(Trig, LOW);

  temp = float(pulseIn(Echo, HIGH));
  cm = (temp * 17 ) / 1000;
  if (cm < 30 && cm > 10)
  {
    back();
    delay(500);
    Left();
    delay(200);
  }
  if (cm >= 30)
  {
    forward();
    delay(100);
  }


  if (cm < 10)
  {
    STOP();
  }
  Serial.print("Echo =");
  Serial.print(temp);
  Serial.print(" | | Distance = ");
  Serial.print(cm);
  Serial.println("cm");
  delay(100);
}

void forward() {
  analogWrite(ENA, 220);
  analogWrite(ENB, 220);

  digitalWrite(IN1, HIGH);
  digitalWrite(IN2, LOW);
  digitalWrite(IN3, LOW);
  digitalWrite(IN4, HIGH);
  Serial.println("Forward");
}

void back() {
  analogWrite(ENA, 220);
  analogWrite(ENB, 220);
  digitalWrite(IN1, LOW);
  digitalWrite(IN2, HIGH);
  digitalWrite(IN3, HIGH);
  digitalWrite(IN4, LOW);
  Serial.println("Back");
}


void Left() {
  analogWrite(ENA, 220);
  analogWrite(ENB, 220);
  digitalWrite(IN1, LOW);
  digitalWrite(IN2, HIGH);
  digitalWrite(IN3, LOW);
  digitalWrite(IN4, HIGH);
  Serial.println("Left");
}

void Right() {
  analogWrite(ENA, 220);
  analogWrite(ENB, 220);
  digitalWrite(IN1, HIGH);
  digitalWrite(IN2, LOW);
  digitalWrite(IN3, HIGH);
  digitalWrite(IN4, LOW);
  Serial.println("Right");
}

void STOP() {
  digitalWrite(ENA, LOW);
  digitalWrite(ENB, LOW);
  digitalWrite(IN1, LOW);
  digitalWrite(IN2, LOW);
  digitalWrite(IN3, LOW);
  digitalWrite(IN4, LOW);
  Serial.println("STOP");
}

Code Explanation

When the transmitting pin of the ultrasonic module detects an obstacle in front, the reflected pin will receive the signal and transmit it to the MCU through IO pin. After the SIGNAL is received, the MCU will drive the L298N module to realize the corresponding action of the motor. For example, if an obstacle is detected in the front range less than 30cm and larger than 10cm, the MCU will control the car to retreat successively and then turn left; while in the front range less than 10cm, the car will stop, while in the front range greater than 30cm, the car will move forward without any obstacle detected.

Last updated