I want to write multiple block of nfc tag using following code but failed


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


#define SS_PIN 10  
#define RST_PIN 7 

MFRC522 mfrc522(SS_PIN, RST_PIN);
MFRC522::MIFARE_Key key;          

int blockNum = 2;  
int blockNum1 = 3;  
int blockNum2 = 4;  
int blockNum3 = 5;  
int blockNum4 = 6;  
int blockNum5 = 7;  
int blockNum6 = 8;  

byte blockData [16] = {"Rack ID 01 "};
byte blockData1 [16] = {"Rack Type grow "};
byte blockData2 [16] = {"Sowing Date 1-22 "};
byte blockData3 [16] = {"HarvestDate 6-22  "};
byte blockData4 [16] = {" Rack Incharge "};
byte blockData5 [16] = {"S220206422"};
byte blockData6 [16] = {"Remarks "};


 

  
 


byte bufferLen = 18;
byte readBlockData[18];
byte readBlockData1[18];
byte readBlockData2[18];
byte readBlockData3[18];
byte readBlockData4[18];
byte readBlockData5[18];
byte readBlockData6[18];


MFRC522::StatusCode status;

void setup() 
{
  Serial.begin(9600);
  SPI.begin();
  mfrc522.PCD_Init();
  delay(500);
  Serial.println("Scan to write data...");
}

void loop()
{
 
  for (byte i = 0; i < 6; i++)
  {
    key.keyByte[i] = 0xFF;
  }

  if ( ! mfrc522.PICC_IsNewCardPresent())
  {
    return;
  }
  

  if ( ! mfrc522.PICC_ReadCardSerial()) 
  {
    return;
  }
  Serial.println("Card Detected");
  Serial.print(F("Card UID:"));
  for (byte i = 0; i < mfrc522.uid.size; i++)
  {
    Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
    Serial.print(mfrc522.uid.uidByte[i], HEX);
  }
  Serial.print("\n");
  Serial.print(F("PICC type: "));
  MFRC522::PICC_Type piccType = mfrc522.PICC_GetType(mfrc522.uid.sak);
  Serial.println(mfrc522.PICC_GetTypeName(piccType));
         
   Serial.print("\n");
   Serial.println("Writing to Data ");
   WriteDataToBlock(blockNum, blockData);
     delay(500);

   WriteDataToBlock(blockNum1, blockData1);
     delay(500);

   WriteDataToBlock(blockNum2, blockData2);
     delay(500);

   WriteDataToBlock(blockNum3, blockData3);
     delay(500);

   WriteDataToBlock(blockNum4, blockData4);
     delay(500);

   WriteDataToBlock(blockNum5, blockData5);
     delay(500);

   WriteDataToBlock(blockNum6, blockData6);
     delay(500);

   
   Serial.print("\n");
   Serial.println("Reading from Data Block...");
   ReadDataFromBlock(blockNum, readBlockData);
     delay(500);

   ReadDataFromBlock(blockNum1, readBlockData1);
     delay(500);

   ReadDataFromBlock(blockNum2, readBlockData2);
     delay(500);

   ReadDataFromBlock(blockNum3, readBlockData3);
     delay(500);

   ReadDataFromBlock(blockNum4, readBlockData4);
     delay(500);

   ReadDataFromBlock(blockNum5, readBlockData5);
     delay(500);

   ReadDataFromBlock(blockNum6, readBlockData6);
     delay(500);


   
 Serial.print("Data Find:");
   for (int j=0 ; j<16 ; j++)
   {
     Serial.write(readBlockData[j]);
	 Serial.write(readBlockData1[j]);
	 Serial.write(readBlockData2[j]);
	 Serial.write(readBlockData3[j]);
	 Serial.write(readBlockData4[j]);
	 Serial.write(readBlockData5[j]);
	 Serial.write(readBlockData6[j]);
   }
   Serial.print("\n");
}


void ReadDataFromBlock(int blockNum, byte readBlockData[]) 
{
  byte status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, blockNum, &key, &(mfrc522.uid));

  if (status != MFRC522::STATUS_OK)
  {
     Serial.print("Reading failed:  ");
     Serial.println(mfrc522.GetStatusCodeName(status));
     return;
  }
  else
  {
    Serial.println("Authentication success");
  }

  status = mfrc522.MIFARE_Read(blockNum, readBlockData, &bufferLen);
  if (status != MFRC522::STATUS_OK)
  {
    Serial.print("Reading failed: ");
    Serial.println(mfrc522.GetStatusCodeName(status));
    return;
  }
  else
  {
    Serial.println("Read successfully");  
  }
  
}

void WriteDataToBlock(int blockNum, byte blockData[]) 
{
  status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, blockNum, &key, &(mfrc522.uid));
  if (status != MFRC522::STATUS_OK)
  {
    Serial.print("Writing  failed");
    Serial.println(mfrc522.GetStatusCodeName(status));
    return;
  }
  else
  {
    Serial.println(" ");
  }

  
  /* Write data to the block */
  status = mfrc522.MIFARE_Write(blockNum, blockData, 16);
  delay(2000);
  if (status != MFRC522::STATUS_OK)
  {
    Serial.print("Writing  failed: ");
    Serial.println(mfrc522.GetStatusCodeName(status));
    return;
  }
  else
  {
    Serial.println("Data was written successfully");
  }
  
}

Welcome to the forum

byte blockData [16] = {"Rack ID 01 "};
byte blockData1 [16] = {"Rack Type grow "};
byte blockData2 [16] = {"Sowing Date 1-22 "};
byte blockData3 [16] = {"HarvestDate 6-22  "};
byte blockData4 [16] = {" Rack Incharge "};
byte blockData5 [16] = {"S220206422"};
byte blockData6 [16] = {"Remarks "};

You don't say what failed but why are these byte and not char ?

Incidentally, some the the data is too long to fit in the array anyway

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