#include <Keypad.h>
#include <Servo.h>
#include <LiquidCrystal_I2C.h>
Servo servoMotor;
LiquidCrystal_I2C lcd(0x27,16,2);
// configuración del keypad
const byte FILAS = 4;
const byte COLUMNAS = 4;
char teclas[FILAS][COLUMNAS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
// Pines según tu diagram.json
byte pinesFilas[FILAS] = {2,3,4,5};
byte pinesColumnas[COLUMNAS] = {6,7,8,10};
Keypad keypad = Keypad(makeKeymap(teclas), pinesFilas, pinesColumnas, FILAS, COLUMNAS);
// contraseña
String password = "1234";
String input = "";
void setup() {
servoMotor.attach(9);
lcd.init();
lcd.backlight();
servoMotor.write(0);
lcd.setCursor(0,0);
lcd.print("Ingrese clave");
}
void loop() {
char tecla = keypad.getKey();
if (tecla) {
if (tecla == '#') {
if (input == password) {
lcd.clear();
lcd.print("Puerta abierta");
servoMotor.write(90);
delay(3000);
servoMotor.write(0);
} else {
lcd.clear();
lcd.print("Clave incorrecta");
delay(2000);
}
input = "";
lcd.clear();
lcd.print("Ingrese clave");
}
else if (tecla == '*') {
input = "";
lcd.setCursor(0,1);
lcd.print(" ");
}
else {
input += tecla;
lcd.setCursor(0,1);
lcd.print(input);
}
}
}