#include <Wire.h>
// Include LCD display library for I2C
#include <LiquidCrystal_I2C.h>
// Include Keypad library
#include <Keypad.h>
char* password ="04567";
int position = 0;
// Pin connected to lock relay input
int pirSensor = A2;
int sensorValue = 0;
int RedLed = 13;
int BlueLed =A1;
int YellowLed = A0;
int GreenLed = 11;
int Buzzer = 12;
int SensorSwitch = 10;
int Sensor =0;
int TripState = 0;
int AlarmStatus = 0;
int SettingStatus = 0;
int Siren = A3;
int SirenStatus = 0;
int displaysetting = 0;
int i =0;
unsigned long previousMillis = 0;
unsigned long SettingPreviousMillis = 0;
unsigned long SettingInterval = 1000;
int SettingDone = 0;
unsigned long sirenPreviousMillis = 0;
unsigned long SirenInterval = 1000;
int Sirencount = 20;
int SetCount = 15;//SETS HOW LONG TO LEAVE AFTER SETTING ALARM
unsigned long interval = 1000;
int count = 15; // SETS HOW LONG TO DEACTIVATE ALARM
int redledState = LOW; // ledState used to set the LED
long ledpreviousMillis = 0; // will store last time LED was updated
long ledinterval = 1000;
long redledinterval = 500;
int greenledState = LOW;
long greenledpreviousMillis = 0; // will store last time LED was updated
long greenledinterval = 2000;
// Constants for row and column sizes
const byte ROWS = 4;
const byte COLS = 4;
// Array to represent keys on keypad
char keyMap [ROWS] [COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
// Connections to Arduino
byte rowPins [ROWS] = {9, 8, 7, 6};
byte colPins [COLS] = {5, 4, 3, 2};
Keypad myKeypad = Keypad(makeKeymap(keyMap), rowPins, colPins, ROWS, COLS);
// Create LCD object
LiquidCrystal_I2C lcd(0x27, 16, 2);
void setup() {
lcd.backlight();
lcd.begin(16,2);
lcd.print ("SYSTEM CHECK");
pinMode(pirSensor, INPUT);
pinMode (RedLed, OUTPUT);
pinMode (GreenLed, OUTPUT);
pinMode (YellowLed, OUTPUT);
pinMode (BlueLed, OUTPUT);
pinMode (Buzzer, OUTPUT);
pinMode (SensorSwitch, INPUT);
digitalWrite (RedLed, HIGH);
lcd.setCursor(0, 1);
lcd.print("*");
delay (200);
digitalWrite (BlueLed, HIGH);
lcd.print("*");
delay (200);
digitalWrite (GreenLed, HIGH);
lcd.print("*");
delay (200);
digitalWrite (YellowLed, HIGH);
lcd.print("*");
delay (200);
digitalWrite (RedLed, LOW);
lcd.print("*");
delay (200);
digitalWrite (BlueLed, LOW);
lcd.print("*");
delay (200);
digitalWrite (GreenLed, LOW);
lcd.print("*");
delay (200);
digitalWrite (YellowLed, LOW);
lcd.print("*");
delay (200);
tone (Buzzer, 5000, 300);
delay (500);
lcd.clear();
Serial.begin (9600);
tone (Siren, 4000, 200);
}
void loop() {
sensorValue = digitalRead(pirSensor);
if (sensorValue == 1) {
digitalWrite(YellowLed, HIGH); // The Relay Input works Inversly
lcd.setCursor(0,0);
lcd.print("PERSON");
lcd.setCursor(0,1);
lcd.print("DETECTED");
}
unsigned long ledcurrentMillis = millis();
unsigned long greenledcurrentMillis = millis();
char whichKey = myKeypad.getKey();
if (SettingStatus == 1){
lcd.setCursor(0,0);
lcd.print("alarm");
lcd.setCursor(0,1);
lcd.print("setting");
}
else if (TripState == 1){
lcd.setCursor(0,0);
lcd.print("INPUT PASSWORD");
lcd.setCursor(0,1);
lcd.print("TO DEACTIVATE");
}
else if (SettingDone == 1){
lcd.setCursor(0,0);
lcd.print("ALARM");
lcd.setCursor(0,1);
lcd.print("ON");
if(ledcurrentMillis - ledpreviousMillis > ledinterval) {
// save the last time you blinked the LED
ledpreviousMillis = ledcurrentMillis;
// if the LED is off turn it on and vice-versa:
if (redledState == LOW)
redledState = HIGH;
else
redledState = LOW;
// set the LED with the ledState of the variable:
digitalWrite(RedLed, redledState);
}
}
else if (sensorValue == 1 ) {
digitalWrite(YellowLed, HIGH); // The Relay Input works Inversly
tone (Buzzer, 5000, 200);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("PERSON");
lcd.setCursor(0,1);
lcd.print("DETECTED");
lcd.clear();
digitalWrite(YellowLed, LOW);
}
else {
digitalWrite(YellowLed, LOW);
lcd.setCursor(0,0);
lcd.print("STEVE'S SHED");
lcd.setCursor(0,1);
lcd.print("PRESS * TO SET");
if(greenledcurrentMillis - greenledpreviousMillis > greenledinterval) {
// save the last time you blinked the LED
greenledpreviousMillis = greenledcurrentMillis;
// if the LED is off turn it on and vice-versa:
if (greenledState == LOW)
greenledState = HIGH;
else
greenledState = LOW;
// set the LED with the ledState of the variable:
digitalWrite(GreenLed, greenledState);
}
}
if (whichKey)
tone (Buzzer, 5000, 100);
if (whichKey)
{
password[i++]=whichKey;
lcd.print(whichKey);
}
if(whichKey =='*'&& SettingDone == 0 ){
//position ++;
lcd.clear();
lcd.setCursor(0,0);
lcd.print(" locked");
delay (1000);
lcd.clear();
SettingStatus = 1;
displaysetting = 1;
position =0;
}
if (SettingStatus == 1 && SettingDone == 0){
unsigned long SettingMillis = millis();
if (SettingMillis -SettingPreviousMillis >= SettingInterval){
SettingPreviousMillis = SettingMillis;
SetCount--;
tone (Buzzer, 3000,100);
if (SetCount <=0){
//digitalWrite (RedLed, HIGH);
digitalWrite (GreenLed, LOW);
SettingStatus = 0;
SettingDone = 1;
lcd.clear();
lcd.setCursor(0,0);
lcd.print("ALARM SET");
delay (1000);
lcd.clear();
SetCount = 10;
}
}
}
if (SettingDone ==1) {
AlarmStatus = 1;
}
if (whichKey == password [position]){
position ++;
}
if (position == 5){
AlarmStatus = 0;
SettingStatus = 0 ;
SettingDone = 0;
TripState = 0; // resets the tripswitch
SirenStatus = 0; // turns off the Siren
count = 10; // resets the timer to 10 seconds
digitalWrite (GreenLed, HIGH);
digitalWrite (YellowLed, LOW);
digitalWrite (RedLed, LOW);
tone (Buzzer, 5000,1000);
lcd.clear();
lcd.setCursor(0,0);
lcd.print(" unlocked");
delay(1000);
position = 0;
lcd.clear();
}
Sensor = digitalRead (SensorSwitch);
if (Sensor == LOW && SettingDone == 1 ){
digitalWrite (BlueLed, HIGH);// shows that door is open
TripState = 1;
if(ledcurrentMillis - ledpreviousMillis > redledinterval) {
// save the last time you blinked the LED
ledpreviousMillis = ledcurrentMillis;
// if the LED is off turn it on and vice-versa:
if (redledState == LOW)
redledState = HIGH;
else
redledState = LOW;
// set the LED with the ledState of the variable:
digitalWrite(RedLed, redledState);
}
}
else
{
digitalWrite (BlueLed, LOW);
}
if (TripState == 1 && AlarmStatus == 1){
//digitalWrite (YellowLed, HIGH); //indicates the door has been open, waiting for reset!
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval){
previousMillis = currentMillis;
Serial.println (count);
count--;
tone (Buzzer, 4000,200);
if (count <=0){
SirenStatus = 1;
}
}
}
if (SirenStatus == 1){
digitalWrite (YellowLed ,HIGH); // sounds the alarm
tone (Siren, 4000,200);
unsigned long sirenCurrentMillis = millis();
if (sirenCurrentMillis - sirenPreviousMillis >= SirenInterval){
sirenPreviousMillis = sirenCurrentMillis;
Serial.println (Sirencount);
Sirencount++;
if (Sirencount >=60){ //sets how long the alarm sounds before reseting its self
SirenStatus = 0;
TripState = 0;
digitalWrite (YellowLed ,LOW);
digitalWrite (Siren,LOW);
Sirencount = 0;
}
}
}
if (sensorValue == 1 && AlarmStatus == 1) {
digitalWrite(YellowLed, HIGH); // The Relay Input works Inversly
tone (Buzzer, 5000, 200);
lcd.clear();
digitalWrite(YellowLed, LOW);
}
}