#include <Keypad.h>
#include <LiquidCrystal_I2C.h>
// Constants and variables
const String pass1 = "1234";
const String pass2 = "5678";
const String pass3 = "9012";
const String pass4 = "3456"; // New password for the fourth relay
const int ledPin1 = 12;
const int ledPin2 = 11;
const int ledPin3 = 10;
const int ledPin4 = 13; // New pin for the fourth relay
String readString;
bool led1_on = false;
bool led2_on = false;
bool led3_on = false;
bool led4_on = false; // New state variable for the fourth relay
// LCD setup
LiquidCrystal_I2C lcd(0x27, 16, 2);
// Keypad setup
const byte ROWS = 4;
const byte COLS = 4;
char keys[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
byte rowPins[ROWS] = {9, 8, 7, 6};
byte colPins[COLS] = {5, 4, 3, 2};
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
void setup() {
Serial.begin(9600);
lcd.init();
lcd.backlight();
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, OUTPUT);
pinMode(ledPin4, OUTPUT); // Initialize the fourth relay pin
digitalWrite(ledPin1, HIGH);
digitalWrite(ledPin2, HIGH);
digitalWrite(ledPin3, HIGH);
digitalWrite(ledPin4, HIGH); // Set the fourth relay pin to HIGH
lcd.setCursor(0, 0);
lcd.print("PASSWORD BASED");
lcd.setCursor(0, 1);
lcd.print("CIRCUIT BREAKER");
delay(2500);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("#BVRIT...");
delay(2500);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("WELCOME ");
delay(1500);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Starting.....");
delay(2000);
lcd.clear();
}
void loop() {
lcd.setCursor(0, 0);
lcd.print("Enter password:");
char key = keypad.getKey();
if (key != NO_KEY) {
Serial.print("Key pressed: ");
Serial.println(key); // Debug: Print the key to Serial Monitor
if (key == 'B') {
readString = "";
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Enter password:");
} else if (key == 'C') { // Use 'C' to submit the password
handlePassword();
} else {
readString += key;
lcd.setCursor(readString.length() - 1, 1);
lcd.print('*');
}
}
}
void handlePassword() {
if (readString == pass1) {
toggleLED(ledPin1, led1_on);
} else if (readString == pass2) {
toggleLED(ledPin2, led2_on);
} else if (readString == pass3) {
toggleLED(ledPin3, led3_on);
} else if (readString == pass4) { // Check the new password
toggleLED(ledPin4, led4_on); // Toggle the fourth relay
} else {
lcd.clear();
lcd.setCursor(0, 1);
lcd.print("WRONG PASSWORD");
delay(1000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Enter password:");
}
readString = "";
}
void toggleLED(int pin, bool &led_on) {
lcd.clear();
if (led_on) {
lcd.setCursor(0, 1);
lcd.print("TURNING OFF");
digitalWrite(pin, HIGH);
} else {
lcd.setCursor(0, 1);
lcd.print("TURNING ON");
digitalWrite(pin, LOW);
}
delay(1000);
lcd.clear();
led_on = !led_on;
lcd.setCursor(0, 0);
lcd.print("Enter password:");
}