Advertisement
Mondegrin

Kassa

Jul 16th, 2023
1,369
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.55 KB | Source Code | 0 0
  1. #include <SPI.h>
  2. #include <MFRC522.h>
  3.  
  4. #include <LiquidCrystal_I2C.h>
  5.  
  6. #include <Keypad.h>
  7.  
  8. const uint8_t RST_PIN = 9;
  9. const uint8_t SS_PIN = 10;
  10.  
  11. const uint8_t ROWS = 4;
  12. const uint8_t COLS = 4;
  13.  
  14. char hexaKeys[ROWS][COLS] = {
  15.   {'1','4','7','*'},
  16.   {'2','5','8','0'},
  17.   {'3','6','9','#'},
  18.   {'A','B','C','D'}
  19. };
  20.  
  21. uint8_t rowPins[ROWS] = {3, 2, 1, 0};
  22. uint8_t colPins[COLS] = {7, 6, 5, 4};
  23.  
  24. Keypad customKeypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
  25.  
  26. MFRC522 rfid(SS_PIN, RST_PIN);
  27. MFRC522::MIFARE_Key key;
  28. MFRC522::StatusCode status;
  29.  
  30. LiquidCrystal_I2C lcd(0x27, 16, 2);
  31.  
  32. void initialaize() {
  33.   Serial.begin(9600);
  34.  
  35.   SPI.begin();
  36.   rfid.PCD_Init();
  37.   for (uint8_t i = 0; i < 6; ++i) key.keyByte[i] = 0xFF;
  38.  
  39.   lcd.init();
  40.   lcd.backlight();
  41.   lcd.clear();
  42.   lcd.setCursor(0, 0);
  43.   lcd.print("Attach a card");
  44. }
  45.  
  46. void print_uid(uint8_t* uid) {
  47.   lcd.setCursor(0, 0);
  48.  
  49.   Serial.print("UID: ");
  50.   for (uint8_t i = 0; i < 4; ++i) {
  51.     Serial.print(uid[i]);
  52.     lcd.print(uid[i]);
  53.   }
  54.   Serial.println();
  55. }
  56.  
  57. void get_uid(uint8_t* uid) {
  58.   for (uint8_t i = 0; i < 4; ++i) uid[i] = rfid.uid.uidByte[i];
  59. }
  60.  
  61. bool array_compare(uint8_t* first_array, uint8_t* second_array, uint64_t first_array_size, uint64_t second_array_size) {
  62.   if (first_array_size != second_array_size) return false;
  63.   for (uint64_t i = 0; i < first_array_size; ++i) {
  64.     if (first_array[i] != second_array[i]) return false;
  65.   }
  66.   return true;
  67. }
  68.  
  69. bool check_card() {
  70.   if (!rfid.PICC_IsNewCardPresent()) return false;
  71.   if (!rfid.PICC_ReadCardSerial()) return false;
  72.   return true;
  73. }
  74.  
  75. bool get_pin_code() {
  76.   lcd.setCursor(0, 0);
  77.   lcd.print("Enter pin code");
  78.   lcd.setCursor(0, 1);
  79.   char pin_code[4] = {'1', '2', '4', '5'};
  80.   char user_pin_code[4];
  81.   for (uint8_t i = 0; i < 4; ++i) {
  82.     while (true) {
  83.       char customKey = customKeypad.getKey();
  84.       if (customKey) {
  85.         Serial.println(customKey);
  86.         lcd.print(customKey);
  87.         user_pin_code[i] = customKey;
  88.         break;
  89.       }
  90.     }
  91.   }
  92.   return array_compare(pin_code, user_pin_code, 4, 4);
  93. }
  94.  
  95. void setup() {
  96.   initialaize();
  97. }
  98.  
  99. void loop() {
  100.   if (!check_card()) return;
  101.   uint8_t uid[4];
  102.   uint8_t a[4] = {5, 135, 178, 249};
  103.   get_uid(uid);
  104.   print_uid(uid);
  105.   if (get_pin_code()) {
  106.     lcd.clear();
  107.     lcd.setCursor(0, 0);
  108.     lcd.print("Done!");
  109.   }
  110.   else {
  111.     lcd.clear();
  112.     lcd.setCursor(0, 0);
  113.     lcd.print("Wrong pin code");
  114.   }
  115.   delay(3000);
  116.   lcd.clear();
  117.   lcd.setCursor(0, 0);
  118.   lcd.print("Attach a card");
  119. }
  120.  
Tags: C++ Arduino
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement