#include <Keypad.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
const int circuitBreakerPin = 13; // Pin connected to the circuit breaker relay
const int ledPin = 12; // Pin connected to an indicator LED
const int buzzerPin = 11; // Pin connected to a buzzer
const byte ROW_NUM = 4; // four rows
const byte COLUMN_NUM = 4; // four columns
char keys[ROW_NUM][COLUMN_NUM] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
byte pin_rows[ROW_NUM] = {9, 8, 7, 6}; // connect to the row pinouts of the keypad
byte pin_column[COLUMN_NUM] = {5, 4, 3, 2}; // connect to the column pinouts of the keypad
Keypad keypad = Keypad(makeKeymap(keys), pin_rows, pin_column, ROW_NUM, COLUMN_NUM);
LiquidCrystal_I2C lcd(0x27, 16, 2); // I2C address 0x27, 16 columns and 2 rows
String defaultPassword = "1234";
String enteredPassword = "";
void setup() {
Serial.begin(9600);
pinMode(circuitBreakerPin, OUTPUT);
pinMode(ledPin, OUTPUT);
pinMode(buzzerPin, OUTPUT);
lcd.begin(16, 2); // Initialize the LCD
lcd.init(); // Initialize the display
lcd.backlight(); // Turn on the backlight
lcd.setCursor(0, 0);
lcd.print("Welcome to");
lcd.setCursor(0, 1);
lcd.print("Smart System!");
delay(3000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("To change the");
lcd.setCursor(0, 1);
lcd.print("Password click A>");
delay(3000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Enter Password");
lcd.setCursor(0, 1);
lcd.print("----------------");
}
void loop() {
char key = keypad.getKey();
if (key) {
beep(); // Beep for every key press
if (key == '#') {
handleAccessRequest();
} else if (key == 'A') {
handlePasswordChange();
} else {
appendToEnteredPassword(key);
lcd.setCursor(enteredPassword.length(), 1);
lcd.print('*');
}
}
}
void handleAccessRequest() {
if (enteredPassword == defaultPassword) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Access Granted");
if (digitalRead(circuitBreakerPin) == HIGH) {
lcd.setCursor(0, 1);
lcd.print("Circuit OFF");
} else {
lcd.setCursor(0, 1);
lcd.print("Circuit ON");
}
toggleCircuitBreaker();
beepSuccess(); // Beep for correct password
delay(2000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Enter Password");
lcd.setCursor(0, 1);
lcd.print("----------------");
clearEnteredPassword();
} else {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Invalid Password");
lcd.setCursor(0, 1);
lcd.print("Try Again!");
beepError(); // Beep for incorrect password
delay(2000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Enter Password");
lcd.setCursor(0, 1);
lcd.print("----------------");
clearEnteredPassword();
}
}
void toggleCircuitBreaker() {
digitalWrite(circuitBreakerPin, !digitalRead(circuitBreakerPin));
digitalWrite(ledPin, digitalRead(circuitBreakerPin));
}
void appendToEnteredPassword(char key) {
if (enteredPassword.length() < 4) {
enteredPassword += key;
}
}
void clearEnteredPassword() {
enteredPassword = "";
lcd.setCursor(0, 1);
lcd.print(" ");
delay(500);
}
void handlePasswordChange() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Change Password");
lcd.setCursor(0, 1);
lcd.print("Enter New Pass");
String newPassword = "";
for (byte i = 0; i < 4; i++) {
char newKey = keypad.getKey();
while (!newKey) {
newKey = keypad.getKey();
}
lcd.print('*');
newPassword += newKey;
}
defaultPassword = newPassword;
beepSuccess(); // Beep for password change success
delay(500);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Password Changed");
lcd.setCursor(0, 1);
lcd.print("Access Granted");
delay(2000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Enter Password");
lcd.setCursor(0, 1);
lcd.print("----------------");
clearEnteredPassword();
}
void beep() {
tone(buzzerPin, 1000, 50); // Beep for every key press
}
void beepSuccess() {
tone(buzzerPin, 1500, 100); // Beep for success
}
void beepError() {
tone(buzzerPin, 500, 200); // Beep for error
}