forked from SpotifaiI/parkify-sense
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscratch.ino
66 lines (49 loc) · 1.39 KB
/
scratch.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
#include <SoftwareSerial.h>
const int RX_PIN = 2;
const int TX_PIN = 3;
int is_avaliable = 1;
String Spot = "C15";
SoftwareSerial espSerial(RX_PIN, TX_PIN);
const int car_distance = 40;
const int TRIG = 12;
const int ECHO = 13;
const int ledGreen = 6;
const int ledRed = 8;
const int LDR_PIN = A2;
void setup() {
Serial.begin(9600);
espSerial.begin(9600);
pinMode(TRIG, OUTPUT);
pinMode(ECHO, INPUT);
pinMode(ledGreen, OUTPUT);
pinMode(ledRed, OUTPUT);
pinMode(LDR_PIN, INPUT);
}
void loop() {
int distance = sensor(TRIG, ECHO);
int ldrValue = analogRead(LDR_PIN);
if (distance <= car_distance && ldrValue > 600) {
is_avaliable = 0;
digitalWrite(ledGreen, LOW);
digitalWrite(ledRed, HIGH);
} else {
is_avaliable = 1;
digitalWrite(ledGreen, HIGH);
digitalWrite(ledRed, LOW);
}
String mensagem = "light=" + String(ldrValue) +
"&distance="+ String(distance) +
"&is_available=" + String(is_avaliable) +
"&parking_slot=" + Spot;
Serial.println(mensagem);
espSerial.println(mensagem);
delay(2000);
}
int sensor(int pinotrig, int pinoecho) {
digitalWrite(pinotrig, LOW);
delayMicroseconds(2);
digitalWrite(pinotrig, HIGH);
delayMicroseconds(10);
digitalWrite(pinotrig, LOW);
return pulseIn(pinoecho, HIGH) / 58;
}