lolock.ino
1.37 KB
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
#include <Servo.h>
#include <LoRaShield.h>
#include <SoftwareSerial.h>
#define LoRaBaud 38400
#define LoRaRx 10
#define LoRaTx 11
#define DigitalPush 2
#define ServoPin 9
#define OPEN 0
#define CLOSE 180
LoRaShield LoRa(LoRaRx, LoRaTx);
int MotorStatus[2];
Servo servo;
void setup() {
LoRa.begin(LoRaBaud);
Serial.begin(115200);
pinMode(LoRaTx, OUTPUT);
pinMode(DigitalPush, INPUT_PULLUP);
servo.attach(ServoPin);
servo.write(CLOSE);
attachInterrupt(digitalPinToInterrupt(DigitalPush), digitalPushInterrupt, CHANGE);
Serial.println("SETUP COMPLETE");
Serial.println(digitalRead(2));
}
void loop(){
LoRa.listen();
String LoRa_Text, LoRa_Message;
while(LoRa.available()) {
LoRa_Text = LoRa.ReadLine();
LoRa_Message = LoRa.GetMessage();
if(LoRa_Message == "26") {
Serial.println("Door Open");
doorOpen();
}
}
if(MotorStatus[0] == 1 && MotorStatus[1] == 1) {
doorOpen();
MotorStatus[0] = 0;
MotorStatus[1] = 0;
}
}
void doorOpen() {
int i;
for(i = 0 ; i < 180 ;i++ ) {
servo.write(i);
delay(2);
}
delay(400);
for(i = 180 ; i >0 ;i-- ) {
servo.write(i);
delay(2);
}
}
void digitalPushInterrupt() {
MotorStatus[digitalRead(2)] = 1;
Serial.print("MOTOR1 ");
Serial.println(MotorStatus[0]);
Serial.print("MOTOR2 ");
Serial.println(MotorStatus[1]);
}