#include <LiquidCrystal.h>
#include <Servo.h>
#include <Keypad.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2); // initialize the library with the numbers of the interface pins
Servo servoMotor; // create servo object to control a servo
const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
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}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {5, 4, 3, 2}; //connect to the column pinouts of the keypad
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
int servoPin = 10; // Servo control pin
bool isServoOn = false; // to keep track of servo status
bool isLocked = true; // to check if the system is locked
unsigned long previousMillis = 0; // store the last time the servo was turned on
void setup() {
lcd.begin(16, 2); // set up the LCD's number of columns and rows
servoMotor.attach(servoPin); // attaches the servo on pin 10 to the servo object
lcd.print("Enter password:"); // Display initial message
}
void loop() {
if (isLocked) {
char key = keypad.getKey();
if (key) {
if (key == '123') {
isLocked = false;
lcd.clear();
lcd.print("Unlocked");
delay(1000);
lcd.clear();
} else {
lcd.setCursor(0, 1);
lcd.print("Wrong password");
delay(1000);
lcd.clear();
lcd.print("Enter password:");
}
}
} else {
unsigned long currentMillis = millis();
if (!isServoOn) {
if (currentMillis - previousMillis >= 60000) { // 1 minute in milliseconds
isServoOn = true;
previousMillis = currentMillis;
}
} else {
lcd.clear();
lcd.print("Servo: ON");
servoMotor.write(90); // set the servo position to 90 degrees
isServoOn = false;
}
}
}