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

Initial commit

parents
#include <SoftwareSerial.h>
#include <SPI.h>
#include <MFRC522.h>
#define alarm 2
#define TX 3
#define RX 4
#define pTrig 5
#define pEcho 6
#define RST_PIN 9
#define SDA_PIN 10
bool autoAlarm = false;
bool alarmOverride = false;
bool alarmStat = false;
bool authorized = false;
bool intruder = false;
bool entering = false;
const long enteringDelayConst = 30000;
long enteringDelay = 30000;
long double accmillis;
bool enteringCountdown = false;
SoftwareSerial bluetooth(TX, RX);
MFRC522 rfid(SDA_PIN, RST_PIN);
void setup() {
//start comm
Serial.begin(9600);
bluetooth.begin(9600);
Serial.println("Started");
//load rfid
SPI.begin();
rfid.PCD_Init();
rfid.PCD_SetAntennaGain(rfid.RxGain_max);
//set pins, turn alarm off
pinMode(alarm, OUTPUT);
pinMode(pTrig, OUTPUT);
pinMode(pEcho, INPUT);
SetAlarm(false);
Serial.println("SETUP Finished");
bluetooth.println("SETUP Finished");
}
void loop() {
accmillis = millis(); // LEAVE AT THE BEGINNING OF LOOP !!!
checkRFID();
checkIntruder();
checkBluetooth();
takeAction();
enteringTimeout(); // LEAVE AT THE END OF LOOP !!!
}
void enteringTimeout() {
if (!enteringCountdown){return;}
if (enteringDelay <= 0) {
setEntering(false);
enteringCountdown = false;
enteringDelay = enteringDelayConst;
} else {
enteringDelay -= millis() - accmillis;
}
}
void checkBluetooth() {
byte BluetoothData;
if (bluetooth.available() > 0) {
BluetoothData = bluetooth.read();
switch (BluetoothData) {
case '0':
SetAlarm(false);
alarmOverride = false;
break;
case '1':
SetAlarm(true);
alarmOverride = true;
break;
case '2':
SetAuto(true);
break;
case '3':
SetAuto(false);
break;
case '\r':
break;
case '\n':
break;
default:
bluetooth.println("unrecognized command");
break;
}
}
delay(50);
}
//does not run when auto alarm is off
//checks for an RFID around
//stop if not found or at wrong read
void checkRFID() {
if (!autoAlarm) {
return;
}
if ( (!rfid.PICC_IsNewCardPresent()) | (!rfid.PICC_ReadCardSerial()) ) {
return;
}
if( (rfid.uid.uidByte[0] == 0x66 & rfid.uid.uidByte[1] == 0xED
& rfid.uid.uidByte[2] == 0xDB & rfid.uid.uidByte[3] == 0x2B) |
(rfid.uid.uidByte[0] == 0xF1 & rfid.uid.uidByte[1] == 0x44
& rfid.uid.uidByte[2] == 0x23 & rfid.uid.uidByte[3] == 0xD9) ) {
authorized = true;
setEntering(true);
Serial.println("authorized");
bluetooth.println("authorized");
}
rfid.PICC_HaltA();
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;
digitalWrite(pTrig, LOW);
delayMicroseconds(2);
digitalWrite(pTrig, HIGH);
delayMicroseconds(5);
digitalWrite(pTrig, LOW);
response = pulseIn(pEcho, HIGH);
distance = response / 58.31;
if (distance < 60 & intruder == false) {
intruder = true;
Serial.println("someone is inside");
bluetooth.println("someone is inside");
} else if (distance > 60 & intruder == true) {
intruder = false;
Serial.println("intruder left");
bluetooth.println("intruder left");
}
}
void takeAction() {
//start alarm if an unathorized person is found
if (autoAlarm & intruder & !authorized & !alarmStat) {
SetAlarm(true);
}
//stop alarm if nobody is present
if (alarmStat & !intruder & !alarmOverride) {
SetAlarm(false);
}
//fall back to unauthorized status when the authorized person leaves
if (authorized & !intruder & !entering) {
authorized = false;
}
}
void SetAlarm(bool stat) {
if (stat) {
digitalWrite(alarm, HIGH);
alarmStat = true;
Serial.println("alarm started");
bluetooth.println("alarm started");
} else {
alarmStat = false;
digitalWrite(alarm, LOW);
Serial.println("alarm stopped");
bluetooth.println("alarm stopped");
}
}
void SetAuto(bool stat) {
if (stat) {
autoAlarm = true;
Serial.println("automatic alarm enabled");
bluetooth.println("automatic alarm enabled");
} else {
autoAlarm = false;
Serial.println("automatic alarm disabled");
bluetooth.println("automatic alarm disabled");
}
}
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