-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbo_hrc.ino
More file actions
55 lines (45 loc) · 1.28 KB
/
bo_hrc.ino
File metadata and controls
55 lines (45 loc) · 1.28 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
// Define ultrasonic sensor pins
#define trigPin 11
#define echoPin 10
// Define motor driver (L293D) pins
#define EnA 5
#define EnB 3
// Set the maximum and minimum distances for motor control
#define maxDistance 20 // in centimeters
void setup() {
// Define ultrasonic sensor pins as input/output
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
// Define motor driver pins as output
pinMode(EnA, OUTPUT);
pinMode(EnB, OUTPUT);
// Initialize serial communication
Serial.begin(9600);
}
void loop() {
// Measure the distance using the ultrasonic sensor
long duration, distance;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration / 2) / 29.1;
// Print the distance to the serial monitor
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");
// Control the motor based on distance
if (distance > maxDistance) {
// Move the motor forward
digitalWrite(EnA, HIGH);
digitalWrite(EnB, LOW);
} else if (distance < maxDistance) {
// Move the motors stop
digitalWrite(EnA, LOW);
digitalWrite(EnB, LOW);
}
// Wait for a moment before the next measurement
delay(1000);
}