#include <Keypad.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h> // Include the custom library
#include <Servo.h>
// Keypad configuration
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] = {2, 3, 4, 5}; // connect to the row pinouts of the keypad
byte colPins[COLS] = {6, 7, 8, 9}; // connect to the column pinouts of the keypad
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
// LCD configuration
LiquidCrystal_I2C lcd(0x27, 16, 2); // Set the LCD address to 0x27 for a 16 chars and 2 line display
// Servo configuration
Servo myServo;
String inputCode = "";
const String pass1 = "4444";
const String pass2 = "5555";
const String pass3 = "1729";
void setup() {
lcd.begin(); // Initialize the LCD with 16 columns and 2 rows
lcd.backlight();
myServo.attach(10); // Attach servo to pin 10
lcd.setCursor(0, 0);
lcd.print("Enter Password:");
myServo.write(0);
}
void loop() {
char key = keypad.getKey();
if (key) {
if (key == '#') {
checkPassword();
inputCode = "";
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Enter Password:");
} else if (key == '*') {
inputCode = "";
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Enter Password:");
} else {
inputCode += key;
lcd.setCursor(0, 1);
lcd.print(inputCode);
}
}
}
void checkPassword() {
if (inputCode == pass1 || inputCode == pass2 || inputCode == pass3) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Access Granted");
myServo.write(90); // Rotate servo to 90 degrees
delay(3000); // Keep servo in position for 3 seconds
myServo.write(0); // Rotate servo back to 0 degrees
lcd.clear();
} else {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Access Denied");
delay(2000); // Display Access Denied for 2 seconds
lcd.clear();
}
lcd.setCursor(0, 0);
lcd.print("Enter Password:");
}
Hi,
The servo motor is just pulsing and not moving and the 789C row of the 4x4 keypad does not work
We tried changing the servo and the keypad.
Thank you