-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathObstacle_Avoiding_Code.ino
112 lines (103 loc) · 2.4 KB
/
Obstacle_Avoiding_Code.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#define speedL 10
#define IN1 9
#define IN2 8
#define IN3 7
#define IN4 6
#define speedR 5
#define trig 4
#define echo 3
long duration, distance;
void setup() {
// Set all pins as OUTPUT
for (int i = 4; i <= 10; i++) {
pinMode(i, OUTPUT);
}
// Set echo pin as INPUT
pinMode(echo, INPUT);
// Begin serial communication
Serial.begin(9600);
}
void Ultrasonic() {
// Clear trigger pin
digitalWrite(trig, LOW);
delayMicroseconds(2);
// Send 10us pulse to trigger pin
digitalWrite(trig, HIGH);
delayMicroseconds(10);
digitalWrite(trig, LOW);
// Measure the duration of the pulse
duration = pulseIn(echo, HIGH);
// Calculate distance
distance = (duration / 2) * 0.0343;
}
void backword() {
// Set motor direction for backward motion
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
// Set motor speeds
analogWrite(speedL, 150);
analogWrite(speedR, 150);
}
void forward() {
// Set motor direction for forward motion
digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
digitalWrite(IN3, LOW);
digitalWrite(IN4, HIGH);
// Set motor speeds
analogWrite(speedL, 150);
analogWrite(speedR, 150);
}
void left() {
// Set motor direction for left turn
digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
digitalWrite(IN3, LOW);
digitalWrite(IN4, HIGH);
// Set motor speeds
analogWrite(speedL, 0);
analogWrite(speedR, 150);
}
void right() {
// Set motor direction for right turn
digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
digitalWrite(IN3, LOW);
digitalWrite(IN4, LOW);
// Set motor speeds
analogWrite(speedL, 150);
analogWrite(speedR, 150);
}
void stopp() {
// Stop both motors
digitalWrite(speedL, LOW);
digitalWrite(speedR, LOW);
}
void loop() {
// Print distance to serial monitor
Serial.println(distance);
// Measure distance using ultrasonic sensor
Ultrasonic();
// If obstacle detected within 35 cm
if (distance < 35 && distance != 0) {
// Stop motors, delay, move backward, delay, then turn right
stopp();
delay(250);
backword();
delay(500);
right();
delay(500);
}
// If no obstacle detected
else if (distance == 0) {
// Move forward
forward();
}
// If obstacle not too close
else {
// Move forward
forward();
}
}