Wesley_Alves

Arduino 4x4 Keypad Password

Mar 9th, 2017
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.06 KB | None | 0 0
  1. /*
  2.   Aluno: Wesley Alves Santana
  3.   Turma: 3IELN2017
  4.   Simulação: https://circuits.io/circuits/4014551-sistemas-programaveis-keypad-diplsay-de-7-segmentos/
  5. */
  6.  
  7. // Keypad:
  8. const byte ROW[] = {A0, A1, A2, A3};
  9. // Display 7 segmentos:
  10. const byte COL[] = {2, 3, 4, 5};
  11. // Segmentos:        a,  b,  c,  d,  e,  f,  g,  p
  12. const byte SEG[] = {12, 13,  7,  8,  9, 11, 10,  6};
  13. // Teclado:
  14. const char KEYS[4][4] = {{'A', 'B', 'C', 'D'}, {'3', '6', '9', '#'}, {'2', '5', '8', '0'}, {'1', '4', '7', '*'}};
  15. //Senha Padrão:
  16. const char PASSWORD[] = {'B', 'C', 'D', '8', '4', '2'};
  17.  
  18. void setup() {
  19.   for (byte i = 2; i <= 13; i++) {
  20.     pinMode (i, OUTPUT);
  21.     digitalWrite (i, LOW);
  22.   }
  23.   Serial.begin(9600);
  24.   Serial.println("=========================");
  25. }
  26.  
  27. void loop() {
  28.   delay(10);
  29.   char pressedKey = getKey();
  30.   if (pressedKey != 'w') {
  31.     senha(pressedKey);
  32.   }
  33. }
  34.  
  35. char scanKey() {
  36.   char r = 'w';
  37.   for (byte i = 0; i < 4; i++) {
  38.     digitalWrite(COL[i], HIGH);
  39.     for (byte j = 0; j < 4; j++) {
  40.       if (digitalRead(ROW[j])) {
  41.         r = KEYS[i][j];
  42.       }
  43.     }
  44.     digitalWrite(COL[i], LOW);
  45.   }
  46.   return r;
  47. }
  48.  
  49. char getKey() {
  50.   static byte estado = 0;
  51.   char key = 'w';
  52.   char scan = scanKey();
  53.   switch (estado) {
  54.     case 0:
  55.       if (scan != 'w') {
  56.         estado = 1;
  57.         key = scan;
  58.       }
  59.       break;
  60.     case 1:
  61.       if (scan == 'w') {
  62.         key = 'w';
  63.         estado = 0;
  64.       }
  65.       break;
  66.   }
  67.   return key;
  68. }
  69.  
  70. void senha(char pressedKey) {
  71.   static bool keepTesting = true, checkPass = false;
  72.   static byte counter = 0;
  73.   if (pressedKey != '#') {
  74.     Serial.print(pressedKey);
  75.     if (keepTesting) {
  76.       if (counter <= 5 && pressedKey == PASSWORD[counter]) {
  77.         checkPass = true;
  78.         counter++;
  79.       } else {
  80.         checkPass = false;
  81.         keepTesting = false;
  82.       }
  83.     }
  84.   }  else if (checkPass) {
  85.     counter = 0;
  86.     keepTesting = true;
  87.     Serial.println("\nSenha correta.");
  88.   } else {
  89.     counter = 0;
  90.     keepTesting = true;
  91.     Serial.println("\nSenha errada.");
  92.   }
  93. }
Advertisement
Add Comment
Please, Sign In to add comment