#include <Keypad.h>
#include <LiquidCrystal_I2C.h>
#include <Servo.h>
#include <avr/wdt.h>
#define I2C_ADDR 0x27
#define LCD_COLUMNS 20
#define LCD_LINES 4
#define LedRojo 13
#define LedVerde 12
LiquidCrystal_I2C lcd(I2C_ADDR, LCD_COLUMNS, LCD_LINES);
Servo myservo;
const byte ROWS = 4;
const byte COLS = 4;
byte INDICE = 0;
char Contra1[7] = "198107";
char Contra[7];
char Contra2[7] = "110901";
char hexaKeys[ROWS][COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
byte rowPins[ROWS] = {10, 9, 8, 7};
byte colPins[COLS] = {6, 5, 4, 3};
Keypad customKeypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
void setup() {
Serial.begin(9600);
lcd.init();
lcd.backlight();
lcd.setCursor(5, 0);
lcd.print("BIENVENIDO");
lcd.setCursor(3, 1);
lcd.print("AL RESIDENCIAL");
lcd.setCursor(6, 2);
lcd.print("OLYMPUS!");
lcd.setCursor(8, 3);
lcd.print(":D");
delay(5000);
lcd.clear();
mostrarMensajeInicial();
myservo.attach(11);
pinMode(A3, INPUT_PULLUP);
pinMode(LedRojo, OUTPUT);
pinMode(LedVerde, OUTPUT);
digitalWrite(LedRojo, LOW);
digitalWrite(LedVerde, HIGH);
}
void(* resetFunc) (void) = 0;
void reiniciarCodigo() {
resetFunc();
}
void AbrirPuerta() {
myservo.write(0);
digitalWrite(LedVerde, LOW);
digitalWrite(LedRojo, HIGH);
delay(5000);
digitalWrite(LedRojo, LOW);
digitalWrite(LedVerde, HIGH);
myservo.write(90);
lcd.clear();
mostrarMensajeInicial();
}
void mostrarMensajeInicial() {
lcd.setCursor(6, 0);
lcd.print("INGRESA");
lcd.setCursor(4, 1);
lcd.print("EL PASSWORD");
lcd.setCursor(5, 2);
lcd.print("POR FAVOR!");
}
void loop() {
char customKey;
while ((customKey = customKeypad.getKey())) {
lcd.setCursor(INDICE + 7, 3);
lcd.print("*");
Serial.println(customKey);
Contra[INDICE] = customKey;
INDICE++;
if (INDICE == 6) {
if (!strcmp(Contra, Contra1) || !strcmp(Contra, Contra2)) {
lcd.clear();
lcd.setCursor(6, 1);
lcd.print("PASSWORD");
lcd.setCursor(6, 2);
lcd.print("CORRECTO");
AbrirPuerta();
INDICE = 0;
}
// Aquí hay un 'else' sin un 'if' correspondiente
// Además, hay un 'break' fuera de un bucle o switch.
else {
Serial.println("INCORRECTO");
INDICE = 0;
lcd.clear();
delay(1);
lcd.setCursor(6, 1);
lcd.print("PASSWORD");
lcd.setCursor(5, 2);
lcd.print("INCORRECTO");
delay(1000);
INDICE = 0;
lcd.clear();
mostrarMensajeInicial();
// Aquí debería haber una instrucción que permita salir del bucle while.
// ¿Quizás necesitas una 'return' en lugar de 'break'?
// O un 'break' dentro de un bucle exterior.
}
}
}
if (!digitalRead(A3)) {
lcd.clear();
lcd.setCursor(7, 1);
lcd.print("PUERTA");
lcd.setCursor(6, 2);
lcd.print("ABIERTA");
AbrirPuerta();
INDICE = 0;
delay(10);
}
}
//en este codigo no se puede hacer la forma de administracion
/*for (int intentos = 0; intentos < 3; intentos++) {
/*#include <Keypad.h>
#include <LiquidCrystal_I2C.h>
#define I2C_ADDR 0x27
#define LCD_COLUMNS 20
#define LCD_LINES 4
LiquidCrystal_I2C lcd(I2C_ADDR, LCD_COLUMNS, LCD_LINES);
const byte ROWS = 4;
const byte COLS = 4;
int INDICE =0;
char Contra1 [7]= "198107";
char Contra[7];
char hexaKeys[ROWS][COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
byte rowPins[ROWS] = {10, 9, 8, 7};
byte colPins[COLS] = {6, 5, 4, 3};
Keypad customKeypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
void setup(){
Serial.begin(9600);
lcd.init();
lcd.backlight();
// Print something
lcd.setCursor(3, 0);
lcd.print("Hello, world!");
lcd.setCursor(2, 1);
lcd.print("Wokwi Online IoT");
lcd.setCursor(5, 2);
lcd.print("Simulator");
lcd.setCursor(7, 3);
lcd.print("Enjoy!");
delay(5000);
lcd.clear();
lcd.setCursor(6, 0);
lcd.print("ingrese");
lcd.setCursor(4, 1);
lcd.print("el password");
lcd.setCursor(5, 2);
lcd.print("por favor!");
}
void loop(){
char customKey = customKeypad.getKey();
if (customKey){
Serial.println(customKey);
Contra[INDICE]= customKey;
INDICE++;
}
if (INDICE == 6) {if(!strcmp(Contra, Contra1))
Serial.println(" correcta");
lcd.clear();
delay(1);
lcd.setCursor(6, 1);
lcd.print("password");
lcd.setCursor(6, 2);
lcd.print("correcto");
delay(10000);
INDICE=0;
lcd.setCursor(3, 0);
lcd.clear();
}
}/*
/*void loop() {
char TECLA= teclado.getKeys();
Serial.print(TECLA);
//if (TECLA){
//CONTRA[INDICE] = TECLA;
// INDICE++;
// delay(500);
//}
/*if (INDICE == 6){
if(!strcmp(CONTRA, CONTRASENA) || !strcmp(CONTRA, CONTRALESA))
Serial.println(" correcta");
else
Serial.println(" incorrecta");
INDICE = 0;
}
} */