Advertisement
1907

ESP32 con teclado - IOT

Oct 4th, 2022 (edited)
774
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 22.26 KB | None | 0 0
  1. // importa libreria Keypad
  2. #include <Keypad.h>
  3. #include <LiquidCrystal_I2C.h>
  4.  
  5. //Crear el objeto lcd  dirección  0x3F y 16 columnas x 2 filas
  6. LiquidCrystal_I2C lcd(0x27,16,2);  //
  7.  
  8. // define numero de filas
  9. const uint8_t ROWS = 4;
  10. // define numero de columnas
  11. const uint8_t COLS = 4;
  12. // define la distribucion de teclas
  13. char keys[ROWS][COLS] = {
  14.   { '1', '2', '3', 'A' },
  15.   { '4', '5', '6', 'B' },
  16.   { '7', '8', '9', 'C' },
  17.   { '*', '0', '#', 'D' }
  18. };
  19. // pines correspondientes a las filas
  20. uint8_t colPins[COLS] = { 2, 15, 25, 26};
  21. // pines correspondientes a las columnas
  22. uint8_t rowPins[ROWS] = { 19, 18, 5, 4  };
  23. // crea objeto con los prametros creados previamente
  24. Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
  25. //Declaramos la variable que almacena el password
  26. //char pass[7]="ABCD45";
  27. char pass = "ABCD45";
  28. char passwordIngresado = '';
  29. //Varible que alamacena la clave que se va ingresando
  30. char ingresapass[7];
  31. //Variable que define el numero de letra que se va ingresando
  32. int indice=0;
  33. int pinLedVerde=12;
  34. int pinLedRojo=13;
  35. int buena = 0;
  36. int intento = 1;
  37. int contadorTeclas=0;
  38. int indicePantalla=7;
  39. //=================================================================================
  40. void setup() {
  41.   //PANTALLA LCD
  42.   // Inicializar el LCD
  43.   lcd.init();
  44.  
  45.   //Encender la luz de fondo.
  46.   lcd.backlight();
  47.  
  48.  
  49.   //KEYPAD
  50.   pinMode(pinLedVerde, OUTPUT);
  51.   pinMode(pinLedRojo, OUTPUT);
  52.   Serial.begin(115200);
  53.  
  54.   //configuración del LCD
  55.   lcd.begin(16, 2);
  56.   //Mensaje inicial LCD
  57.   lcd.print("Digite su clave!:");
  58.   lcd.setCursor(0,1);
  59.   lcd.print("* es ENTER");
  60.   delay(500);
  61.   //Desplazamiento del texto
  62.   for (int pos=0;pos<13;pos++){
  63.   //izquierda
  64.     lcd.scrollDisplayLeft();
  65.     delay(500);
  66.   }
  67.   //desplazamiento a la derecha
  68.   for (int pos=0;pos < 26;pos++){
  69.     lcd.scrollDisplayRight();
  70.     delay(500);
  71.   }
  72.   //Desplazamiento del texto
  73.   for (int pos= 0;pos <13;pos++){
  74.     //izquierda
  75.     lcd.scrollDisplayLeft();
  76.     delay(500);
  77.   }
  78.   delay(1000);
  79.   lcd.clear();
  80.   }
  81.  
  82. void loop() {
  83.   int conteo = 60;
  84.   // obtiene tecla presionada y asigna a variable
  85.   lcd.setCursor(0,0);
  86.   //print the number of second since reset:
  87.   lcd.print("clave: ");
  88.   char key=keypad.getKey();
  89.   lcd.display();
  90.  
  91.   //CUANDO EL USUARIO INGRESA LA CONTRASEÑA
  92.   //
  93.   if(key){
  94.     contadorTeclas++;
  95.     lcd.setCursor(indicePantalla,0);
  96.    
  97.     if(contadorTeclas<=6){
  98.         lcd.print("*");
  99.         passwordIngresado += key;
  100.         if (passwordIngresado == pass){
  101.             buena = 6;
  102.         }
  103.     }
  104.     indicePantalla++;
  105.   }
  106.  
  107.   if (key=='*'){
  108.     contadorTeclas=0;
  109.     if(buena==6 and passwordIngresado != ''){
  110.       lcd.clear();
  111.       lcd.setCursor(0,0);
  112.       lcd.print("Acceso permitido");
  113.       lcd.setCursor(0,1);
  114.       lcd.print("BIENVENIDO");
  115.       digitalWrite(pinLedVerde, HIGH);
  116.       delay(2000);
  117.       digitalWrite(pinLedVerde,LOW);
  118.       delay(500);
  119.      
  120.       delay(2000);
  121.      
  122.       lcd.clear();
  123.       indicePantalla=7;
  124.       buena=0;
  125.      
  126.     }
  127.     else if (passwordIngresado == ''){
  128.       lcd.clear();
  129.       lcd.setCursor(0,1);
  130.       lcd.print("Contr. nula");
  131.       digitalWrite(pinLedRojo, HIGH);
  132.       delay(2000);
  133.       digitalWrite(pinLedRojo,LOW);
  134.       delay(500);
  135.       lcd.setCursor(0,0);
  136.       indicePantalla=7;
  137.       buena=0;
  138.     }
  139.     else{
  140.       // Mostramos el mensaje de acceso denegado
  141.       lcd.clear();
  142.       lcd.setCursor(0,1);
  143.       lcd.print("Acceso Denegado");
  144.       digitalWrite(pinLedRojo, HIGH);
  145.       delay(2000);
  146.       digitalWrite(pinLedRojo,LOW);
  147.       delay(500);
  148.       intento++;
  149.       delay(1000);
  150.       lcd.setCursor(0,0);
  151.       lcd.print("Intentos#");
  152.       lcd.setCursor(9,0);
  153.       lcd.print(intento);
  154.       delay(1000);
  155.       lcd.clear();
  156.       indicePantalla=7;
  157.       buena=0;
  158.     }
  159.   if(intento>3){
  160.     while(conteo !=0){
  161.       lcd.clear();
  162.       lcd.setCursor(0,0);
  163.       lcd.print("sistema");
  164.       lcd.setCursor(0,1);
  165.       lcd.print("Bloqueado");
  166.       lcd.setCursor(12,1);
  167.       lcd.print(conteo);
  168.       delay(1000);
  169.         conteo--;
  170.     }
  171.     intento=0;
  172.     lcd.clear();
  173.   }
  174.   }
  175. }
  176.  
  177.  
  178.  
  179. ---------------------------------- VERSION 2 ----------------------------------
  180. -------------------------------------------------------------------------------
  181.  
  182.  
  183. // importa libreria Keypad
  184. #include <Keypad.h>
  185. #include <LiquidCrystal_I2C.h>
  186.  
  187. //Crear el objeto lcd  dirección  0x3F y 16 columnas x 2 filas
  188. LiquidCrystal_I2C lcd(0x27,16,2);  //
  189.  
  190. // define numero de filas
  191. const uint8_t ROWS = 4;
  192. // define numero de columnas
  193. const uint8_t COLS = 4;
  194. // define la distribucion de teclas
  195. char keys[ROWS][COLS] = {
  196.   { '1', '2', '3', 'A' },
  197.   { '4', '5', '6', 'B' },
  198.   { '7', '8', '9', 'C' },
  199.   { '*', '0', '#', 'D' }
  200. };
  201. // pines correspondientes a las filas
  202. uint8_t colPins[COLS] = { 2, 15, 25, 26};
  203. // pines correspondientes a las columnas
  204. uint8_t rowPins[ROWS] = { 19, 18, 5, 4  };
  205. // crea objeto con los prametros creados previamente
  206. Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
  207. //Declaramos la variable que almacena el password
  208. //char pass[7]="ABCD45";
  209. char pass = "ABCD45";
  210. char passwordIngresado = '';
  211. //Varible que alamacena la clave que se va ingresando
  212. char ingresapass[7];
  213. //Variable que define el numero de letra que se va ingresando
  214. int indice=0;
  215. int pinLedVerde=12;
  216. int pinLedRojo=13;
  217. bool accesoHabilitado = false;
  218. int intento = 1;
  219. int contadorCaracteres=0;
  220. int indicePantalla=7;
  221. //=================================================================================
  222. void setup() {
  223.   //PANTALLA LCD
  224.   // Inicializar el LCD
  225.   lcd.init();
  226.  
  227.   //Encender la luz de fondo.
  228.   lcd.backlight();
  229.  
  230.  
  231.   //KEYPAD
  232.   pinMode(pinLedVerde, OUTPUT);
  233.   pinMode(pinLedRojo, OUTPUT);
  234.   Serial.begin(115200);
  235.  
  236.   //configuración del LCD
  237.   lcd.begin(16, 2);
  238.   //Mensaje inicial LCD
  239.   lcd.print("Digite su clave!:");
  240.   lcd.setCursor(0,1);
  241.   lcd.print("* es ENTER");
  242.   delay(500);
  243.   //Desplazamiento del texto
  244.   for (int pos=0;pos<13;pos++){
  245.   //izquierda
  246.     lcd.scrollDisplayLeft();
  247.     delay(500);
  248.   }
  249.   //desplazamiento a la derecha
  250.   for (int pos=0;pos < 26;pos++){
  251.     lcd.scrollDisplayRight();
  252.     delay(500);
  253.   }
  254.   //Desplazamiento del texto
  255.   for (int pos= 0;pos <13;pos++){
  256.     //izquierda
  257.     lcd.scrollDisplayLeft();
  258.     delay(500);
  259.   }
  260.   delay(1000);
  261.   lcd.clear();
  262.   }
  263.  
  264. void loop() {
  265.   int conteo = 60;
  266.   // obtiene tecla presionada y asigna a variable
  267.   lcd.setCursor(0,0);
  268.   //print the number of second since reset:
  269.   lcd.print("clave: ");
  270.   char teclaIngresada=keypad.getKey();
  271.   lcd.display();
  272.  
  273.   //CUANDO EL USUARIO INGRESA LA CONTRASEÑA
  274.   // Se obtuvo correctamente un caracter
  275.   if(teclaIngresada){
  276.     contadorCaracteres++;
  277.     lcd.setCursor(indicePantalla,0);
  278.    
  279.     if(contadorCaracteres<=6){
  280.         lcd.print(teclaIngresada);
  281.         passwordIngresado += teclaIngresada;
  282.         if (passwordIngresado == pass){
  283.             accesoHabilitado = true;
  284.         }
  285.     }
  286.     indicePantalla++;
  287.   }
  288.  
  289.   // Se presionó el botón de aceptar o 'ENTER'
  290.   if (teclaIngresada=='*'){
  291.     contadorCaracteres=0;
  292.     if(accesoHabilitado == true and passwordIngresado != ''){
  293.       lcd.clear();
  294.       lcd.setCursor(0,0);
  295.       lcd.print("Acceso permitido");
  296.       lcd.setCursor(0,1);
  297.       lcd.print("BIENVENIDO");
  298.       digitalWrite(pinLedVerde, HIGH);
  299.       delay(2000);
  300.       digitalWrite(pinLedVerde,LOW);
  301.       delay(500);
  302.      
  303.       delay(2000);
  304.      
  305.       lcd.clear();
  306.       indicePantalla=7;
  307.       accesoHabilitado = false;
  308.      
  309.     }
  310.     else if (passwordIngresado == ''){
  311.       lcd.clear();
  312.       lcd.setCursor(0,1);
  313.       lcd.print("Contrasena nula");
  314.       digitalWrite(pinLedRojo, HIGH);
  315.       delay(2000);
  316.       digitalWrite(pinLedRojo,LOW);
  317.       delay(500);
  318.       lcd.setCursor(0,0);
  319.       indicePantalla=7;
  320.       accesoHabilitado = false;
  321.     }
  322.     else{
  323.       // Mostramos el mensaje de acceso denegado
  324.       lcd.clear();
  325.       lcd.setCursor(0,1);
  326.       lcd.print("Acceso Denegado");
  327.       digitalWrite(pinLedRojo, HIGH);
  328.       delay(2000);
  329.       digitalWrite(pinLedRojo,LOW);
  330.       delay(500);
  331.       intento++;
  332.       delay(1000);
  333.       lcd.setCursor(0,0);
  334.       lcd.print("Intentos#");
  335.       lcd.setCursor(9,0);
  336.       lcd.print(intento);
  337.       delay(1000);
  338.       lcd.clear();
  339.       indicePantalla=7;
  340.       accesoHabilitado = false;
  341.     }
  342.     if(intento>3){
  343.         while(conteo !=0){
  344.         lcd.clear();
  345.         lcd.setCursor(0,0);
  346.         lcd.print("sistema");
  347.         lcd.setCursor(0,1);
  348.         lcd.print("Bloqueado");
  349.         lcd.setCursor(12,1);
  350.         lcd.print(conteo);
  351.         delay(1000);
  352.             conteo--;
  353.         }
  354.         intento=0;
  355.         lcd.clear();
  356.         }
  357.     }
  358.     if (teclaIngresada = '#'){
  359.         lcd.clear();
  360.         lcd.setCursor(0,1);
  361.         lcd.print("Contrasena");
  362.         lcd.setCursor(1,1);
  363.         lcd.print("Reiniciada");
  364.         digitalWrite(pinLedRojo, HIGH);
  365.         delay(2000);
  366.         digitalWrite(pinLedRojo,LOW);
  367.         delay(500);
  368.         lcd.clear();
  369.         indicePantalla=7;
  370.         accesoHabilitado = false ;
  371.     }
  372.  
  373. }
  374.  
  375.  
  376.  
  377. ---------------------------------- VERSION 3----------------------------------
  378. -------------------------------------------------------------------------------
  379.  
  380.  
  381.  
  382. // importa libreria Keypad
  383. #include <Keypad.h>
  384.  
  385. #include <LiquidCrystal_I2C.h>
  386.  
  387. //Crear el objeto lcd  dirección  0x3F y 16 columnas x 2 filas
  388. LiquidCrystal_I2C lcd(0x27, 16, 2); //
  389.  
  390. // define numero de filas
  391. const uint8_t ROWS = 4;
  392. // define numero de columnas
  393. const uint8_t COLS = 4;
  394. // define la distribucion de teclas
  395. char keys[ROWS][COLS] = {
  396.   {
  397.     '1',
  398.     '2',
  399.     '3',
  400.     'A'
  401.   },
  402.   {
  403.     '4',
  404.     '5',
  405.     '6',
  406.     'B'
  407.   },
  408.   {
  409.     '7',
  410.     '8',
  411.     '9',
  412.     'C'
  413.   },
  414.   {
  415.     '*',
  416.     '0',
  417.     '#',
  418.     'D'
  419.   }
  420. };
  421. // pines correspondientes a las filas
  422. uint8_t colPins[COLS] = {
  423.   2,
  424.   15,
  425.   25,
  426.   26
  427. };
  428. // pines correspondientes a las columnas
  429. uint8_t rowPins[ROWS] = {
  430.   19,
  431.   18,
  432.   5,
  433.   4
  434. };
  435. // crea objeto con los prametros creados previamente
  436. Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
  437. //Declaramos la variable que almacena el password
  438. //char pass[7]="ABCD45";
  439. String pass = "ABC123";
  440. String passwordIngresado;
  441. //Varible que alamacena la clave que se va ingresando
  442. char ingresapass[7];
  443. //Variable que define el numero de letra que se va ingresando
  444. int indice = 0;
  445. int pinLedVerde = 12;
  446. int pinLedRojo = 13;
  447. bool accesoHabilitado = false;
  448. int intento = 1;
  449. int contadorCaracteres = 0;
  450. int indicePantalla = 7;
  451.  
  452.  
  453. void setup() {
  454.   //PANTALLA LCD
  455.   // Inicializar el LCD
  456.   lcd.init();
  457.  
  458.   //Encender la luz de fondo.
  459.   lcd.backlight();
  460.  
  461.   //KEYPAD
  462.   pinMode(pinLedVerde, OUTPUT);
  463.   pinMode(pinLedRojo, OUTPUT);
  464.   Serial.begin(115200);
  465.  
  466.   //configuración del LCD
  467.   lcd.begin(16, 2);
  468.   //Mensaje inicial LCD
  469.   lcd.print("Digite su clave:");
  470.   lcd.setCursor(0, 1);
  471.   lcd.print("* para ingresar");
  472.   delay(500);
  473.   //Desplazamiento del texto
  474.   for (int pos = 0; pos < 13; pos++) {
  475.     //izquierda
  476.     lcd.scrollDisplayLeft();
  477.     delay(100);
  478.   }
  479.   //desplazamiento a la derecha
  480.   for (int pos = 0; pos < 26; pos++) {
  481.     lcd.scrollDisplayRight();
  482.     delay(100);
  483.   }
  484.   delay(500);
  485.   lcd.clear();
  486. }
  487.  
  488.  
  489. void loop() {
  490.   int conteo = 60;
  491.   // obtiene tecla presionada y asigna a variable
  492.   lcd.setCursor(0, 0);
  493.   //print the number of second since reset:
  494.   lcd.print("clave: ");
  495.   char teclaIngresada = keypad.getKey();
  496.   Serial.println(teclaIngresada);
  497.   lcd.display();
  498.  
  499.   //CUANDO EL USUARIO INGRESA LA CONTRASEÑA
  500.   // Se obtuvo correctamente un caracter
  501.   if (teclaIngresada) {
  502.     contadorCaracteres++;
  503.     lcd.setCursor(indicePantalla, 0);
  504.  
  505.     if (contadorCaracteres <= 6) {
  506.       lcd.print(teclaIngresada);
  507.       passwordIngresado += teclaIngresada;
  508.       if (passwordIngresado == pass) {
  509.         accesoHabilitado = true;
  510.         Serial.println("CONTRASENIA CORRECTA MI TERNA");
  511.       }
  512.       else{
  513.         Serial.print("CONTRASEÑA DEFINIDA: ");
  514.         Serial.println(pass);
  515.         Serial.print("CONTRASEÑA INGRESADA: ");
  516.         Serial.print(passwordIngresado);
  517.       }
  518.     }
  519.     indicePantalla++;
  520.  
  521.     // Se presionó el botón de aceptar o 'ENTER'
  522.     if (teclaIngresada == '*') {
  523.       contadorCaracteres = 0;
  524.       if (accesoHabilitado == true and !passwordIngresado.isEmpty()) {
  525.         lcd.clear();
  526.         lcd.setCursor(0, 0);
  527.         lcd.print("Acceso permitido");
  528.         lcd.setCursor(0, 1);
  529.         lcd.print("Bienvenido");
  530.         digitalWrite(pinLedVerde, HIGH);
  531.         delay(2000);
  532.         digitalWrite(pinLedVerde, LOW);
  533.         delay(500);
  534.         delay(2000);
  535.         lcd.clear();
  536.         indicePantalla = 7;
  537.         accesoHabilitado = false;
  538.         passwordIngresado = "";
  539.         contadorCaracteres = 0;
  540.  
  541.       } else if (passwordIngresado.isEmpty()) {
  542.         lcd.clear();
  543.         lcd.setCursor(0, 1);
  544.         lcd.print("Contrasena nula");
  545.         digitalWrite(pinLedRojo, HIGH);
  546.         delay(2000);
  547.         digitalWrite(pinLedRojo, LOW);
  548.         delay(500);
  549.         lcd.setCursor(0, 0);
  550.         indicePantalla = 7;
  551.         accesoHabilitado = false;
  552.         passwordIngresado = "";
  553.         contadorCaracteres = 0;
  554.       } else {
  555.         // Mostramos el mensaje de acceso denegado
  556.         lcd.clear();
  557.         lcd.setCursor(0, 1);
  558.         lcd.print("Acceso denegado");
  559.         digitalWrite(pinLedRojo, HIGH);
  560.         delay(2000);
  561.         digitalWrite(pinLedRojo, LOW);
  562.         delay(500);
  563.         intento++;
  564.         delay(1000);
  565.         lcd.setCursor(0, 0);
  566.         lcd.print("Intentos: ");
  567.         lcd.setCursor(9, 0);
  568.         lcd.print(intento);
  569.         delay(1000);
  570.         lcd.clear();
  571.         indicePantalla = 7;
  572.         accesoHabilitado = false;
  573.         passwordIngresado = "";
  574.         contadorCaracteres = 0;
  575.         lcd.clear();
  576.       }
  577.       if (intento > 3) {
  578.         while (conteo != 0) {
  579.           lcd.clear();
  580.           lcd.setCursor(0, 0);
  581.           lcd.print("Sistema");
  582.           lcd.setCursor(0, 1);
  583.           lcd.print("Bloqueado");
  584.           lcd.setCursor(12, 1);
  585.           lcd.print(conteo);
  586.           delay(50);
  587.           conteo--;
  588.         }
  589.         intento = 0;
  590.         lcd.clear();
  591.       }
  592.     }
  593.     if (teclaIngresada == '#') {
  594.       lcd.clear();
  595.       lcd.setCursor(0, 0);
  596.       lcd.print("Contrasena");
  597.       lcd.setCursor(0, 1);
  598.       lcd.print("reiniciada");
  599.       digitalWrite(pinLedRojo, HIGH);
  600.       delay(2000);
  601.       digitalWrite(pinLedRojo, LOW);
  602.       delay(500);
  603.       lcd.clear();
  604.       passwordIngresado = "";
  605.       contadorCaracteres = 0;
  606.       indicePantalla = 7;
  607.       accesoHabilitado = false;
  608.       lcd.clear();
  609.     }
  610.  
  611.   }
  612.  
  613. }
  614.  
  615.  
  616. ---------------------------VERSIÓN 4 ---------------------------------------------------------
  617.  
  618. // importa libreria Keypad
  619. #include <Keypad.h>
  620. #include <LiquidCrystal_I2C.h>
  621.  
  622. //SENSOR DE DISTANCIA
  623. LiquidCrystal_I2C lcd2(0x3F,16,2);  // set the LCD address to 0x27 for a 16 chars and 2 line display
  624. int echo = 13;
  625. int trigger = 12;
  626. int pulso;
  627. long tiempo;
  628. float distancia;
  629.  
  630.                                 // Crear el objeto lcd  dirección  0x3F y 16
  631.                                 // columnas x 2 filas
  632.                                 LiquidCrystal_I2C lcd(0x27, 16, 2);  //
  633.  
  634. // define numero de filas
  635. const uint8_t ROWS = 4;
  636. // define numero de columnas
  637. const uint8_t COLS = 4;
  638. // define la distribucion de teclas
  639. char keys[ROWS][COLS] = {{'1', '2', '3', 'A'},
  640.                          {'4', '5', '6', 'B'},
  641.                          {'7', '8', '9', 'C'},
  642.                          {'*', '0', '#', 'D'}};
  643. // pines correspondientes a las filas
  644. uint8_t colPins[COLS] = {2, 15, 25, 26};
  645. // pines correspondientes a las columnas
  646. uint8_t rowPins[ROWS] = {19, 18, 5, 4};
  647. // crea objeto con los prametros creados previamente
  648. Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
  649. // Declaramos la variable que almacena el password
  650. // char pass[7]="ABCD45";
  651. String pass = "ABC123";
  652. String passwordIngresado;
  653. // Varible que alamacena la clave que se va ingresando
  654. char ingresapass[7];
  655. // Variable que define el numero de letra que se va ingresando
  656. int indice = 0;
  657. int pinLedVerde = 12;
  658. int pinLedRojo = 13;
  659. bool accesoHabilitado = false;
  660. int intento = 1;
  661. int contadorCaracteres = 0;
  662. int indicePantalla = 7;
  663. int echoController = true;
  664.  
  665. void setup() {
  666.   //SENSOR DE MOVIMIENTO
  667.   pinMode(echo,INPUT);
  668.   pinMode(trigger,OUTPUT);
  669.   lcd2.init();                      // initialize the lcd
  670.   lcd2.init();
  671.  
  672.   // PANTALLA LCD
  673.   // Inicializar el LCD
  674.   lcd.init();
  675.  
  676.   // Encender la luz de fondo.
  677.   lcd.backlight();
  678.  
  679.   // KEYPAD
  680.   pinMode(pinLedVerde, OUTPUT);
  681.   pinMode(pinLedRojo, OUTPUT);
  682.   Serial.begin(115200);
  683.  
  684.   // configuración del LCD
  685.   lcd.begin(16, 2);
  686.   // Mensaje inicial LCD
  687.   lcd.print("Digite su clave:");
  688.   lcd.setCursor(0, 1);
  689.   lcd.print("* para ingresar");
  690.   delay(500);
  691.   // Desplazamiento del texto
  692.   for (int pos = 0; pos < 13; pos++) {
  693.     // izquierda
  694.     lcd.scrollDisplayLeft();
  695.     delay(100);
  696.   }
  697.   // desplazamiento a la derecha
  698.   for (int pos = 0; pos < 26; pos++) {
  699.     lcd.scrollDisplayRight();
  700.     delay(100);
  701.   }
  702.   delay(500);
  703.   lcd.clear();
  704. }
  705.  
  706. void loop() {
  707.   if (echoController){
  708.     //SENSOR DE MOVIMIENTO
  709.     digitalWrite(trigger,HIGH);     //Para estabilizar el sensor
  710.     delayMicroseconds(20);
  711.     digitalWrite(trigger,LOW);     // envío del pulso ultrasónico
  712.     tiempo = pulseIn(echo,HIGH);      //Función para medir la longitud del pulso entrante
  713.       Serial.println(tiempo);
  714.     //Obtener distancia en
  715.     distancia= (0.0175*tiempo);  //Fórmula para calcular la distancia en valor entero
  716.     Serial.println(distancia);
  717.     //  lcd2.print(distancia);
  718.     //  lcd2.print("cm");
  719.     lcd2.clear();
  720.     if (distancia < 20) {
  721.       echoController = false;
  722.     }
  723.   }
  724.   else {
  725.     int conteo = 60;
  726.     // obtiene tecla presionada y asigna a variable
  727.     lcd.setCursor(0, 0);
  728.     // print the number of second since reset:
  729.     lcd.print("clave: ");
  730.     char teclaIngresada = keypad.getKey();
  731.     Serial.println(teclaIngresada);
  732.     lcd.display();
  733.  
  734.     // CUANDO EL USUARIO INGRESA LA CONTRASEÑA
  735.     // Se obtuvo correctamente un caracter
  736.     if (teclaIngresada) {
  737.       contadorCaracteres++;
  738.       lcd.setCursor(indicePantalla, 0);
  739.  
  740.       if (contadorCaracteres <= 6) {
  741.         lcd.print(teclaIngresada);
  742.         passwordIngresado += teclaIngresada;
  743.         if (passwordIngresado == pass) {
  744.           accesoHabilitado = true;
  745.           Serial.println("CONTRASENIA CORRECTA MI TERNA");
  746.         } else {
  747.           Serial.print("CONTRASEÑA DEFINIDA: ");
  748.           Serial.println(pass);
  749.           Serial.print("CONTRASEÑA INGRESADA: ");
  750.           Serial.print(passwordIngresado);
  751.         }
  752.       }
  753.       indicePantalla++;
  754. a
  755.       // Se presionó el botón de aceptar o 'ENTER'
  756.       if (teclaIngresada == '*') {
  757.         contadorCaracteres = 0;
  758.         if (accesoHabilitado == true and !passwordIngresado.isEmpty()) {
  759.           lcd.clear();
  760.           lcd.setCursor(0, 0);
  761.           lcd.print("Acceso permitido");
  762.           lcd.setCursor(0, 1);
  763.           lcd.print("Bienvenido");
  764.           digitalWrite(pinLedVerde, HIGH);
  765.           delay(2000);
  766.           digitalWrite(pinLedVerde, LOW);
  767.           delay(500);
  768.           delay(2000);
  769.           lcd.clear();
  770.           indicePantalla = 7;
  771.           accesoHabilitado = false;
  772.           passwordIngresado = "";
  773.           contadorCaracteres = 0;
  774.  
  775.           //SENSOR DE MOVIMIENTO
  776.           digitalWrite(trigger,HIGH);     //Para estabilizar el sensor
  777.           delayMicroseconds(20);
  778.           digitalWrite(trigger,LOW);     // envío del pulso ultrasónico
  779.           tiempo = pulseIn(echo,HIGH);      //Función para medir la longitud del pulso entrante
  780.             Serial.println(tiempo);
  781.           //Obtener distancia en
  782.           distancia= (0.0175*tiempo);  //Fórmula para calcular la distancia en valor entero
  783.           Serial.println(distancia);
  784.           //  lcd2.print(distancia);
  785.           //  lcd2.print("cm");
  786.           lcd2.clear();
  787.           if (distancia < 20) {
  788.             echoController = false;
  789.           }
  790.         } else if (passwordIngresado.isEmpty()) {
  791.           lcd.clear();
  792.           lcd.setCursor(0, 1);
  793.           lcd.print("Contrasena nula");
  794.           digitalWrite(pinLedRojo, HIGH);
  795.           delay(2000);
  796.           digitalWrite(pinLedRojo, LOW);
  797.           delay(500);
  798.           lcd.setCursor(0, 0);
  799.           indicePantalla = 7;
  800.           accesoHabilitado = false;
  801.           passwordIngresado = "";
  802.           contadorCaracteres = 0;
  803.         } else {
  804.           // Mostramos el mensaje de acceso denegado
  805.           lcd.clear();
  806.           lcd.setCursor(0, 1);
  807.           lcd.print("Acceso denegado");
  808.           digitalWrite(pinLedRojo, HIGH);
  809.           delay(2000);
  810.           digitalWrite(pinLedRojo, LOW);
  811.           delay(500);
  812.           intento++;
  813.           delay(1000);
  814.           lcd.setCursor(0, 0);
  815.           lcd.print("Intentos: ");
  816.           lcd.setCursor(9, 0);
  817.           lcd.print(intento);
  818.           delay(1000);
  819.           lcd.clear();
  820.           indicePantalla = 7;
  821.           accesoHabilitado = false;
  822.           passwordIngresado = "";
  823.           contadorCaracteres = 0;
  824.           lcd.clear();
  825.         }
  826.         if (intento > 3) {
  827.           while (conteo != 0) {
  828.             lcd.clear();
  829.             lcd.setCursor(0, 0);
  830.             lcd.print("Sistema");
  831.             lcd.setCursor(0, 1);
  832.             lcd.print("Bloqueado");
  833.             lcd.setCursor(12, 1);
  834.             lcd.print(conteo);
  835.             delay(50);
  836.             conteo--;
  837.           }
  838.           intento = 0;
  839.           lcd.clear();
  840.         }
  841.       }
  842.       if (teclaIngresada == '#') {
  843.         lcd.clear();
  844.         lcd.setCursor(0, 0);
  845.         lcd.print("Contrasena");
  846.         lcd.setCursor(0, 1);
  847.         lcd.print("reiniciada");
  848.         digitalWrite(pinLedRojo, HIGH);
  849.         delay(2000);
  850.         digitalWrite(pinLedRojo, LOW);
  851.         delay(500);
  852.         lcd.clear();
  853.         passwordIngresado = "";
  854.         contadorCaracteres = 0;
  855.         indicePantalla = 7;
  856.         accesoHabilitado = false;
  857.         lcd.clear();
  858.       }
  859.     }
  860.   }
  861.  
  862. }
  863.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement