Arduino SA
Search…
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

1
#include <AFMotor.h>
2
#define Trig 12
3
#define Echo 13
4
#define ENA 5
5
#define ENB 6
6
#define IN1 3
7
#define IN2 4
8
#define IN3 2
9
#define IN4 7
10
float cm; //Distance variable
11
float temp; //
12
13
void setup() {
14
Serial.begin(9600);
15
pinMode(Trig, OUTPUT);
16
pinMode(Echo, INPUT);
17
18
pinMode(IN1, OUTPUT);
19
pinMode(IN2, OUTPUT);
20
pinMode(IN3, OUTPUT);
21
pinMode(IN4, OUTPUT);
22
23
pinMode(ENA, OUTPUT);
24
pinMode(ENB, OUTPUT);
25
}
26
27
void loop() {
28
digitalWrite(Trig, LOW);
29
delayMicroseconds(2);
30
digitalWrite(Trig, HIGH);
31
delayMicroseconds(10);
32
digitalWrite(Trig, LOW);
33
34
temp = float(pulseIn(Echo, HIGH));
35
cm = (temp * 17 ) / 1000;
36
if (cm < 30 && cm > 10)
37
{
38
back();
39
delay(500);
40
Left();
41
delay(200);
42
}
43
if (cm >= 30)
44
{
45
forward();
46
delay(100);
47
}
48
49
50
if (cm < 10)
51
{
52
STOP();
53
}
54
Serial.print("Echo =");
55
Serial.print(temp);
56
Serial.print(" | | Distance = ");
57
Serial.print(cm);
58
Serial.println("cm");
59
delay(100);
60
}
61
62
void forward() {
63
analogWrite(ENA, 170);
64
analogWrite(ENB, 170);
65
66
digitalWrite(IN1, HIGH);
67
digitalWrite(IN2, LOW);
68
digitalWrite(IN3, LOW);
69
digitalWrite(IN4, HIGH);
70
Serial.println("Forward");
71
}
72
73
void back() {
74
analogWrite(ENA, 170);
75
analogWrite(ENB, 170);
76
digitalWrite(IN1, LOW);
77
digitalWrite(IN2, HIGH);
78
digitalWrite(IN3, HIGH);
79
digitalWrite(IN4, LOW);
80
Serial.println("Back");
81
}
82
83
84
void Left() {
85
analogWrite(ENA, 200);
86
analogWrite(ENB, 200);
87
digitalWrite(IN1, LOW);
88
digitalWrite(IN2, HIGH);
89
digitalWrite(IN3, LOW);
90
digitalWrite(IN4, HIGH);
91
Serial.println("Left");
92
}
93
94
void Right() {
95
analogWrite(ENA, 170);
96
analogWrite(ENB, 170);
97
digitalWrite(IN1, HIGH);
98
digitalWrite(IN2, LOW);
99
digitalWrite(IN3, HIGH);
100
digitalWrite(IN4, LOW);
101
Serial.println("Right");
102
}
103
104
void STOP() {
105
digitalWrite(ENA, LOW);
106
digitalWrite(ENB, LOW);
107
digitalWrite(IN1, LOW);
108
digitalWrite(IN2, LOW);
109
digitalWrite(IN3, LOW);
110
digitalWrite(IN4, LOW);
111
Serial.println("STOP");
112
}
Copied!

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 modified 3mo ago