/*
The Project is about smart home system it's name is "HomeBot".
---------------
At the door of the house there is a keypad to enter the password.
If the answer is correct, the green LED lights up and the door opens
(moving the servo motor 90 degrees).
If the answer is incorrect, the red LED lights up and a buzzer sounds,
writing on the LCD to enter the password again.
---------------------
Inside the house,
we measure the intensity of light in the place
so that we can control the intensity of the house lights.
We also measure the percentage of gas in the air so that in case of danger,
we open all the windows and doors using the Servo motor.
__________________________________________________
Note: We faced a problem with the number of pins in the Arduino,
so we cancelled the 2 LEDs and worked on the Buzzer only.
We also used one Servo motor and made it the door function at the keyboard (before entering)
and the windows function and all the doors in the house (after entering).
AND ANY HIDING CODE WAS FOR THAT REASON!
*/
#include <Keypad.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Servo.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
Servo myservo;
// Servo doorServo;
const byte rows[4] = {13,12,11,10};
const byte cols[4] = {9,8,7,6};
char keys[4][4] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
Keypad mykeypad = Keypad(makeKeymap(keys), rows, cols, 4, 4);
// The right Password
const String correctPassword = "1234";
String enteredPassword = "";
const int maxPasswordLength = 8;
// Pin definitions
// const int servoPin = 3;
// const int greenLed = 1; // Correct password LED
// const int redLed = 3; // Wrong password LED
const int buzzerPin = 2;
// Servo positions
const int lockedPosition = 0;
const int unlockedPosition = 90;
bool isLocked = true; // System starts locked
bool inDoor = false;
int ldrPin = A0;
int ldrValue = 0;
int ledPin = 3;
int ledValue = 0;
int gaspin = 4;
void setup() {
Serial.begin(9600);
// Initialize pins
// pinMode(greenLed, OUTPUT);
// pinMode(redLed, OUTPUT);
pinMode(buzzerPin, OUTPUT);
//LCD setup
lcd.init();
lcd.clear();
lcd.backlight();
// Attach door servo and set to locked position
// doorServo.attach(servoPin);
// doorServo.write(lockedPosition);
// Initial state: red LED on (system locked)
// digitalWrite(redLed, LOW);
// digitalWrite(greenLed, LOW);
Serial.println("Security System Initialized");
Serial.println("System LOCKED");
Serial.println("Enter password (# to confirm, * to clear):");
//Home Sensors
pinMode(ledPin, OUTPUT);
pinMode(gaspin, INPUT);
myservo.attach(5);
myservo.write(lockedPosition);
}
void loop() {
if(inDoor==false){
char myKey = mykeypad.getKey();
if (myKey) {
handleKeyPress(myKey);
}
}
if(inDoor==true){
int gasValue = digitalRead(gaspin);
if (gasValue == HIGH) {
myservo.write(180);
} else {
myservo.write(90);
}
ldrValue = analogRead(ldrPin);
ledValue = map(ldrValue, 0, 1023, 0, 255);
analogWrite(ledPin, ledValue);
lcd.setCursor(0, 0);
lcd.print("LDR: ");
lcd.print(ldrValue);
lcd.print(" ");
lcd.setCursor(0, 1);
lcd.print("LED: ");
lcd.print(ledValue);
lcd.print(" Gas: ");
lcd.print(gasValue);
Serial.print("LDR: ");
Serial.print(ldrValue);
Serial.print(" | LED: ");
Serial.print(ledValue);
Serial.print(" | Gas: ");
Serial.println(gasValue);
delay(500);
}
}
void handleKeyPress(char key) {
switch(key) {
case '#': // Enter key - check password
checkPassword();
break;
case '*': // Clear key
clearPassword();
lcd.clear();
lcd.print("Password cleared");
delay(1000);
lcd.clear();
lcd.print("Enter password: ");
lcd.setCursor(0,1);
Serial.println("Password cleared");
Serial.println("Enter password:");
break;
default: // Regular keys
if (enteredPassword.length() < maxPasswordLength) {
enteredPassword += key;
lcd.print("*");
Serial.print("*"); // Show asterisks for security
shortBeep(); // Feedback beep for each keypress
} else {
lcd.clear();
lcd.print("Maximum password");
lcd.setCursor(0,1);
lcd.print("length reached!!");
delay(2000);
lcd.clear();
Serial.println("\nMaximum password length reached!");
shortBeep();
}
break;
}
}
void checkPassword() {
lcd.clear();
Serial.println(); // New line after password entry
if (enteredPassword == correctPassword) {
correctPasswordAction();
} else {
wrongPasswordAction();
}
clearPassword(); // Reset for next attempt
}
void correctPasswordAction() {
lcd.print("Access GRANTED!");
Serial.println("Access GRANTED! Unlocking...");
// Visual feedback
// digitalWrite(greenLed, HIGH);
// digitalWrite(redLed, LOW);
// Audio feedback - success tone
successBeep();
// Unlock servo - rotate to unlocked position
myservo.write(unlockedPosition);
isLocked = false;
lcd.setCursor(0,1);
lcd.print("System UNLOCKED");
Serial.println("System UNLOCKED");
delay(3000); // Keep unlocked for 3 seconds
// Relock automatically after delay
relockSystem();
inDoor = true;
}
void wrongPasswordAction() {
lcd.clear();
lcd.print("Access DENIED!");
lcd.setCursor(0,1);
lcd.print("Wrong password.");
Serial.println("Access DENIED! Wrong password.");
// Visual feedback - red LED blinks
// digitalWrite(greenLed, LOW);
// for (int i = 0; i < 3; i++) {
// digitalWrite(redLed, HIGH);
// delay(200);
// digitalWrite(redLed, LOW);
// delay(200);
// }
// digitalWrite(redLed, HIGH);
// Audio feedback - error buzz
errorBuzz();
delay(500);
lcd.clear();
lcd.print("Enter it again:");
lcd.setCursor(0,1);
Serial.println("Enter password again:");
inDoor= false;
}
void relockSystem() {
lcd.clear();
lcd.print("Relocking door..");
Serial.println("Relocking system...");
// Rotate servo back to locked position
myservo.write(lockedPosition);
isLocked = true;
// Visual feedback
// digitalWrite(greenLed, LOW);
// digitalWrite(redLed, HIGH);
shortBeep(); // Confirm relock
lcd.setCursor(0,1);
lcd.print("System LOCKED");
Serial.println("System LOCKED");
delay(500);
lcd.clear();
// lcd.print("Enter password:");
// lcd.setCursor(0,1);
// Serial.println("Enter password:");
}
void clearPassword() {
enteredPassword = "";
}
// Audio feedback functions
void shortBeep() {
tone(buzzerPin, 1500, 100);
delay(150);
noTone(buzzerPin);
}
void successBeep() {
tone(buzzerPin, 2000, 200);
delay(250);
tone(buzzerPin, 2500, 300);
delay(350);
noTone(buzzerPin);
}
void errorBuzz() {
for (int i = 0; i < 3; i++) {
tone(buzzerPin, 1000, 300);
delay(400);
}
noTone(buzzerPin);
}
// Optional: Manual lock/unlock functions
void unlockSystem() {
if (isLocked) {
myservo.write(unlockedPosition);
isLocked = false;
// digitalWrite(greenLed, HIGH);
// digitalWrite(redLed, LOW);
lcd.clear();
lcd.print("UNLOCKED..");
Serial.println("Manually UNLOCKED");
delay(500);
lcd.clear();
}
}
void lockSystem() {
if (!isLocked) {
myservo.write(lockedPosition);
isLocked = true;
// digitalWrite(greenLed, LOW);
// digitalWrite(redLed, HIGH);
lcd.clear();
lcd.print("LOCKED..");
Serial.println("Manually LOCKED");
delay(500);
lcd.clear();
}
}