mc_workshop

DID keypad_pin

May 16th, 2021
568
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.   Keypad code
  3.   July 2020 by MC Workshop.
  4.   This example code is in the public domain.
  5. */
  6. #include <Wire.h>
  7. #include <LiquidCrystal_I2C.h>
  8. #include <Keypad.h>
  9. #define Password_Length 4 //lunghezza della password + 1
  10. int signalPin = 13; //LED di bordo
  11. char Data[Password_Length];
  12. char Master[Password_Length] = "123"; // Password
  13. byte data_count = 0, master_count = 0;
  14. bool Pass_is_good;
  15. char customKey;
  16. const byte ROWS = 4;
  17. const byte COLS = 4;
  18. char hexaKeys[ROWS][COLS] = {
  19.   {'1', '2', '3', 'A'},
  20.   {'4', '5', '6', 'B'},
  21.   {'7', '8', '9', 'C'},
  22.   {'*', '0', '#', 'D'}
  23. };
  24. byte rowPins[ROWS] = {9, 8, 7, 6};
  25. byte colPins[COLS] = {5, 4, 3, 2};
  26. Keypad customKeypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
  27. LiquidCrystal_I2C lcd(0x21, 16, 2);
  28. void setup() {
  29.   lcd.init();
  30.   lcd.backlight();
  31.   pinMode(signalPin, OUTPUT);
  32.   Serial.begin(9600);
  33.   Serial.println ("Immettere password");
  34. }
  35. void loop() {
  36.   lcd.setCursor(0, 0);
  37.   lcd.print("Enter Password:");
  38.  
  39.   customKey = customKeypad.getKey();
  40.   if (customKey) {
  41.     Data[data_count] = customKey;
  42.     lcd.setCursor(data_count, 1);
  43.     lcd.print(Data[data_count]);
  44.     data_count++;
  45.   }
  46.   if (data_count == Password_Length - 1) {
  47.     lcd.clear();
  48.     if (!strcmp(Data, Master)) {
  49.       lcd.print("Correct");
  50.       Serial.println ("Password OK");
  51.       digitalWrite(signalPin, HIGH);
  52.       Serial.println ("LED pin 13 acceso");
  53.       delay(5000);
  54.       digitalWrite(signalPin, LOW);
  55.       Serial.println ("LED pin 13 spento");
  56.     }
  57.     else {
  58.       lcd.print("Incorrect");
  59.       Serial.println ("Password errata");
  60.       delay(1000);
  61.     }
  62.     lcd.clear();
  63.     clearData();
  64.   }
  65. }
  66. void clearData() {
  67.   while (data_count != 0) {
  68.     Data[data_count--] = 0;
  69.   }
  70.   return;
  71. }
RAW Paste Data