Advertisement
RuiViana

Alarme_Porta

Sep 8th, 2016
308
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.67 KB | None | 0 0
  1. //#include <Wire.h>
  2. #include <LiquidCrystal.h>
  3. #include <Keypad.h>
  4. #define sensorPorta 2
  5. #define ledDigitacao 3
  6. int statusPorta = 0;
  7. bool acessoLiberado = false;
  8. bool jaMostrou = false;
  9. bool jaMostrouPortaAberta = false;
  10. int count = 0;                                              //Contador de uso geral
  11. char pass [4] = {'1', '9', '7', '1'};                 //Senha
  12.  
  13. const byte ROWS = 4; //four rows
  14. const byte COLS = 3; //three columns
  15. char keys[ROWS][COLS] = {{'1', '2', '3'}, {'4', '5', '6'}, {'7', '8', '9'}, {'*', '0', '#'}};
  16.  
  17. byte rowPins[ROWS] = {10, 9, 8, 7}; //connect to the row pinouts of the keypad
  18. byte colPins[COLS] = {13, 12, 11}; //connect to the column pinouts of the keypad
  19.  
  20. LiquidCrystal lcd(A0, A1, A2, A3, A4, A5);
  21. Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
  22. // --------------------------
  23. void setup()
  24. {
  25.   pinMode(ledDigitacao, OUTPUT);
  26. //  pinMode(sensorPorta, INPUT_PULLUP);                   // <<<<<<< Pullup
  27.   pinMode(sensorPorta, INPUT);
  28.   lcd.begin(20, 4);
  29.   lcd.setCursor(0, 0);
  30.   lcd.clear();
  31.   Serial.begin(9600);
  32.   statusPorta = digitalRead(sensorPorta);
  33.   if (statusPorta == HIGH)                              // se a porta está fechada...
  34.     key_init();                                         // pedir pra digitar a senha
  35.   else
  36.   {
  37.     lcd.print("Porta aberta");
  38.   }
  39.   // Interrupt pelo switch da porta ligado no port 2 ao ir de HIGH para LOW
  40.   attachInterrupt(digitalPinToInterrupt(sensorPorta), AbriuPorta, FALLING);
  41. }
  42. //---------------------------
  43. void AbriuPorta()
  44. {
  45.   count = 5;                                            // Se abriu a porta o valor 5 força a saida da rotina de senha
  46.   jaMostrou = false;
  47. }
  48. // --------------------------
  49. void loop()
  50. {
  51.   if (portaEstaAberta())                                // se a porta está fechada... tem que ativar o alarme
  52.   {
  53.     jaMostrouPortaAberta = false;                       // Apronta para exibir mensagem de porta aberta
  54.     if(digitalRead(sensorPorta)==HIGH)                  // So faz aproxima linha se a porta estiver fechada
  55.     key_init();                                         // aparece a tela de digitar a senha
  56.     char key = keypad.getKey();
  57.     if (key != NO_KEY)                                  // Se foi pressionada uma tecla:
  58.     {
  59.       if (key == '#')                                   // Se a tecla é '#'
  60.       {
  61.         code_entry_init();                              // Então espera que seja inserida uma senha
  62.         int entrada = 0;
  63.         while (count < 4 )                              // Conta 4 entradas/teclas
  64.         {
  65. //                                                      // Creio que aqui tenho que ficar monitorando se a porta abre e interromper a execução do loop()
  66.           char key = keypad.getKey();                   // Obtém tecla pressionada
  67.           if (key != NO_KEY)                            // 1 Se foi pressionada uma tecla:
  68.           {
  69.             lcd.print("#");
  70.             digitalWrite(ledDigitacao, HIGH);
  71.             delay(300);
  72.             digitalWrite(ledDigitacao, LOW);
  73.             entrada += 1;                               // Faz entrada = entrada + 1
  74.             if (key == pass[count])count += 1;          // Se a tecla pressionada corresponde ao dígito da senha correspondente, soma 1 no contador
  75.             if ( count == 4 ) unlocked();               // Se contador chegou a 4 e com dígitos corretos, desbloqueia siatema
  76.             if ((key == '#') || (entrada == 4))         // Se foi pressionada a tecla "#' ou foram feitas 4 entradas,
  77.             {
  78.               key_init();                               // Reinicializa o sistema
  79.               break;                                    // Para o sistema e espera por uma tecla
  80.             }
  81.           }            // fim do 1Se pressionada uma tecla
  82.         }              // fim do conta 4 entradas/teclas
  83.       }                // fim da entrada da tecla #
  84.     }                  // fim do se pressionada uma tecla
  85.   }                    // fim do se a porta está fechada
  86. }
  87. // --------------------------
  88. boolean portaEstaAberta()
  89. {
  90.   statusPorta = digitalRead(sensorPorta);               // verifica se a porta está aberta ou fechada
  91.   delay(200);
  92.   if (statusPorta == LOW)
  93.   {
  94.     acessoLiberado        = false;
  95.     if (!jaMostrouPortaAberta)
  96.     {
  97.       lcd.clear();
  98.       lcd.print("Porta aberta");
  99.       Serial.println("Porta aberta");
  100.       jaMostrouPortaAberta  = true;
  101.     }
  102.     return false;
  103.   }
  104.   else
  105.   {
  106.     return true;
  107.   }
  108. }
  109. // --------------------------
  110. void key_init ()                                        // tela de "digite a senha"
  111. {
  112.   if (!jaMostrou)
  113.   {
  114.     lcd.clear();                                        //Limpa o LCD
  115.     lcd.print("Aguardando...");                         //Emite mensagem
  116.     lcd.setCursor(0, 1);                                //Muda de linha
  117.     lcd.print("Tecle #");                               //Emite mensagem
  118.     Serial.println("Aguardando... Tecle#");
  119.     jaMostrou = true;
  120.   }
  121.   count = 0;                                            //Variável count é zero na inicialização
  122. }
  123. // --------------------------
  124. void code_entry_init()                                  //Subrotina de Entrada da Senha
  125. {
  126.   lcd.clear();
  127.   lcd.print("Entre a Senha:");
  128.   count = 0;                                            //Variável count é zero na entrada de senha
  129. }
  130. // --------------------------
  131. void unlocked()                                         //Subrotina para Acesso Liberado
  132. {
  133.   lcd.clear();                                          //Limpa LCD
  134.   lcd.print("Acesso Liberado!");                        //Emite mensagem
  135.   acessoLiberado = true;
  136.   delay(2000);
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement