#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Keypad.h>
#include <Servo.h>
LiquidCrystal_I2C lcd(0x27, 20, 4);
Servo puerta;
//--------------- PINES -----------------
const int ledVerde = 10;
const int ledRojo = 11;
const int buzzer = 12;
const int pir = A1; // PIR en A1
const int servoPin = 9;
//--------------- 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'}
};
byte pinesFilas[FILAS] = {2, 3, 4, 5};
byte pinesColumnas[COLUMNAS] = {6, 7, 8, A0};
Keypad keypad = Keypad(
makeKeymap(teclas),
pinesFilas,
pinesColumnas,
FILAS,
COLUMNAS
);
//--------------- VARIABLES -------------
String claveCorrecta = "2580";
String claveIngresada = "";
int intentos = 0;
int contadorIntentos = 0;
bool bloqueado = false;
//----------------------------------------
void setup() {
pinMode(ledVerde, OUTPUT);
pinMode(ledRojo, OUTPUT);
pinMode(buzzer, OUTPUT);
pinMode(pir, INPUT);
puerta.attach(servoPin);
puerta.write(0);
Serial.begin(9600);
lcd.init();
lcd.backlight();
pantallaInicial();
}
//----------------------------------------
void loop() {
if (!bloqueado) {
detectarMovimiento();
}
if (bloqueado) {
bloqueoSistema();
return;
}
char tecla = keypad.getKey();
if (tecla) {
if (tecla >= '0' && tecla <= '9') {
if (claveIngresada.length() < 4) {
claveIngresada += tecla;
lcd.setCursor(claveIngresada.length() - 1, 2);
lcd.print("*");
}
}
if (tecla == '#') {
verificarClave();
}
if (tecla == '*') {
claveIngresada = "";
lcd.setCursor(0, 2);
lcd.print(" ");
}
}
}
//----------------------------------------
void pantallaInicial() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("CONTROL DE ACCESO");
lcd.setCursor(0, 1);
lcd.print("Ingrese Clave:");
}
//----------------------------------------
void verificarClave() {
contadorIntentos++;
if (claveIngresada == claveCorrecta) {
Serial.print("Intento ");
Serial.print(contadorIntentos);
Serial.println(": Correcto");
accesoCorrecto();
intentos = 0;
}
else {
Serial.print("Intento ");
Serial.print(contadorIntentos);
Serial.println(": Incorrecto");
accesoIncorrecto();
intentos++;
if (intentos >= 3) {
bloqueado = true;
Serial.println("Sistema Bloqueado");
}
}
claveIngresada = "";
pantallaInicial();
}
//----------------------------------------
void accesoCorrecto() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("ACCESO");
lcd.setCursor(0, 1);
lcd.print("AUTORIZADO");
digitalWrite(ledVerde, HIGH);
tone(buzzer, 1000, 300);
puerta.write(90);
delay(5000);
puerta.write(0);
digitalWrite(ledVerde, LOW);
}
//----------------------------------------
void accesoIncorrecto() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("CLAVE");
lcd.setCursor(0, 1);
lcd.print("INCORRECTA");
for (int i = 0; i < 3; i++) {
digitalWrite(ledRojo, HIGH);
tone(buzzer, 500);
delay(300);
digitalWrite(ledRojo, LOW);
noTone(buzzer);
delay(300);
}
}
//----------------------------------------
void bloqueoSistema() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("SISTEMA");
lcd.setCursor(0, 1);
lcd.print("BLOQUEADO");
unsigned long inicio = millis();
while (millis() - inicio < 20000) {
digitalWrite(ledRojo, HIGH);
delay(500);
digitalWrite(ledRojo, LOW);
delay(500);
if ((millis() - inicio) % 5000 < 100) {
tone(buzzer, 300, 1000);
}
}
bloqueado = false;
intentos = 0;
pantallaInicial();
}
//----------------------------------------
void detectarMovimiento() {
if (digitalRead(pir) == HIGH) {
Serial.println("Movimiento Detectado");
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("MOVIMIENTO");
lcd.setCursor(0, 1);
lcd.print("DETECTADO");
digitalWrite(ledVerde, HIGH);
delay(2000);
digitalWrite(ledVerde, LOW);
pantallaInicial();
while (digitalRead(pir) == HIGH) {
delay(10);
}
}
}