-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathobstacle_detection_ultrasonic.ino
147 lines (123 loc) · 3.5 KB
/
obstacle_detection_ultrasonic.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
const int trigPin = 10; // Ultrasonic sensor trigger pin
const int echoPin = 9; // Ultrasonic sensor echo pin
const int motor1Pin1 = 13; // Motor 1 input 1 pin
const int motor1Pin2 = 8; // Motor 1 input 2 pin
const int motor2Pin1 = 0; // Motor 2 input 1 pin
const int motor2Pin2 = 1; // Motor 2 input 2 pin
const int servoPin = 5; // Servo motor signal pin
const int minDistance = 5; // Minimum distance to obstacle
const int maxDistance = 20; // Maximum distance to obstacle
const int speed = 200; // Motor speed
const int turnDelay = 500; // Time to turn (in ms)
// Color Sensor Pins
const int S0 = 2;
const int S1 = 3;
const int S2 = 4;
const int S3 = 6;
const int OUT = 7;
const int LED = A0;
// Motor Pins
const int enA = 11;
const int in1 = 13;
const int in2 = 8;
// Color Thresholds
const int redThreshold = 200;
const int greenThreshold = 500;
// Color Frequencies
const int redFrequency = 500;
const int greenFrequency = 1000;
void setup() {
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(motor1Pin1, OUTPUT);
pinMode(motor1Pin2, OUTPUT);
pinMode(motor2Pin1, OUTPUT);
pinMode(motor2Pin2, OUTPUT);
pinMode(servoPin, OUTPUT);
// Color Sensor Setup
pinMode(S0, OUTPUT);
pinMode(S1, OUTPUT);
pinMode(S2, OUTPUT);
pinMode(S3, OUTPUT);
pinMode(OUT, INPUT);
// Motor Setup
pinMode(enA, OUTPUT);
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
// Color Sensor Frequency Scaling
digitalWrite(S0, HIGH);
digitalWrite(S1, LOW);
}
void loop() {
// Ultrasonic sensor distance measurement
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
long duration = pulseIn(echoPin, HIGH);
int distance = duration / 58;
// If an obstacle is detected
if (distance > minDistance && distance < maxDistance) {
// Stop the car
digitalWrite(motor1Pin1, LOW);
digitalWrite(motor1Pin2, LOW);
digitalWrite(motor2Pin1, LOW);
digitalWrite(motor2Pin2, LOW);
// Change direction
for (int angle = 0; angle <= 180; angle++) {
digitalWrite(servoPin, HIGH);
delayMicroseconds(500 + angle * 5);
digitalWrite(servoPin, LOW);
delayMicroseconds(19500 - angle * 5);
digitalWrite(servoPin, HIGH);
delayMicroseconds(15000 - angle * 5);
}
// Start moving forward again
digitalWrite(motor1Pin1, HIGH);
digitalWrite(motor1Pin2, LOW);
digitalWrite(motor2Pin1, HIGH);
digitalWrite(motor2Pin2, LOW);
delay(100);
} else {
// Move the car forward
digitalWrite(motor1Pin1, HIGH);
digitalWrite(motor1Pin2, LOW);
digitalWrite(motor2Pin1, HIGH);
digitalWrite(motor2Pin2, LOW);
}
// Read Color Sensor Values
int red = 0;
int green = 0;
for (int i = 0; i < 5; i++) {
digitalWrite(S2, LOW);
digitalWrite(S3, LOW);
delay(10);
red += pulseIn(OUT, LOW);
digitalWrite(S2, HIGH);
digitalWrite(S3, HIGH);
delay(10);
green += pulseIn(OUT, LOW);
}
// Detect Red Color
if (red > redThreshold && green < greenThreshold) {
// Stop the Car
analogWrite(enA, 0);
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
}
// Detect Green Color
else if (green > greenThreshold && red < redThreshold) {
// Move the Car Forward
analogWrite(enA, 200);
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
}
// No Color Detected
else {
// Move the Car Forward
analogWrite(enA, 200);
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
}
}