Advertisement
leo3065

[Arduino] Reading/Writing RFID tag to record and count

Jul 1st, 2016
2,641
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.96 KB | None | 0 0
  1. /* The power for MFRC522 modile and LCD 5110 is 3.3 volts!!
  2.    Pin for MFRC522:
  3.    -----------------------------------------------------------------------------------------
  4.                MFRC522      Arduino       Arduino   Arduino    Arduino          Arduino
  5.                Reader/PCD   Uno           Mega      Nano v3    Leonardo/Micro   Pro Micro
  6.    Signal      Pin          Pin           Pin       Pin        Pin              Pin
  7.    -----------------------------------------------------------------------------------------
  8.    RST/Reset   RST          9             5         D9         RESET/ICSP-5     RST
  9.    SPI SS      SDA(SS)      10            53        D10        10               10
  10.    SPI MOSI    MOSI         11 / ICSP-4   51        D11        ICSP-4           16
  11.    SPI MISO    MISO         12 / ICSP-1   50        D12        ICSP-1           14
  12.    SPI SCK     SCK          13 / ICSP-3   52        D13        ICSP-3           15
  13.  
  14.    Pin for LCD 5110:
  15.    pin 22 - Serial clock out (SCLK)
  16.    pin 23 - Serial data out (DIN)
  17.    pin 24 - Data/Command select (D/C)
  18.    pin 25 - LCD chip select (CS)
  19.    pin 26 - LCD reset (RST)
  20. */
  21.  
  22. #include <SPI.h>
  23. #include <MFRC522.h>
  24. #include <Adafruit_GFX.h>
  25. #include <Adafruit_PCD8544.h>
  26.  
  27. #define SS_PIN 53
  28. #define RST_PIN 5
  29.  
  30. #define BEEP 6
  31. #define BUTTON 7
  32.  
  33. MFRC522 rfid(SS_PIN, RST_PIN);
  34. MFRC522::MIFARE_Key key;
  35. Adafruit_PCD8544 display = Adafruit_PCD8544(22, 23, 24, 25, 26);
  36.  
  37. bool resetMode = false;
  38.  
  39. void setup() {
  40.   Serial.begin(9600);
  41.   SPI.begin(); // Init SPI bus
  42.   rfid.PCD_Init(); // Init MFRC522
  43.  
  44.   for (byte i = 0; i < 6; i++) {
  45.     key.keyByte[i] = 0xFF; // Key used is FF FF FF FF FF FF
  46.   }
  47.  
  48.   // Init LCD
  49.   display.begin();
  50.   display.setContrast(50);
  51.   display.setTextSize(1);
  52.   display.setTextColor(BLACK);
  53.   display.setRotation(2); // using it upside down
  54.   display.setCursor(0, 0);
  55.   display.clearDisplay();
  56.   display.display();
  57.  
  58.   pinMode(BEEP, OUTPUT);
  59.   pinMode(BUTTON, INPUT);
  60.   display.println("Press the bu-tton to reset the counter ");
  61.   display.display();
  62. }
  63.  
  64. void loop() {
  65.  
  66.   if ( digitalRead(BUTTON) == 1) {
  67.     //reset mode
  68.     display.clearDisplay();
  69.     display.println("Swipe the ca-rd to reset  the counter ");
  70.     display.display();
  71.     resetMode = true;
  72.     return;
  73.   }
  74.  
  75.   // Look for new cards
  76.   if ( ! rfid.PICC_IsNewCardPresent())
  77.     return;
  78.  
  79.   // Verify if the UID has been readed
  80.   if ( ! rfid.PICC_ReadCardSerial())
  81.     return;
  82.  
  83.   // Check for compatibility
  84.   MFRC522::PICC_Type piccType = rfid.PICC_GetType(rfid.uid.sak);
  85.   if (    piccType != MFRC522::PICC_TYPE_MIFARE_MINI
  86.           &&  piccType != MFRC522::PICC_TYPE_MIFARE_1K
  87.           &&  piccType != MFRC522::PICC_TYPE_MIFARE_4K) {
  88.     Serial.println(F("This only works with MIFARE Classic cards."));
  89.     return;
  90.   }
  91.  
  92.   // Use sector #1 (Block #4 - #7) to store data, to be specific Block #4
  93.   byte sector = 1;
  94.   byte blockAddr = 4;
  95.   byte trailerBlock = 7;
  96.  
  97.   // Initialize data to write
  98.   byte dataBlock[16];
  99.   for (int i = 0; i < 16; i++)
  100.     dataBlock[i] = 0x00;
  101.  
  102.   // Initialize array to store the result
  103.   byte buffer[18];
  104.   byte size = sizeof(buffer);
  105.  
  106.   // Initialize variable for storing counter
  107.   byte count = 0;
  108.  
  109.   MFRC522::StatusCode status;
  110.  
  111.   // Using key A
  112.   Serial.println(F("Authenticating using key A..."));
  113.   status = (MFRC522::StatusCode) rfid.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, trailerBlock, &key, &(rfid.uid));
  114.   if (status != MFRC522::STATUS_OK) {
  115.     Serial.print(F("PCD_Authenticate() failed: "));
  116.     Serial.println(rfid.GetStatusCodeName(status));
  117.     return;
  118.   }
  119.  
  120.   if (resetMode) {
  121.     // Reset mode
  122.     status = (MFRC522::StatusCode) rfid.MIFARE_Write(blockAddr, dataBlock, 16);
  123.     if (status != MFRC522::STATUS_OK) {
  124.       Serial.print(F("MIFARE_Write() failed: "));
  125.       Serial.println(rfid.GetStatusCodeName(status));
  126.     }
  127.   } else {
  128.     // Counter mode
  129.  
  130.     //Read the number first
  131.     status = (MFRC522::StatusCode) rfid.MIFARE_Read(blockAddr, buffer, &size);
  132.     if (status != MFRC522::STATUS_OK) {
  133.       Serial.print(F("MIFARE_Read() failed: "));
  134.       Serial.println(rfid.GetStatusCodeName(status));
  135.     }
  136.     count = buffer[15];
  137.  
  138.     //INC
  139.     count++;
  140.  
  141.     //Write the number back
  142.     dataBlock[15] = count;
  143.     status = (MFRC522::StatusCode) rfid.MIFARE_Write(blockAddr, dataBlock, 16);
  144.     if (status != MFRC522::STATUS_OK) {
  145.       Serial.print(F("MIFARE_Write() failed: "));
  146.       Serial.println(rfid.GetStatusCodeName(status));
  147.     }
  148.   }
  149.  
  150.   //Read the number again
  151.   status = (MFRC522::StatusCode) rfid.MIFARE_Read(blockAddr, buffer, &size);
  152.   if (status != MFRC522::STATUS_OK) {
  153.     Serial.print(F("MIFARE_Read() failed: "));
  154.     Serial.println(rfid.GetStatusCodeName(status));
  155.   }
  156.   count = buffer[15];
  157.  
  158.   //Turn on the buzzer
  159.   digitalWrite(BEEP, HIGH);
  160.  
  161.   // Print the data to serial
  162.   SerialPrintHex(rfid.uid.uidByte, rfid.uid.size);
  163.   Serial.print(" ");
  164.   Serial.println(count);
  165.  
  166.   // Print the data to LCD
  167.   display.clearDisplay();
  168.   display.println("Press the bu-tton to reset the counter ");
  169.   display.print("UID: ");
  170.   lcdPrintHex(rfid.uid.uidByte, rfid.uid.size);
  171.   display.println();
  172.   display.print("Counter: ");
  173.   display.println(count);
  174.   display.display();
  175.  
  176.   //Turn off the buzzer
  177.   digitalWrite(BEEP, LOW);
  178.  
  179.   //Beep twice if is in reset mode
  180.   if (resetMode) {
  181.     delay(50);
  182.     digitalWrite(BEEP, HIGH);
  183.     delay(100);
  184.     digitalWrite(BEEP, LOW);
  185.     resetMode = false;
  186.   }
  187.   // Halt PICC
  188.   rfid.PICC_HaltA();
  189.   rfid.PCD_StopCrypto1();
  190. }
  191.  
  192. // Function for print byte array in hex
  193. void SerialPrintHex(byte *buffer, byte bufferSize) {
  194.   for (byte i = 0; i < bufferSize; i++) {
  195.     Serial.print(buffer[i] < 0x10 ? "0" : "");
  196.     Serial.print(buffer[i], HEX);
  197.   }
  198. }
  199.  
  200. void lcdPrintHex(byte *buffer, byte bufferSize) {
  201.   for (byte i = 0; i < bufferSize; i++) {
  202.     display.print(buffer[i] < 0x10 ? "0" : "");
  203.     display.print(buffer[i], HEX);
  204.     display.display();
  205.   }
  206. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement