lolock.ino 1.37 KB
#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]);
}