RFID Write Questions

Hey there,

I am trying to write a code that will write data to various blocks in an rfid card. I will admit that I have mostly had AI write this code for me. It does everything I want but after writing to a card it freezes. Aside from doing a hard reset of the arduino is there a way to put the switch case back into an idle state and check for another new card?

Thanks in advance!

#include <SPI.h>
#include <MFRC522.h>

#define SS_PIN 10
#define RST_PIN 9

MFRC522 mfrc522(SS_PIN, RST_PIN);

MFRC522::MIFARE_Key keyA; // Key for authentication using Key A
MFRC522::MIFARE_Key keyB; // Key for authentication using Key B

byte blockNumber9 = 9;   // Block number to write SName
byte blockNumber10 = 10; // Block number to write Gname
byte blockNumber13 = 13; // Block number to write data
byte blockNumber30 = 30; // Block number for PIN Enable
byte blockNumber33 = 33; // Block number for PIN Value

char SName[16] = "New"; // Data to be written to Block 9
char GName[16] = "User"; // Data to be written to Block 10
byte dataBlock13[16] = {0x13, 0x13, 0x13, 0x13, 0x13, 0x13, 0x13, 0x13, 0x13, 0x13, 0x13, 0x13, 0x13, 0, 0, 0};
byte dataBlock30[16] = {0}; // Data to be written to Block 30 (Initial value: 0)
char dataBlock33[16] = "0123456789"; // Data to be written to Block 33

enum State {
  IDLE,
  DETECT_CARD,
  WRITE_AND_READ,
};

State currentState = IDLE;

void setup() {
  Serial.begin(9600);
  SPI.begin();
  mfrc522.PCD_Init();
  Serial.println("starting");

  // Set the authentication keys (for both Key A and Key B)
  for (byte i = 0; i < 6; i++) {
    keyA.keyByte[i] = 0xFF; // Set keyA to 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF
    keyB.keyByte[i] = 0xA0; // Set keyB to 0xA0 0xA1 0xA2 0xA3 0xA4 0xA5
  }
}

void loop() {
  switch (currentState) {
    case IDLE:
      // Look for new cards
      if (mfrc522.PICC_IsNewCardPresent()) {
        if (mfrc522.PICC_ReadCardSerial()) {
          Serial.println("RFID card detected...");
          currentState = WRITE_AND_READ;
        }
      }
      break;

    case WRITE_AND_READ:
      // Write "New" to Block 9
      byte dataBlock9[16];
      memcpy(dataBlock9, SName, 16);
      writeAndVerifyData(blockNumber9, dataBlock9, keyA);

      // Write "User" to Block 10
      byte dataBlock10[16];
      memcpy(dataBlock10, GName, 16);
      writeAndVerifyData(blockNumber10, dataBlock10, keyA);

      // Write 13 to Block 13
      writeAndVerifyData(blockNumber13, dataBlock13, keyA);

      // Write data to Block 30 (PIN Enable)
      writeAndVerifyData(blockNumber30, dataBlock30, keyA);

      // Write data to Block 33 (PIN Value)
      writeAndVerifyData(blockNumber33, (byte*)dataBlock33, keyA);

      // Read data from Block 9
      readData(blockNumber9);

      // Read data from Block 10
      readData(blockNumber10);

      // Read data from Block 13
      readData(blockNumber13);

      // Read data from Block 30
      readData(blockNumber30);

      // Read data from Block 33
      readData(blockNumber33);

      currentState = IDLE;
      Serial.println(currentState);
      Serial.println("Success!");
      break;
  }
}

bool writeAndVerifyData(byte blockNumber, byte data[], MFRC522::MIFARE_Key key) {
  // Function to write data to a block and verify if the write was successful

  // Authenticating the block using Key A
  if (mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, blockNumber, &key, &(mfrc522.uid)) != MFRC522::STATUS_OK) {
    Serial.println("Authentication with Key A failed.");
    return false;
  }

  // Writing data to the block
  if (mfrc522.MIFARE_Write(blockNumber, data, 16) == MFRC522::STATUS_OK) {
    Serial.println("Data write and verification successful!");
    return true;
  } else {
    Serial.println("Data write or verification failed!");
    return false;
  }
}

void readData(byte blockNumber) {
  // Function to read and display data from a block

  byte readbackData[18];
  byte bufferSize = sizeof(readbackData);

  // Authenticating the block using Key A
  if (mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, blockNumber, &keyA, &(mfrc522.uid)) == MFRC522::STATUS_OK) {
    // Reading data from the block
    if (mfrc522.MIFARE_Read(blockNumber, readbackData, &bufferSize) == MFRC522::STATUS_OK) {
      Serial.print("Data read from Block ");
      Serial.print(blockNumber);
      Serial.print(": ");
      if (blockNumber == blockNumber9 || blockNumber == blockNumber10) {
        for (byte i = 0; i < bufferSize; i++) {
          char ch = (char)readbackData[i];
          Serial.print(ch);
        }
      } else if (blockNumber == blockNumber13) {
        // Serial.print("Block 13 Data: ");
        for (byte i = 0; i < bufferSize; i++) {
          if (readbackData[i] == 0x13) {
            Serial.print("13 ");
          }
        }
      } else if (blockNumber == blockNumber30) {
        Serial.print("(PIN Enable) ");
        if (readbackData[0] == 0) {
          Serial.print("Disabled");
        } else if (readbackData[0] == 1) {
          Serial.print("Enabled");
        } else {
          Serial.print("Unknown Value");
        }
      } else if (blockNumber == blockNumber33) {
        Serial.print("(PIN Value) ");
        for (byte i = 0; i < bufferSize; i++) {
          char ch = (char)readbackData[i];
          Serial.print(ch);
        }
      }
      Serial.println();
    } else {
      Serial.println("Data read failed!");
    }
  } else {
    Serial.println("Authentication failed: Timeout in communication.");
  }
}

Add some serial.Print() statements at that point to show you that the code actually got to that point and then add more statements identifying anything else that is done. That way you can see exactly how far the code got and discover the logic error.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.