-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharduino-radar.ino
72 lines (57 loc) · 1.71 KB
/
arduino-radar.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
#include <Servo.h>
// Define ultrasonic sensor and motor pins
#define trigPin 5
#define echoPin 6
#define servoPin 8
float duration; // Travel time in microseconds
float distance; // Distance in centimeters
int angle; // Position of the servo motor
Servo servoMotor; // Create servoMotor object
void setup() {
pinMode(trigPin, OUTPUT); // Trig
pinMode(echoPin, INPUT); // Echo
servoMotor.attach(servoPin); // Servo motor
Serial.begin(9600);
}
void loop() {
// Rotate 0 to 180 degrees
for (angle = 0; angle <= 180; angle++) { // Increase by 1 degree
servoMotor.write(angle);
delay(30);
calculateDistance(angle);
if (angle % 2 == 0) // Display data for angles in increments of 2
displayData();
}
// Rotate 180 to 0 degrees
for (angle = 180; angle >= 0; angle--) { // Decrease by 1 degree
servoMotor.write(angle);
delay(30);
calculateDistance(angle);
if (angle % 2 == 0) // Display data for angles in increments of 2
displayData();
}
}
// Calculate distance
float calculateDistance(int angle) {
// Clear trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Turn on trigPin for 10 microseconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Duration in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculate distance
distance = duration * (0.0343) / 2; // Duration in microseconds multiplied by speed of sound in cm/μs divided by 2
// Distance is set to 0 when the sensor is unreadable
if (distance >= 400 || distance <= 3) {
distance = 0;
}
}
// Display data
void displayData() {
Serial.print(angle); // Display angle
Serial.print(",");
Serial.println(distance); // Display distance (cm)
}