MFRC522 Read, store and write

Good day

I have a a MFRC522 module connected to a Arduino nano.
I read data from x y and z blocks
store that data on my arduino, do some calculations and then i want to write that data to said blocks (overwrite)
My problem is when i get to the write portion of my code, my mfrc522.PICC_IsNewCardPressent() fails
Please can you suggest any solutions

#include <MFRC522.h>

const int pinRST = A1;
const int pinSDA = A3;

MFRC522 mfrc522(pinSDA, pinRST);
MFRC522::StatusCode status;
MFRC522::MIFARE_Key key;

void setup()
{
pinMode(pinSDA, OUTPUT);
pinMode(pinRST, OUTPUT);

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

void ReadFromRFID()
{
byte block = 4;
byte len = 34;
byte buffer1[34];
byte buffer2[34];

String SN;
String BB;

if ( ! mfrc522.PICC_IsNewCardPresent()) {
    Serial.println("No Card");
    return;
  }
 
  if ( ! mfrc522.PICC_ReadCardSerial()) {
    Serial.println("No Serial");
    return;
  }

status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, block, &key, &(mfrc522.uid));
  if (status != MFRC522::STATUS_OK) {
    Serial.print("30003");
    return;
  }
 
  status = mfrc522.MIFARE_Read(block, buffer1, &len);
  if (status != MFRC522::STATUS_OK) {
    Serial.print("30004");
    return;
  }
  for (uint8_t i = 0; i < 16; i++)
  {
    if (buffer1[i] != 32)
    {
      SN += (char)buffer1[i];
    }
  }

block = 8;
  
  status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, block, &key, &(mfrc522.uid));
  if (status != MFRC522::STATUS_OK) {
    Serial.print("30003");
    return;
  }
 
  status = mfrc522.MIFARE_Read(block, buffer2, &len);
  if (status != MFRC522::STATUS_OK) {
    Serial.print("30004");
    return;
  }
  for (uint8_t i = 0; i < 16; i++)
  {
    if (buffer2[i] != 32)
    {
      BB += (char)buffer2[i];
    }
  }
}

void Calculate()
{
//do my calculations
WriteToRFID();
}

void WriteToRFID()
{
if ( ! mfrc522.PICC_IsNewCardPresent()) {
    return;
  }
 
  if ( ! mfrc522.PICC_ReadCardSerial()) {
    return;
  }

String g = String(CalculatedValue);
byte buffer1[g.length()];
g.getBytes(buffer1, g.length()+1);
byte block;
for(byte i = g.length(); i < 34; i++) buffer1[i] = ' ';

block = 4;

  status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, block, &key, &(mfrc522.uid));
    if (status != MFRC522::STATUS_OK) {
      //Serial.print(F("PCD_Authenticate() failed: "));
      //Serial.println(mfrc522.GetStatusCodeName(status));
      Serial.println("20002");
      return;
    }
    else //Serial.println(F("PCD_Authenticate() success: "));
  
    // Write block
    status = mfrc522.MIFARE_Write(block, buffer1, 16);
    if (status != MFRC522::STATUS_OK) {
      return;
    }
    else

    block = 8;
  
    status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, block, &key, &(mfrc522.uid));
    if (status != MFRC522::STATUS_OK) {
      return;
    }
    
    status = mfrc522.MIFARE_Write(block, &buffer1[16], 16);
    if (status != MFRC522::STATUS_OK) {
      return;
    }
}

Thank You

@mr_tropica, your topic has been moved to a more suitable section of the forum.

When you get to the writing portion, you don't have a new card. You have already detected the new card and read it so you are already authenticated with it. Just do the write. Look at the examples that came with the library.

So should i remove both the

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

Or just

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

And leave the

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

There?

It is difficult to say since your sketch does not compile. There is no loop() function present. You have just shown a collection of functions but nobody but you knowns how they are called. Please post your complete sketch.

I would start with the library example that does writing and modify it do store your data.

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