-
Notifications
You must be signed in to change notification settings - Fork 4
/
radar_firebase.ino
155 lines (106 loc) · 4.73 KB
/
radar_firebase.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
148
149
150
151
152
153
154
155
#include <FirebaseArduino.h>
#include <ESP8266WiFi.h>
#include <Servo.h>
const int trigPin = D2;
const int echoPin = D3;
//attach servo to D5
Servo myservo; // create servo object to control a servo
#define FIREBASE_HOST "agricloud-7458b.firebaseio.com"
#define WIFI_SSID "vinay" // Change the name of your WIFI
#define WIFI_PASSWORD "vinay1234" // Change the password of your WIFI
void setup()
{
Serial.begin(9600);
WiFi.begin (WIFI_SSID, WIFI_PASSWORD);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println ("");
Serial.println ("WiFi Connected!");
Firebase.begin(FIREBASE_HOST);
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output]
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
myservo.attach(D5);
}
void loop()
{
int angle;
long duration;
int distance;
for(angle = 0; angle <= 90 ; angle += 10)
{ // in steps of 1 degree
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculating the distance
distance= duration*0.034/2;
// Prints the distance on the Serial Monitor
Serial.print("Distance: ");
Serial.println(distance);
Serial.print("angle: ");
Serial.println(angle);
if (distance >= 10 && angle == 10 && distance <=30 )
{
Firebase.setFloat ("box",4);
}
if (distance <= 15 && angle == 30 && distance <=30 )
{
Firebase.setFloat ("box",2);
}
if (distance <= 30 && distance >=15 && angle == 40)
{
Firebase.setFloat ("box",3);
}
if (distance <= 15 && angle == 70 && distance <=30)
{
Firebase.setFloat ("box",1);
}
myservo.write(angle); // tell servo to go to position in variable 'pos'
delay(10); // waits 15ms for the servo to reach the position
}
for(angle = 90; angle>=0; angle-=10) // goes from 180 degrees to 0 degrees
{ // in steps of 1 degree
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculating the distance
distance= duration*0.034/2;
// Prints the distance on the Serial Monitor
Serial.print("Distance: ");
Serial.println(distance);
Serial.print("angle: ");
Serial.println(angle);
if (distance >= 10 && angle == 10 && distance <=30 )
{
Firebase.setFloat ("box",4);
}
if (distance <= 15 && angle == 30 && distance <=30 )
{
Firebase.setFloat ("box",2);
}
if (distance <= 30 && distance >=15 && angle == 40)
{
Firebase.setFloat ("box",3);
}
if (distance <= 15 && angle == 70 && distance <=30)
{
Firebase.setFloat ("box",1);
}
myservo.write(angle); // tell servo to go to position in variable 'pos'
delay(100); // waits 15ms for the servo to reach the position
}
}