#include #include // Initialize the LCD (pins 12, 5, 4, 3, 2, 1) LiquidCrystal lcd(12, 5, 4, 3, 2, 1); // Set the correct code const String correctCode = "1234"; // Define bulb pin const int bulbPin = 13; // Keypad configuration 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] = {A0, A1, 11, 10}; byte colPins[COLS] = {9, 8, 7, 6}; // Initialize keypad Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS); String enteredCode = ""; // Store entered code int attempts = 0; // Count failed attempts void setup() { pinMode(bulbPin, OUTPUT); digitalWrite(bulbPin, LOW); // Ensure bulb is off initially lcd.begin(16, 2); lcd.print("Enter Code:"); } void loop() { char key = keypad.getKey(); if (key) { // Clear the LCD before showing the key lcd.clear(); // Handle special keys if (key == '#') { // Check if entered code matches if (enteredCode == correctCode) { lcd.print("Access Granted"); digitalWrite(bulbPin, HIGH); // Turn on bulb delay(5000); // Keep bulb on for 5 seconds digitalWrite(bulbPin, LOW); // Turn off bulb enteredCode = ""; // Reset the entered code attempts = 0; // Reset the attempts lcd.clear(); lcd.print("Enter Code:"); } else { // Wrong code entered attempts++; lcd.print("Access Denied"); if (attempts >= 3) { // Lock system after 3 attempts lcd.clear(); lcd.print("System Locked"); while (true); // Halt the program indefinitely } else { delay(2000); lcd.clear(); lcd.print("Try Again:"); } enteredCode = ""; // Reset the entered code } } else if (key == '*') { // Clear the entered code enteredCode = ""; lcd.print("Cleared"); delay(2000); lcd.clear(); lcd.print("Enter Code:"); } else { // Append the entered key to the code enteredCode += key; lcd.print(enteredCode); } } }