Showing
1 changed file
with
76 additions
and
0 deletions
lolock_arduino/lolock.ino
0 → 100644
| 1 | +#include <Servo.h> | ||
| 2 | +#include <LoRaShield.h> | ||
| 3 | +#include <SoftwareSerial.h> | ||
| 4 | + | ||
| 5 | +#define LoRaBaud 38400 | ||
| 6 | +#define LoRaRx 10 | ||
| 7 | +#define LoRaTx 11 | ||
| 8 | + | ||
| 9 | +#define DigitalPush 2 | ||
| 10 | + | ||
| 11 | +#define ServoPin 9 | ||
| 12 | + | ||
| 13 | +#define OPEN 0 | ||
| 14 | +#define CLOSE 180 | ||
| 15 | + | ||
| 16 | + | ||
| 17 | +LoRaShield LoRa(LoRaRx, LoRaTx); | ||
| 18 | +int MotorStatus[2]; | ||
| 19 | +Servo servo; | ||
| 20 | + | ||
| 21 | +void setup() { | ||
| 22 | + LoRa.begin(LoRaBaud); | ||
| 23 | + Serial.begin(115200); | ||
| 24 | + pinMode(LoRaTx, OUTPUT); | ||
| 25 | + pinMode(DigitalPush, INPUT_PULLUP); | ||
| 26 | + | ||
| 27 | + servo.attach(ServoPin); | ||
| 28 | + servo.write(CLOSE); | ||
| 29 | + | ||
| 30 | + attachInterrupt(digitalPinToInterrupt(DigitalPush), digitalPushInterrupt, CHANGE); | ||
| 31 | + | ||
| 32 | + Serial.println("SETUP COMPLETE"); | ||
| 33 | + Serial.println(digitalRead(2)); | ||
| 34 | +} | ||
| 35 | + | ||
| 36 | +void loop(){ | ||
| 37 | + | ||
| 38 | + LoRa.listen(); | ||
| 39 | + String LoRa_Text, LoRa_Message; | ||
| 40 | + while(LoRa.available()) { | ||
| 41 | + LoRa_Text = LoRa.ReadLine(); | ||
| 42 | + | ||
| 43 | + LoRa_Message = LoRa.GetMessage(); | ||
| 44 | + if(LoRa_Message == "26") { | ||
| 45 | + Serial.println("Door Open"); | ||
| 46 | + doorOpen(); | ||
| 47 | + } | ||
| 48 | + } | ||
| 49 | + if(MotorStatus[0] == 1 && MotorStatus[1] == 1) { | ||
| 50 | + doorOpen(); | ||
| 51 | + MotorStatus[0] = 0; | ||
| 52 | + MotorStatus[1] = 0; | ||
| 53 | + } | ||
| 54 | +} | ||
| 55 | +void doorOpen() { | ||
| 56 | + int i; | ||
| 57 | + for(i = 0 ; i < 180 ;i++ ) { | ||
| 58 | + servo.write(i); | ||
| 59 | + delay(2); | ||
| 60 | + } | ||
| 61 | + delay(400); | ||
| 62 | + for(i = 180 ; i >0 ;i-- ) { | ||
| 63 | + servo.write(i); | ||
| 64 | + delay(2); | ||
| 65 | + } | ||
| 66 | +} | ||
| 67 | +void digitalPushInterrupt() { | ||
| 68 | + MotorStatus[digitalRead(2)] = 1; | ||
| 69 | + Serial.print("MOTOR1 "); | ||
| 70 | + Serial.println(MotorStatus[0]); | ||
| 71 | + Serial.print("MOTOR2 "); | ||
| 72 | + Serial.println(MotorStatus[1]); | ||
| 73 | +} | ||
| 74 | + | ||
| 75 | + | ||
| 76 | + |
-
Please register or login to post a comment