Commit abfd51b4 authored by Ondřej Kučera's avatar Ondřej Kučera
Browse files

fix issue #3 & minor improvements

parent 770ad4e2
......@@ -16,10 +16,14 @@ bool alarmStat = false;
bool authorized = false;
bool intruder = false;
bool entering = false;
const long enteringDelayConst = 30000;
long enteringDelay = 30000;
const long enteringDelayConst = 15000;
long enteringDelay = 15000;
bool entered = false;
bool leaving = false;
const long leavingDelayConst = 10000;
long leavingDelay = 10000;
long double accmillis;
bool enteringCountdown = false;
long double lastIntruderTime;
SoftwareSerial bluetooth(TX, RX);
MFRC522 rfid(SDA_PIN, RST_PIN);
......@@ -55,23 +59,37 @@ void loop() {
checkBluetooth();
takeAction();
leavingTimeout(); // LEAVE AT THE END OF LOOP !!!
enteringTimeout(); // LEAVE AT THE END OF LOOP !!!
}
void enteringTimeout() {
if (!enteringCountdown) {
if (!entering) {
return;
}
if (enteringDelay <= 0) {
setEntering(false);
enteringCountdown = false;
entering = false;
entered = true;
enteringDelay = enteringDelayConst;
} else {
enteringDelay -= millis() - accmillis;
}
}
void leavingTimeout() {
if (!leaving) {
return;
}
if (leavingDelay <= 0) {
leaving = false;
authorized = false;
leavingDelay = leavingDelayConst;
} else {
leavingDelay -= millis() - accmillis;
}
}
void checkBluetooth() {
......@@ -126,7 +144,7 @@ void checkRFID() {
(rfid.uid.uidByte[0] == 0xF1 & rfid.uid.uidByte[1] == 0x44
& rfid.uid.uidByte[2] == 0x23 & rfid.uid.uidByte[3] == 0xD9) ) {
authorized = true;
setEntering(true);
entered = false;
Serial.println("authorized");
bluetooth.println("authorized");
}
......@@ -135,19 +153,6 @@ void checkRFID() {
rfid.PCD_StopCrypto1();
}
void setEntering(bool stat) {
if (stat) {
entering = true;
enteringCountdown = true;
} else {
entering = false;
}
}
void setEnteringFalse() {
entering = false;
}
void checkIntruder() {
long response, distance;
......@@ -162,10 +167,12 @@ void checkIntruder() {
if (distance < 60 & intruder == false) {
intruder = true;
lastIntruderTime = accmillis;
Serial.println("someone is inside");
bluetooth.println("someone is inside");
} else if (distance > 60 & intruder == true) {
} else if (distance > 60 & intruder == true & (accmillis - lastIntruderTime) > 4000) {
intruder = false;
lastIntruderTime = accmillis;
Serial.println("intruder left");
bluetooth.println("intruder left");
}
......@@ -178,14 +185,23 @@ void takeAction() {
SetAlarm(true);
}
//stop alarm if nobody is present
//stop alarm if nobody is present; respect manual alarm
if (alarmStat & !intruder & !alarmOverride) {
SetAlarm(false);
}
//fall back to unauthorized status when the authorized person leaves
if (authorized & !intruder & !entering) {
authorized = false;
//start an entering timeout to let an authorized person get in
if (authorized & !entered & !entering & !leaving) {
entering = true;
Serial.println("entering started");
bluetooth.println("entering started");
}
//start a leaving timeout to let an authorized person leave
if (authorized & !intruder & entered & !entering & !leaving) {
leaving = true;
Serial.println("leaving started");
bluetooth.println("leaving started");
}
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment