-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathObject Detection.txt
More file actions
119 lines (93 loc) · 3.14 KB
/
Object Detection.txt
File metadata and controls
119 lines (93 loc) · 3.14 KB
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
// Right motor
int enableRightMotor = 6;
int rightMotorPin1 = 7;
int rightMotorPin2 = 8;
// Left motor
int enableLeftMotor = 5;
int leftMotorPin1 = 9;
int leftMotorPin2 = 10;
// Ultrasonic Sensor Pins
#define TRIGGER_PIN A0
#define ECHO_PIN A1
// Speed for motors (0-255)
int motorSpeed = 80;
int semiCircleSpeed = 80; // Speed for semicircular movement
int semiCircleTime = 2000; // Duration for semicircle in milliseconds
void setup() {
// Initialize motor control pins as outputs
pinMode(enableRightMotor, OUTPUT);
pinMode(rightMotorPin1, OUTPUT);
pinMode(rightMotorPin2, OUTPUT);
pinMode(enableLeftMotor, OUTPUT);
pinMode(leftMotorPin1, OUTPUT);
pinMode(leftMotorPin2, OUTPUT);
// Initialize ultrasonic sensor pins
pinMode(TRIGGER_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
// Initialize Serial for debugging
Serial.begin(9600);
// Start moving forward
moveForward();
}
void loop() {
// Measure distance using the ultrasonic sensor
float distance = measureDistance();
// Print the distance for debugging
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");
// If an object is detected within 15 cm, perform a semicircular motion
if (distance > 0 && distance <= 20) {
moveSemicircle();
moveForward(); // Resume moving forward after semicircle
}
delay(100); // Small delay for stable readings
}
// Function to move motors forward
void moveForward() {
// Set Right Motor: Forward Direction
digitalWrite(rightMotorPin1, HIGH);
digitalWrite(rightMotorPin2, LOW);
// Set Left Motor: Forward Direction
digitalWrite(leftMotorPin1, HIGH);
digitalWrite(leftMotorPin2, LOW);
// Set Motor Speeds
analogWrite(enableRightMotor, motorSpeed);
analogWrite(enableLeftMotor, motorSpeed);
}
// Function to perform semicircular motion
void moveSemicircle() {
// Stop the right motor and run the left motor for semicircular motion
digitalWrite(rightMotorPin1, LOW);
digitalWrite(rightMotorPin2, LOW);
digitalWrite(leftMotorPin1, HIGH);
digitalWrite(leftMotorPin2, LOW);
analogWrite(enableRightMotor, 0); // Stop right motor
analogWrite(enableLeftMotor, semiCircleSpeed); // Left motor continues
delay(semiCircleTime); // Time for the semicircular motion
// Stop both motors after semicircle
stopMotors();
}
// Function to stop all motors
void stopMotors() {
digitalWrite(rightMotorPin1, LOW);
digitalWrite(rightMotorPin2, LOW);
digitalWrite(leftMotorPin1, LOW);
digitalWrite(leftMotorPin2, LOW);
analogWrite(enableRightMotor, 0);
analogWrite(enableLeftMotor, 0);
}
// Function to measure distance using ultrasonic sensor
float measureDistance() {
// Send a 10-microsecond pulse to the trigger pin
digitalWrite(TRIGGER_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIGGER_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIGGER_PIN, LOW);
// Measure the duration of the echo pulse
long duration = pulseIn(ECHO_PIN, HIGH);
// Convert the duration to distance (in cm)
float distance = (duration / 2.0) * 0.0343;
return distance;
}