-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlora_transmitter.ino
63 lines (48 loc) · 1.26 KB
/
lora_transmitter.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
#include "Ultrasonic.h"
#include "SoftwareSerial.h"
#define RX 2
#define TX 3
SoftwareSerial lora(RX, TX);
Ultrasonic ultrasonic(7, 8);
int distance, last_distance;
uint8_t speed, last_speed;
unsigned long time, last_time, last_accident;
const byte ID = B01000000;
const byte accident_check = B00100000;
byte b_speed = "00011010";
byte packet;
bool hasCrashed = false;
void setup() {
Serial.begin(9600);
lora.begin(9600);
Serial.println("[*] Initializing transmitter setup.");
last_time = 0;
last_distance = ultrasonic.read();
Serial.println("[*] Setup done.");
}
void loop() {
distance = ultrasonic.read();
time = millis();
if((time - last_time) >= 50){
last_speed = speed;
speed = (distance - last_distance) / (time - last_time);
b_speed = byte(max(speed, last_speed));
last_time = time;
if(distance <= 5){
if(!hasCrashed){
hasCrashed = true;
last_accident = time;
Serial.println("[*] Accident detected.");
packet = ID | accident_check | b_speed;
lora.write(packet);
Serial.println("[*] Packet sent.");
delay(500);
}
} else {
if(hasCrashed && (time - last_accident >= 7000)){
hasCrashed = false;
}
}
//Serial.println(distance);
}
}