-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathultra_sonic.ino
59 lines (46 loc) · 1.52 KB
/
ultra_sonic.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
#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#define trig D1
#define echo D2
long duration;
int distance;
// You should get Auth Token in the Blynk App.
char auth[] = "H8Jz0eeweykqfqbV9SWwNZhMF6IjqeY9";
char ssid[] = "PPO A5 2020"; // your ssid
char pass[] = "poiuytre"; // your pass
BlynkTimer timer;
void setup()
{
// Debug console
pinMode(trig, OUTPUT); // Sets the trigPin as an Output
pinMode(echo, INPUT); // Sets the echoPin as an Inpu
Serial.begin(9600);
Blynk.begin(auth, ssid, pass);
// Setup a function to be called every second
timer.setInterval(1000L, sendSensor);
}
void loop()
{
Blynk.run();
timer.run();
}
void sendSensor()
{
digitalWrite(trig, LOW); // Makes trigPin low
delayMicroseconds(2); // 2 micro second delay
digitalWrite(trig, HIGH); // tigPin high
delayMicroseconds(10); // trigPin high for 10 micro seconds
digitalWrite(trig, LOW); // trigPin low
duration = pulseIn(echo, HIGH); //Read echo pin, time in microseconds
distance = duration * 0.034 / 2; //Calculating actual/real distance
Serial.print("Distance = "); //Output distance on arduino serial monitor
Serial.println(distance);
if(distance <= 5)
{
Blynk.tweet("My Arduino project is tweeting using @blynk_app and it’s awesome!\n #arduino #IoT #blynk");
Blynk.notify("Post has been twitted");
}
Blynk.virtualWrite(V0, distance);
delay(1000); //Pause for 3 seconds and start measuring distance again
}