|
| 1 | +//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> RFID + LCD + Button (Combined) |
| 2 | +//----------------------------------------Including the libraries. |
| 3 | +#include <LiquidCrystal_I2C.h> |
| 4 | +#include <SPI.h> |
| 5 | +#include <MFRC522.h> |
| 6 | +//---------------------------------------- |
| 7 | + |
| 8 | +// Defines SS/SDA PIN and Reset PIN for RFID-RC522. |
| 9 | +#define SS_PIN 5 |
| 10 | +#define RST_PIN 4 |
| 11 | + |
| 12 | +// Defines the button PIN. |
| 13 | +#define BTN_PIN 15 |
| 14 | + |
| 15 | +// LCD configuration |
| 16 | +int lcdColumns = 20; |
| 17 | +int lcdRows = 4; |
| 18 | + |
| 19 | +// Variables |
| 20 | +int readsuccess; |
| 21 | +char str[32] = ""; |
| 22 | +String UID_Result = "--------"; |
| 23 | + |
| 24 | +// LCD object (address: 0x27, 20 cols, 4 rows) |
| 25 | +LiquidCrystal_I2C lcd(0x27, lcdColumns, lcdRows); |
| 26 | + |
| 27 | +// RFID object |
| 28 | +MFRC522 mfrc522(SS_PIN, RST_PIN); |
| 29 | + |
| 30 | +//________________________________________________________________________________byteArray_to_string() |
| 31 | +void byteArray_to_string(byte array[], unsigned int len, char buffer[]) { |
| 32 | + for (unsigned int i = 0; i < len; i++) { |
| 33 | + byte nib1 = (array[i] >> 4) & 0x0F; |
| 34 | + byte nib2 = (array[i] >> 0) & 0x0F; |
| 35 | + buffer[i*2+0] = nib1 < 0xA ? '0' + nib1 : 'A' + nib1 - 0xA; |
| 36 | + buffer[i*2+1] = nib2 < 0xA ? '0' + nib2 : 'A' + nib2 - 0xA; |
| 37 | + } |
| 38 | + buffer[len*2] = '\0'; |
| 39 | +} |
| 40 | +//________________________________________________________________________________ |
| 41 | + |
| 42 | +//________________________________________________________________________________getUID() |
| 43 | +int getUID() { |
| 44 | + if(!mfrc522.PICC_IsNewCardPresent()) { |
| 45 | + return 0; |
| 46 | + } |
| 47 | + if(!mfrc522.PICC_ReadCardSerial()) { |
| 48 | + return 0; |
| 49 | + } |
| 50 | + |
| 51 | + byteArray_to_string(mfrc522.uid.uidByte, mfrc522.uid.size, str); |
| 52 | + UID_Result = str; |
| 53 | + |
| 54 | + Serial.println(); |
| 55 | + Serial.print("UID : "); |
| 56 | + Serial.println(UID_Result); |
| 57 | + |
| 58 | + mfrc522.PICC_HaltA(); |
| 59 | + mfrc522.PCD_StopCrypto1(); |
| 60 | + |
| 61 | + return 1; |
| 62 | +} |
| 63 | +//________________________________________________________________________________ |
| 64 | + |
| 65 | +//________________________________________________________________________________setup() |
| 66 | +void setup(){ |
| 67 | + // Start serial |
| 68 | + Serial.begin(115200); |
| 69 | + delay(1000); |
| 70 | + Serial.println(); |
| 71 | + Serial.println("Please tap your card or keychain..."); |
| 72 | + |
| 73 | + // Setup button |
| 74 | + pinMode(BTN_PIN, INPUT_PULLUP); |
| 75 | + |
| 76 | + // Init LCD |
| 77 | + lcd.init(); |
| 78 | + lcd.backlight(); |
| 79 | + lcd.clear(); |
| 80 | + |
| 81 | + // Init SPI & RFID |
| 82 | + SPI.begin(); |
| 83 | + mfrc522.PCD_Init(); |
| 84 | + delay(1000); |
| 85 | +} |
| 86 | +//________________________________________________________________________________ |
| 87 | + |
| 88 | +//________________________________________________________________________________loop() |
| 89 | +void loop(){ |
| 90 | + lcd.setCursor(0, 0); |
| 91 | + lcd.print("Tap your card/key "); |
| 92 | + |
| 93 | + lcd.setCursor(0, 1); |
| 94 | + lcd.print("or keychain "); |
| 95 | + |
| 96 | + lcd.setCursor(0, 2); |
| 97 | + lcd.print("UID: "); |
| 98 | + lcd.print(UID_Result); |
| 99 | + lcd.print(" "); // Clear extra space |
| 100 | + |
| 101 | + lcd.setCursor(0, 3); |
| 102 | + lcd.print("BTN: "); |
| 103 | + lcd.print(digitalRead(BTN_PIN)); |
| 104 | + lcd.print(" "); // Clear old text |
| 105 | + |
| 106 | + readsuccess = getUID(); |
| 107 | + |
| 108 | + if(readsuccess){ |
| 109 | + delay(2000); |
| 110 | + } |
| 111 | + |
| 112 | + delay(100); |
| 113 | +} |
| 114 | + |
| 115 | + |
| 116 | + |
| 117 | +//________________________________________________________________________________ |
| 118 | +//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< |
0 commit comments