#ifndef IRAM_ATTR
#define IRAM_ATTR
#endif
// Definición de pines de salida
const int outputs[] = {28, 27, 26, 22, 21}; // Pines de salida
// Definición de pines de botones
//pin 2 -> botton 1
//pin 3 -> botton 4
//pin 4 -> botton 7
//pin 5 -> botton 2
//pin 6 -> botton 5
//pin 7 -> botton 8
//pin 8 -> botton 3
//pin 9 -> botton 6
//pin 10 -> botton 9
//pin 11 -> botton 0
const int buttons[] = {12, 2, 5, 8, 3, 6, 9, 4, 7, 10};
// Funciones de los botones
const int CLEAR_BUTTON = 13;
const int ENTER_BUTTON = 11;
const int OPC = 14;
const char *password_master = {"9999"};
// Contraseñas predefinidas
char* passwords[] = {"1234", "5678", "0000", "2345", "4321"}; // Contraseñas válidas
String inputPassword = ""; // Variable para almacenar la entrada del usuario
void change_password(uint8_t num_password, String password) {
password.toCharArray(passwords[num_password-1], password.length()+1);
Serial1.println("\nNueva contraseña " + password);
inputPassword = "";
//passwords[num_password] = password;
}
void IRAM_ATTR clear_data() {
inputPassword = "";
Serial1.println("\nContraseña borrada");
}
void setup() {
// put your setup code here, to run once:
Serial1.begin(115200);
Serial1.println("Inicia el programa!");
// Configurar pines de salida como OUTPUT
for (int i = 0; i < sizeof(outputs) / sizeof(outputs[0]); i++) {
pinMode(outputs[i], OUTPUT);
digitalWrite(outputs[i], LOW); // Asegurarse de que las salidas estén apagadas
}
// Configurar botones como INPUT_PULLUP
for (int i = 0; i < sizeof(buttons) / sizeof(buttons[0]); i++) {
pinMode(buttons[i], INPUT_PULLUP);
}
//pinMode(CLEAR_BUTTON, INPUT_PULLUP);
pinMode(CLEAR_BUTTON, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(CLEAR_BUTTON), clear_data, FALLING);
pinMode(ENTER_BUTTON, INPUT_PULLUP);
pinMode(OPC, INPUT_PULLUP);
}
void loop() {
// Leer botones numéricos
for (int i = 0; i < sizeof(buttons) / sizeof(buttons[0]); i++) {
if (digitalRead(buttons[i]) == LOW) {
delay(200); // Anti-rebote
inputPassword += String(i); // Agregar número a la contraseña
Serial1.print("*");
}
}
// Verificar si se presionó el botón CLEAR
//if (digitalRead(CLEAR_BUTTON) == LOW) {
// delay(200);
// inputPassword = ""; // Borrar la contraseña ingresada
// Serial1.println("\nContraseña borrada");
//}
// Verificar si se presionó el botón ENTER
if (digitalRead(ENTER_BUTTON) == LOW) {
delay(200);
view_pass();
}
// Verificar si se presionó el botón RESET
if (digitalRead(OPC) == LOW) {
delay(200);
uint8_t num_password = 255;
String new_password = "";
if (master_password()) {
inputPassword = "";
Serial1.println("\nEscriba el número de piso");
while (num_password == 255) {
for (int i = 0; i < sizeof(buttons) / sizeof(buttons[0]); i++) {
if (digitalRead(buttons[i]) == LOW) {
delay(200); // Anti-rebote
num_password = i; // Agregar número a la contraseña
}
}
}
if (num_password <= 5 || num_password >= 1)
{
//Escriba la contraseña
Serial1.println("Escriba la contraseña");
while (inputPassword.length() < 4) {
for (int i = 0; i < sizeof(buttons) / sizeof(buttons[0]); i++) {
if (digitalRead(buttons[i]) == LOW) {
delay(200); // Anti-rebote
inputPassword += String(i); // Agregar número a la contraseña
Serial1.print("*");
}
}
}
new_password = inputPassword;
inputPassword = "";
Serial1.println("\nConfirme la contraseña");
//Confirma la contraseña
while (inputPassword.length() < 4) {
for (int i = 0; i < sizeof(buttons) / sizeof(buttons[0]); i++) {
if (digitalRead(buttons[i]) == LOW) {
delay(200); // Anti-rebote
inputPassword += String(i); // Agregar número a la contraseña
Serial1.print("*");
}
}
}
if (new_password == inputPassword) {
change_password(num_password, inputPassword);
inputPassword = "";
}
else {
inputPassword = "";
Serial1.println("\nContraseñas distintas");
}
}
else {
inputPassword = "";
Serial1.println("\nNúmero de piso incorrecto");
}
}
else {
inputPassword = "";
Serial1.println("\nContraseña maestra incorrecta");
}
}
}
void view_pass() {
// Separar las partes
String functionStr = inputPassword.substring(0, 1); // Los primeros 2 caracteres
String floorStr = inputPassword.substring(1, 2); // El tercer carácter
String passwordStr = inputPassword.substring(2); // Desde el cuarto carácter hasta el final
// Convertir a uint8_t
uint8_t function = functionStr.toInt();
uint8_t floor_num = floorStr.toInt();
if (passwordStr == passwords[floor_num-1]) {
Serial1.print("\nContraseña Correcta --- Realizando función ");
Serial1.println(function);
active_floor(floor_num, function);
}
else {
Serial1.println("\nContraseña incorrecta");
}
inputPassword = "";
}
bool master_password() {
if (inputPassword == password_master)
return true;
else
return false;
}
void active_floor(uint8_t floor_num, uint8_t function){
switch (function) {
case 0:
digitalWrite(outputs[floor_num-1], HIGH);
delay(5000);
digitalWrite(outputs[floor_num-1], LOW);
break;
case 1:
digitalWrite(outputs[floor_num-1], HIGH);
break;
case 2:
digitalWrite(outputs[floor_num-1], LOW);
break;
}
}