-
Notifications
You must be signed in to change notification settings - Fork 0
/
radar_code.ino
101 lines (81 loc) · 1.92 KB
/
radar_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
#include <Servo.h>.
const int trigPin = 8;
const int echoPin = 9;
const int led = 4;
long timetaken;
int objectdistance;
int pos = 0;
Servo servo1;
void setup() {
pinMode(led, OUTPUT);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(led, OUTPUT);
Serial.begin(9600);
servo1.attach(7);
digitalWrite(led, HIGH);
}
void loop() {
for(pos = 0; pos < 180; pos++)
{
servo1.write(pos);
delay(50);
objectdistance = distance();
if (10<objectdistance<15)
{
digitalWrite(led, LOW);
delay(1000);
digitalWrite(led, HIGH);
}else if ( 0<objectdistance<10)
{
servo1.write(pos);
delay(5000);
for(pos = 0; pos < 5; pos++) {
digitalWrite(led, LOW);
delay(100);
digitalWrite(led, HIGH);
}
Serial.print(pos);
Serial.print("here something");
}
else continue;
}
for(pos = 180; pos>=1; pos-=1)
{
servo1.write(pos);
delay(40);
objectdistance= distance();
if (10<objectdistance<15)
{
digitalWrite(led, LOW);
delay(1000);
digitalWrite(led, HIGH);
}else if ( 0<objectdistance<10)
{
servo1.write(pos);
delay(5000);
for(pos = 0; pos < 5; pos += 1) {
digitalWrite(led, LOW);
delay(100);
digitalWrite(led, HIGH);
}
Serial.print(pos);
Serial.print("here something");
Serial.print(objectdistance);
Serial.print("cm");
}
else continue;
Serial.print(objectdistance);
Serial.print("cm");
Serial.print(",");
}
}
int distance(){ digitalWrite(trigPin, LOW);
delayMicroseconds(5);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
timetaken = pulseIn(echoPin, HIGH);
int objectdistanced = (timetaken*0.034/2);
return objectdistance;
}