#include <Servo.h>
#include <Keypad.h>
#include <LiquidCrystal.h>
// 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] = { 2, 3, 24, 5 };
byte colPins[COLS] = { 6, 7, 8, 9 };
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
// Servo configuration
Servo myservo;
// Analog input and button configuration
const int analogPin = A0; // Analog input pin
const int buttonPin = 53; // Button pin
bool startProcess = false;
// LCD configuration
LiquidCrystal lcd(4, 10, 11, 12, 13, 14); // rs, en, d4, d5, d6, d7
// Passwords and input code
String inputCode = "";
const String pass1 = "3141";
const String pass2 = "1009";
const String pass3 = "1729";
void setup() {
Serial.begin(9600);
// Ensure the LCD is initialized correctly
lcd.begin(16, 2);
myservo.attach(22);
myservo.write(0); // Initial position
pinMode(buttonPin, INPUT_PULLUP);
pinMode(analogPin, INPUT); // Set analog pin as input
lcd.print("System Ready");
Serial.println("System Ready"); // Debugging output
}
void loop() {
// Check if the button is pressed to start the process
if (digitalRead(buttonPin) == LOW && !startProcess) {
startProcess = true;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Enter Passcode :"); // Move servo to open position
delay(1000); // Ensure enough delay for the servo to reach the position
lcd.setCursor(0, 1);
lcd.noAutoscroll();
Serial.println("Start Process: Enter password");
}
if (startProcess) {
char key = keypad.getKey();
if (key != NO_KEY) {
Serial.print("Key Pressed: ");
Serial.println(key);
inputCode += key;
delay(50); // Debounce delay
lcd.setCursor(0, 1);
lcd.print(inputCode);
}
// Check if the entered code is 4 characters long
if (inputCode.length() == 4) {
Serial.println("Checking password");
if (inputCode == pass1 || inputCode == pass2 || inputCode == pass3) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Access Granted");
Serial.println("ACCESS GRANTED");
myservo.write(180); // Move servo to 180 degrees
delay(3000); // Ensure enough delay for the servo to reach the position
myservo.write(0); // Move servo back to 0 degrees
delay(1000); // Ensure enough delay for the servo to reach the position
inputCode = "";
startProcess = false; // Reset process
} else {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("WRONG PASSWORD");
Serial.println("WRONG PASSWORD");
delay(2000);
lcd.clear();
inputCode = "";
startProcess = false; // Reset process
}
}
// Check if the entered code is too long
if (inputCode.length() >= 5) {
inputCode = "";
lcd.clear();
lcd.print("Number too long");
Serial.println("Number too long");
delay(2000);
lcd.clear();
startProcess = false; // Reset process
}
}
}