#include <Keypad.h>
#include <LiquidCrystal.h>
// Define the correct password
const String correctPassword = "1234";
String inputPassword = "";
// Define pins for RGB LED, LED, and relay
#define RGB_RED_PIN 19
#define RGB_GREEN_PIN 18
#define RGB_BLUE_PIN 14
#define LED_PIN 16
#define RELAY_PIN 17
// Define pins for the LCD (RS, E, D4, D5, D6, D7)
#define LCD_RS 28
#define LCD_E 27
#define LCD_D4 26
#define LCD_D5 22
#define LCD_D6 21
#define LCD_D7 20
// Initialize the LCD
LiquidCrystal lcd(LCD_RS, LCD_E, LCD_D4, LCD_D5, LCD_D6, LCD_D7);
// 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] = {0, 1, 2, 3}; // Connect to the row pinouts of the keypad
byte colPins[COLS] = {4, 5, 6, 7}; // Connect to the column pinouts of the keypad
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
void setup() {
// Initialize pins
pinMode(RGB_RED_PIN, OUTPUT);
pinMode(RGB_GREEN_PIN, OUTPUT);
pinMode(RGB_BLUE_PIN, OUTPUT);
pinMode(LED_PIN, OUTPUT);
pinMode(RELAY_PIN, OUTPUT);
// Initialize LCD
lcd.begin(16, 2); // Set up LCD with 16 columns and 2 rows
lcd.setCursor(0, 0);
lcd.print("Enter Password:");
}
void resetRGB() {
digitalWrite(RGB_RED_PIN, LOW);
digitalWrite(RGB_GREEN_PIN, LOW);
digitalWrite(RGB_BLUE_PIN, LOW);
}
void displayMessage(const char* message) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(message);
}
void checkPassword() {
if (inputPassword == correctPassword) {
digitalWrite(RGB_GREEN_PIN, HIGH);
digitalWrite(RGB_RED_PIN, LOW);
digitalWrite(LED_PIN, HIGH);
digitalWrite(RELAY_PIN, HIGH);
displayMessage("Access Granted");
} else {
digitalWrite(RGB_RED_PIN, HIGH);
digitalWrite(RGB_GREEN_PIN, LOW);
digitalWrite(LED_PIN, LOW);
digitalWrite(RELAY_PIN, LOW);
displayMessage("Access Denied");
}
}
void loop() {
char key = keypad.getKey();
if (key) {
if (key == '#') { // Confirm password input
checkPassword();
delay(2000); // Wait 2 seconds
resetRGB();
displayMessage("Enter Password:");
inputPassword = ""; // Reset password
}
else if (key == '*') { // Reset password input
inputPassword = "";
displayMessage("Enter Password:");
}
else { // Append the pressed key to the password
inputPassword += key;
lcd.clear();
lcd.setCursor(0, 0);
for (unsigned int i = 0; i < inputPassword.length(); i++) {
lcd.print('*'); // Display * for each character entered
}
}
}
}