Advertisement
1907

ESP32 with keyboad 4x4

Oct 6th, 2022
811
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.70 KB | None | 0 0
  1.  
  2. // importa libreria Keypad
  3. #include <Keypad.h>
  4. #include <LiquidCrystal_I2C.h>
  5.  
  6. //SENSOR DE DISTANCIA
  7. LiquidCrystal_I2C lcd2(0x3F,16,2);  // set the LCD address to 0x27 for a 16 chars and 2 line display
  8. int echo = 13;
  9. int trigger = 12;
  10. int pulso;
  11. long tiempo;
  12. float distancia;
  13.  
  14.                                 // Crear el objeto lcd  dirección  0x3F y 16
  15.                                 // columnas x 2 filas
  16.                                 LiquidCrystal_I2C lcd(0x27, 16, 2);  //
  17.  
  18. // define numero de filas
  19. const uint8_t ROWS = 4;
  20. // define numero de columnas
  21. const uint8_t COLS = 4;
  22. // define la distribucion de teclas
  23. char keys[ROWS][COLS] = {{'1', '2', '3', 'A'},
  24.                          {'4', '5', '6', 'B'},
  25.                          {'7', '8', '9', 'C'},
  26.                          {'*', '0', '#', 'D'}};
  27. // pines correspondientes a las filas
  28. uint8_t colPins[COLS] = {2, 15, 25, 26};
  29. // pines correspondientes a las columnas
  30. uint8_t rowPins[ROWS] = {19, 18, 5, 4};
  31. // crea objeto con los prametros creados previamente
  32. Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
  33. // Declaramos la variable que almacena el password
  34. // char pass[7]="ABCD45";
  35. String pass = "ABC123";
  36. String passwordIngresado;
  37. // Varible que alamacena la clave que se va ingresando
  38. char ingresapass[7];
  39. // Variable que define el numero de letra que se va ingresando
  40. int indice = 0;
  41. int pinLedVerde = 12;
  42. int pinLedRojo = 13;
  43. bool accesoHabilitado = false;
  44. int intento = 1;
  45. int contadorCaracteres = 0;
  46. int indicePantalla = 7;
  47. boolean echoController = true;
  48. boolean bienvenidoText = false;
  49.  
  50. void setup() {
  51.   //SENSOR DE MOVIMIENTO
  52.   pinMode(echo,INPUT);
  53.   pinMode(trigger,OUTPUT);
  54.   lcd2.init();                      // initialize the lcd
  55.   lcd2.init();
  56.  
  57.   // PANTALLA LCD
  58.   // Inicializar el LCD
  59.   lcd.init();
  60.  
  61.   // Encender la luz de fondo.
  62.   lcd.backlight();
  63.  
  64.   // KEYPAD
  65.   pinMode(pinLedVerde, OUTPUT);
  66.   pinMode(pinLedRojo, OUTPUT);
  67.   Serial.begin(115200);
  68.  
  69.   // configuración del LCD
  70.   lcd.begin(16, 2);
  71.   // Mensaje inicial LCD
  72.   lcd.print("Digite su clave:");
  73.   lcd.setCursor(0, 1);
  74.   lcd.print("* para ingresar");
  75.   delay(500);
  76.   // Desplazamiento del texto
  77.   for (int pos = 0; pos < 13; pos++) {
  78.     // izquierda
  79.     lcd.scrollDisplayLeft();
  80.     delay(100);
  81.   }
  82.   // desplazamiento a la derecha
  83.   for (int pos = 0; pos < 26; pos++) {
  84.     lcd.scrollDisplayRight();
  85.     delay(100);
  86.   }
  87.   delay(500);
  88.   lcd.clear();
  89. }
  90.  
  91. void loop() {
  92.   if (echoController){
  93.     //SENSOR DE MOVIMIENTO
  94.     digitalWrite(trigger,HIGH);     //Para estabilizar el sensor
  95.     delayMicroseconds(20);
  96.     digitalWrite(trigger,LOW);     // envío del pulso ultrasónico
  97.     tiempo = pulseIn(echo,HIGH);      //Función para medir la longitud del pulso entrante
  98.       Serial.println(tiempo);
  99.     //Obtener distancia en
  100.     distancia= (0.0175*tiempo);  //Fórmula para calcular la distancia en valor entero
  101.     Serial.println(distancia);
  102.     //  lcd2.print(distancia);
  103.     //  lcd2.print("cm");
  104.     lcd2.clear();
  105.     if (distancia < 20) {
  106.       echoController = false;
  107.       bienvenidoText = true;
  108.     }
  109.   }
  110.   else {
  111.     if(bienvenidoText){
  112.       lcd2.print("Bienvenido");
  113.       bienvenidoText = false;
  114.       delay(2000);
  115.       lcd2.clear();
  116.     }
  117.     int conteo = 60;
  118.     // obtiene tecla presionada y asigna a variable
  119.     lcd.setCursor(0, 0);
  120.     // print the number of second since reset:
  121.     lcd.print("clave: ");
  122.     char teclaIngresada = keypad.getKey();
  123.     Serial.println(teclaIngresada);
  124.     lcd.display();
  125.  
  126.     // CUANDO EL USUARIO INGRESA LA CONTRASEÑA
  127.     // Se obtuvo correctamente un caracter
  128.     if (teclaIngresada) {
  129.       contadorCaracteres++;
  130.       lcd.setCursor(indicePantalla, 0);
  131.  
  132.       if (contadorCaracteres <= 6) {
  133.         lcd.print(teclaIngresada);
  134.         passwordIngresado += teclaIngresada;
  135.         if (passwordIngresado == pass) {
  136.           accesoHabilitado = true;
  137.           Serial.println("CONTRASENIA CORRECTA MI TERNA");
  138.         } else {
  139.           Serial.print("CONTRASEÑA DEFINIDA: ");
  140.           Serial.println(pass);
  141.           Serial.print("CONTRASEÑA INGRESADA: ");
  142.           Serial.print(passwordIngresado);
  143.         }
  144.       }
  145.       indicePantalla++;
  146.       // Se presionó el botón de aceptar o 'ENTER'
  147.       if (teclaIngresada == '*') {
  148.         contadorCaracteres = 0;
  149.         if (accesoHabilitado == true and !passwordIngresado.isEmpty()) {
  150.           lcd.clear();
  151.           lcd.setCursor(0, 0);
  152.           lcd.print("Acceso permitido");
  153.           lcd.setCursor(0, 1);
  154.           lcd.print("Bienvenido");
  155.           digitalWrite(pinLedVerde, HIGH);
  156.           delay(2000);
  157.           digitalWrite(pinLedVerde, LOW);
  158.           delay(500);
  159.           delay(2000);
  160.           lcd.clear();
  161.           indicePantalla = 7;
  162.           accesoHabilitado = false;
  163.           passwordIngresado = "";
  164.           contadorCaracteres = 0;
  165.  
  166.           //SENSOR DE MOVIMIENTO
  167.           digitalWrite(trigger,HIGH);     //Para estabilizar el sensor
  168.           delayMicroseconds(20);
  169.           digitalWrite(trigger,LOW);     // envío del pulso ultrasónico
  170.           tiempo = pulseIn(echo,HIGH);      //Función para medir la longitud del pulso entrante
  171.             Serial.println(tiempo);
  172.           //Obtener distancia en
  173.           distancia= (0.0175*tiempo);  //Fórmula para calcular la distancia en valor entero
  174.           Serial.println(distancia);
  175.           //  lcd2.print(distancia);
  176.           //  lcd2.print("cm");
  177.           lcd2.clear();
  178.           echoController = true;
  179.           lcd.clear();
  180.           lcd.setCursor(0, 0);
  181.           lcd2.print("Hasta Luego");
  182.           lcd.print("Echo goodbye");
  183.           lcd.clear();
  184.  
  185.         } else if (passwordIngresado.isEmpty()) {
  186.           lcd.clear();
  187.           lcd.setCursor(0, 1);
  188.           lcd.print("Contrasena nula");
  189.           digitalWrite(pinLedRojo, HIGH);
  190.           delay(2000);
  191.           digitalWrite(pinLedRojo, LOW);
  192.           delay(500);
  193.           lcd.setCursor(0, 0);
  194.           indicePantalla = 7;
  195.           accesoHabilitado = false;
  196.           passwordIngresado = "";
  197.           contadorCaracteres = 0;
  198.         } else {
  199.           // Mostramos el mensaje de acceso denegado
  200.           lcd.clear();
  201.           lcd.setCursor(0, 1);
  202.           lcd.print("Acceso denegado");
  203.           digitalWrite(pinLedRojo, HIGH);
  204.           delay(2000);
  205.           digitalWrite(pinLedRojo, LOW);
  206.           delay(500);
  207.           intento++;
  208.           delay(1000);
  209.           lcd.setCursor(0, 0);
  210.           lcd.print("Intentos: ");
  211.           lcd.setCursor(9, 0);
  212.           lcd.print(intento);
  213.           delay(1000);
  214.           lcd.clear();
  215.           indicePantalla = 7;
  216.           accesoHabilitado = false;
  217.           passwordIngresado = "";
  218.           contadorCaracteres = 0;
  219.           lcd.clear();
  220.         }
  221.         if (intento > 3) {
  222.           while (conteo != 0) {
  223.             lcd.clear();
  224.             lcd.setCursor(0, 0);
  225.             lcd.print("Sistema");
  226.             lcd.setCursor(0, 1);
  227.             lcd.print("Bloqueado");
  228.             lcd.setCursor(12, 1);
  229.             lcd.print(conteo);
  230.             delay(50);
  231.             conteo--;
  232.           }
  233.           intento = 0;
  234.           lcd.clear();
  235.         }
  236.       }
  237.       if (teclaIngresada == '#') {
  238.         lcd.clear();
  239.         lcd.setCursor(0, 0);
  240.         lcd.print("Contrasena");
  241.         lcd.setCursor(0, 1);
  242.         lcd.print("reiniciada");
  243.         digitalWrite(pinLedRojo, HIGH);
  244.         delay(2000);
  245.         digitalWrite(pinLedRojo, LOW);
  246.         delay(500);
  247.         lcd.clear();
  248.         passwordIngresado = "";
  249.         contadorCaracteres = 0;
  250.         indicePantalla = 7;
  251.         accesoHabilitado = false;
  252.         lcd.clear();
  253.       }
  254.     }
  255.   }
  256.  
  257. }
  258.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement