bool last; unsigned long int start; int red=12; int green=11; int blue=10; int hall=8; String code=""; String pass="1234"; bool in; int lock; int locklast; bool alarm; void setup() { pinMode(hall, INPUT); pinMode(blue, OUTPUT); pinMode(green, OUTPUT); pinMode(red, OUTPUT); //Used for testing. pinMode(13, OUTPUT); Serial.begin(9600); // Default communication rate of the Bluetooth module } void loop() { //Green true, when the door is closed, else false. digitalWrite(green, !digitalRead(hall)); //Blue true, when the door is open, unless red is true. if(digitalRead(red)) { digitalWrite(blue, false); } else { digitalWrite(blue, digitalRead(hall)); } //Start counter, when the door changes from being closed to open. if(digitalRead(hall)!=last) { start=millis(); } //Red true, when the door has been opened for 5 sekonds, else false. if(millis()>start+5000 && !digitalRead(green)) { digitalWrite(red, true); } else { digitalWrite(red, false); } //Helping variable to detect when the door changes from being closed to open. last=digitalRead(hall); login(); readlock(); logoff(); //Setting the alarm, if the securitysystem is locked and the door is being opened. if(lock=='1' && digitalRead(hall)) { alarm=true; } //Blinking red, as long as the alarm is set. while(alarm) { digitalWrite(blue, false); digitalWrite(green, false); digitalWrite(red, true); delay(500); digitalWrite(red, false); delay(500); login(); readlock(); logoff(); if(lock=='0') { alarm=false; } } } void login() { //Reads code parsed from a phone. if(Serial.available() && code=="") { // Checks whether data is comming from the serial port code = Serial.readString(); // Reads the data from the serial port } //Checks the code from the phone against the password. if(code==pass) { in=true; digitalWrite(13, true); } else { in=false; code=""; digitalWrite(13, false); } } void readlock() { //Helping variable for logging off. locklast=lock; //Reading lock, unlock and logoff from the phone. if(Serial.available() && in) { lock = Serial.read(); // Reads the data from the serial port } } void logoff() { //If logoff recieved reverts lock to its last state, resets the code and sets login to false. if(lock=='2') { lock=locklast; code=""; in=false; } }