// Include required libraries
#include <LiquidCrystal_I2C.h>
#include <Keypad.h>
#include <SPI.h>
#include <EEPROM.h>
// Create instances
LiquidCrystal_I2C lcd(0x27, 16, 2); // LCD
const int buzzer = 2;// the pin that the buzzer is atteched to
const int pir = 12; // the pin that the sensor is atteched to
int state = LOW; // by default, no motion detected
int val = 0; // variable to store the sensor status (value)
#define AO_PIN A0 // the pin that the ldr is atteched to
const int ledone = 3; // led1 pin
const int ledtwo = 4; // led2 pin
int light = 0; // Variable to store light intensity
char initial_passwordA[4] = {'1', '6', '5', '8'}; // Variable to store initial password
char initial_passwordB[4] = {'2', '0', '9', '5'};
char passwordA[4]; // Variable to store users password
char passwordB[4]; // Variable to store users password
char key_pressed = 0; // Variable to store incoming keys
int i = 0; // Variable used for counter
// defining how many rows and columns our keypad have
const byte rows = 4;
const byte columns = 4;
// Keypad pin map
char hexaKeys[rows][columns] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
// Initializing pins for keypad
byte row_pins[rows] = {8, 7, 6, 5};
byte column_pins[columns] = {A3, A2, A1};
// Create instance for keypad
Keypad keypad_key = Keypad( makeKeymap(hexaKeys), row_pins, column_pins, rows, columns);
void setup()
{
pinMode(ledone, OUTPUT);
pinMode(ledtwo, OUTPUT);
pinMode(buzzer, OUTPUT); // initalize buzzer as an output
pinMode(pir, INPUT); // initialize sensor as an input
Serial.begin(9600); // initialize serial communication
lcd.begin(0x27, 16, 2); // LCD screen
lcd.backlight();
// lcd.setCursor(0, 0);
SPI.begin(); // Initialize SPI bus
lcd.clear();
}
void loop()
{
light = analogRead(AO_PIN); // read ldr value
val = digitalRead(pir); // read sensor value
if (val == HIGH) // check if the sensor is HIGH
{
digitalWrite(buzzer, HIGH); // turn LED ON
delay(100); // delay 100 milliseconds
if (state == LOW)
{
//lcd.clear();
state = HIGH; // update variable state to HIGH
lcd.setCursor(0, 1);
lcd.print("Motion detected!"); // print motion detection
delay(3000);
}
}
else
{
digitalWrite(buzzer, LOW); // turn LED OFF
delay(200); // delay 200 milliseconds
if (state == HIGH)
{
lcd.setCursor(0, 1);
lcd.clear();
state = LOW; // update variable state to LOW
}
}
if (light > 512)
{
//digitalWrite(ledone, HIGH);
//digitalWrite(ledtwo, LOW);
lcd.setCursor(0, 0);
lcd.print("Enter Pass1_");
}
else
{
// digitalWrite(ledone, LOW);
//digitalWrite(ledtwo, HIGH);
lcd.setCursor(0, 0);
lcd.print("Enter Pass2_");
key_pressed = keypad_key.getKey(); // Storing keys
if (key_pressed)
{
passwordB[i++] = key_pressed; // Storing in password variable
lcd.print("#");
}
if (i == 4) // If 4 keys are completed
{
// delay(200);
if (!(strncmp(passwordB, initial_passwordB, 4))) // If password is matched
{
lcd.setCursor(0, 0);
lcd.clear();
lcd.print("Pass Accepted");
// sg90.write(90); // Door Opened
digitalWrite(ledtwo, HIGH);
delay(3000);
digitalWrite(ledtwo, LOW);
//sg90.write(0); // Door Closed
lcd.clear();
i = 0;
}
else // If password is not matched
{
lcd.setCursor(0, 0);
lcd.clear();
lcd.print("Wrong Password");
// digitalWrite(buzzerPin, HIGH);
digitalWrite(ledone, HIGH);
delay(3000);
// digitalWrite(buzzerPin, LOW);
digitalWrite(ledone, LOW);
lcd.clear();
i = 0;
// RFIDMode = true; // Make RFID mode true
}
}
}
}