Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SPI.h>
- #include <MFRC522.h>
- #include <LiquidCrystal_I2C.h>
- #include <Keypad.h>
- const uint8_t RST_PIN = 9;
- const uint8_t SS_PIN = 10;
- const uint8_t ROWS = 4;
- const uint8_t COLS = 4;
- char hexaKeys[ROWS][COLS] = {
- {'1','4','7','*'},
- {'2','5','8','0'},
- {'3','6','9','#'},
- {'A','B','C','D'}
- };
- uint8_t rowPins[ROWS] = {3, 2, 1, 0};
- uint8_t colPins[COLS] = {7, 6, 5, 4};
- Keypad customKeypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
- MFRC522 rfid(SS_PIN, RST_PIN);
- MFRC522::MIFARE_Key key;
- MFRC522::StatusCode status;
- LiquidCrystal_I2C lcd(0x27, 16, 2);
- void initialaize() {
- Serial.begin(9600);
- SPI.begin();
- rfid.PCD_Init();
- for (uint8_t i = 0; i < 6; ++i) key.keyByte[i] = 0xFF;
- lcd.init();
- lcd.backlight();
- lcd.clear();
- lcd.setCursor(0, 0);
- lcd.print("Attach a card");
- }
- void print_uid(uint8_t* uid) {
- lcd.setCursor(0, 0);
- Serial.print("UID: ");
- for (uint8_t i = 0; i < 4; ++i) {
- Serial.print(uid[i]);
- lcd.print(uid[i]);
- }
- Serial.println();
- }
- void get_uid(uint8_t* uid) {
- for (uint8_t i = 0; i < 4; ++i) uid[i] = rfid.uid.uidByte[i];
- }
- bool array_compare(uint8_t* first_array, uint8_t* second_array, uint64_t first_array_size, uint64_t second_array_size) {
- if (first_array_size != second_array_size) return false;
- for (uint64_t i = 0; i < first_array_size; ++i) {
- if (first_array[i] != second_array[i]) return false;
- }
- return true;
- }
- bool check_card() {
- if (!rfid.PICC_IsNewCardPresent()) return false;
- if (!rfid.PICC_ReadCardSerial()) return false;
- return true;
- }
- bool get_pin_code() {
- lcd.setCursor(0, 0);
- lcd.print("Enter pin code");
- lcd.setCursor(0, 1);
- char pin_code[4] = {'1', '2', '4', '5'};
- char user_pin_code[4];
- for (uint8_t i = 0; i < 4; ++i) {
- while (true) {
- char customKey = customKeypad.getKey();
- if (customKey) {
- Serial.println(customKey);
- lcd.print(customKey);
- user_pin_code[i] = customKey;
- break;
- }
- }
- }
- return array_compare(pin_code, user_pin_code, 4, 4);
- }
- void setup() {
- initialaize();
- }
- void loop() {
- if (!check_card()) return;
- uint8_t uid[4];
- uint8_t a[4] = {5, 135, 178, 249};
- get_uid(uid);
- print_uid(uid);
- if (get_pin_code()) {
- lcd.clear();
- lcd.setCursor(0, 0);
- lcd.print("Done!");
- }
- else {
- lcd.clear();
- lcd.setCursor(0, 0);
- lcd.print("Wrong pin code");
- }
- delay(3000);
- lcd.clear();
- lcd.setCursor(0, 0);
- lcd.print("Attach a card");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement