Read RFID block and save to SD card fail

Read RFID block and save to SD card fail

Here’s my code:

/*
 * MFRC522 - Library to use ARDUINO RFID MODULE KIT 13.56 MHZ WITH TAGS SPI W AND R BY COOQROBOT.
 * The library file MFRC522.h has a wealth of useful info. Please read it.
 * The functions are documented in MFRC522.cpp.
 *
 * Based on code Dr.Leong   ( WWW.B2CQSHOP.COM )
 * Created by Miguel Balboa (circuitito.com), Jan, 2012.
 * Rewritten by Søren Thing Andersen (access.thing.dk), fall of 2013 (Translation to English, refactored, comments, anti collision, cascade levels.)
 * Released into the public domain.
 *
 * Sample program showing how to read data from a PICC using a MFRC522 reader on the Arduino SPI interface.
 *----------------------------------------------------------------------------- empty_skull
 * Aggiunti pin per arduino Mega
 * add pin configuration for arduino mega
 * http://mac86project.altervista.org/
 ----------------------------------------------------------------------------- Nicola Coppola
 * Pin layout should be as follows:
 * Signal     Pin              Pin               Pin
 *            Arduino Uno      Arduino Mega      MFRC522 board
 * ------------------------------------------------------------
 * Reset      9                5                 RST
 * SPI SS     10               53                SDA
 * SPI MOSI   11               51                MOSI
 * SPI MISO   12               50                MISO
 * SPI SCK    13               52                SCK
 *
 * The reader can be found on eBay for around 5 dollars. Search for "mf-rc522" on ebay.com.
 */

#include <SPI.h>
#include <MFRC522.h>
#include <SD.h>
#include "Wire.h"

#define SS_PIN 53
#define RST_PIN 5
#define SDPIN 4
#define DS1307_I2C_ADDRESS 0x68
MFRC522 mfrc522(SS_PIN, RST_PIN);  // Create MFRC522 instance.
MFRC522::MIFARE_Key key;
File myFile;

byte readbackblock[16];
char n;
//byte erase[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};//all zeros. This can be used to delete a block.
void setup() {
  Serial.begin(9600); // Initialize serial communications with the PC
  SPI.begin();      // Init SPI bus
pinMode(SDPIN, OUTPUT);
pinMode(SS_PIN, OUTPUT);
digitalWrite(SDPIN,HIGH);
digitalWrite(SS_PIN,LOW);

Wire.begin();  

  mfrc522.PCD_Init(); // Init MFRC522 card
  Serial.println("Text to file in SD");
 
  //type A
  for (byte i = 0; i < 6; i++) {
    key.keyByte[i] = 0xAA;
  }
  //type B
  for (byte i = 0; i < 6; i++) {
    key.keyByte[i + 10] = 0xAA;
  }

 
digitalWrite(SDPIN,LOW);
digitalWrite(SS_PIN,HIGH);
  Serial.print("Initializing SD card...");

  if (!SD.begin(4)) {
    Serial.println("initialization failed!");
    return;
  }
  Serial.println("initialization done.");

  
}

void loop() {
  // Look for new cards
  
digitalWrite(SDPIN,HIGH);
digitalWrite(SS_PIN,LOW);
  if ( ! mfrc522.PICC_IsNewCardPresent()) {
    return;
  }
 Serial.println("card present");
  // Select one of the cards
  if ( ! mfrc522.PICC_ReadCardSerial()) {
    return;
  }
   Serial.println("correct key");
  
digitalWrite(SDPIN,HIGH);
digitalWrite(SS_PIN,LOW); 
  myFile=SD.open("test.txt",FILE_WRITE);
  if (myFile) {
    Serial.print("Writing to test.txt...");
    digitalWrite(SDPIN,HIGH);
    digitalWrite(SS_PIN,LOW);
    readBlock(1,readbackblock);
    digitalWrite(SDPIN,LOW);
    digitalWrite(SS_PIN,HIGH);
    for(int i=0;i<16;i++)
    myFile.write(readbackblock[i]);
    //myFile.print("congrats!");
    // close the file:
    myFile.close();
    Serial.println("done.");
    }else {
    // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }

  myFile = SD.open("test.txt");
  if (myFile) {
    Serial.println("test.txt:");

    // read from the file until there's nothing else in it:
    while (myFile.available()) {
      Serial.write(myFile.read());
    }
    // close the file:
    myFile.close();
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }

}

I have this function.ino

https://l.facebook.com/l.php?u=https%3A%2F%2Fz-1-cdn.fbsbx.com%2Fv%2Ft59.2708-21%2F13463039_10202051070314525_1868541993_n.txt%2Fj.txt%3F_nc_eui%3D7VA9KVpicuhopRsnlFHB9Q04aYs%26oh%3D0748f76a5d57e027a66626b2a224d869%26oe%3D5762F51E%26dl%3D1&h=RAQGi2c0u

And this was the output

The arduino already detects the sd and rfid, but when it comes to reading a block from the rfid card, it errors.

Can you help me determine the problem? Thanks!

The error message is being generated in this block of code:

    Serial.print("Writing to test.txt...");
    digitalWrite(SDPIN,HIGH);
    digitalWrite(SS_PIN,LOW);
    readBlock(1,readbackblock);
    digitalWrite(SDPIN,LOW);
    digitalWrite(SS_PIN,HIGH);
    for(int i=0;i<16;i++)
        myFile.write(readbackblock[i]);
    // close the file:
    myFile.close();
    Serial.println("done.");

My guess would be the readBlock() function is detecting the error. I would look closer at that function. I’m guessing it is in one of the libraries but you have the sources for the libraries.

Yes sir, the function.ino for that readBlock's link is also posted could you check it sir? Thanks!

Yes sir, the function.ino for that readBlock's link is also posted

NO IT IS NOT.

A URL is NOT the same as posting code. If you want help from facebork, post your request for help THERE. If you want help HERE, post your code HERE.

The text file you linked to seems to be incomplete and badly formatted.

 † † 
int writeBlock(int blockNumber, byte arrayAddress[]) 
{
 †//this makes sure that we only write into s. Every 4th block is a trailer block for the access/security info.
 †int largestModulo4Number=blockNumber/4*4;
 †int trailerBlock=largestModulo4Number+3;//determine trailer block for the sector
 †//if (blockNumber > 2 && (blockNumber+1)%4 == 0){Serial.print(blockNumber);Serial.println(" is a trailer block:");return 2;}//block number is a trailer block (modulo 4); quit and send error code 2
 †Serial.print(blockNumber);
 †Serial.println(" is a :");
 †
 †/*****************************************authentication of the desired block for access***********************************************************/
 †byte status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, trailerBlock, &key, &(mfrc522.uid));
 †//byte PCD_Authenticate(byte command, byte blockAddr, MIFARE_Key *key, Uid *uid);
 †//this method is used to authenticate a certain block for writing or reading
 †//command: See enumerations above -> PICC_CMD_MF_AUTH_KEY_A = 0x60 (=1100000), // this command performs authentication with Key A
 †//blockAddr is the number of the block from 0 to 15.
 †//MIFARE_Key *key is a pointer to the MIFARE_Key struct defined above, this struct needs to be defined for each block. New cards have all A/B= FF FF FF FF FF FF
 †//Uid *uid is a pointer to the UID struct that contains the user ID of the card.
 †if (status != MFRC522::STATUS_OK) {
 † † † † Serial.print("PCD_Authenticate() failed: ");
 † † † †// Serial.println(mfrc522.GetStatusCodeName(status));
 † † † † return 3;//return "3" as error message
 †}
 †//it appears the authentication needs to be made before every block read/write within a specific sector.
 †//If a different sector is being authenticated access to the previous one is lost.


 †/*****************************************writing the block***********************************************************/
 † † † †
 †status = mfrc522.MIFARE_Write(blockNumber, arrayAddress, 16);//valueBlockA is the block number, MIFARE_Write(block number (0-15), byte array containing 16 values, number of bytes in block (=16))
 †//status = mfrc522.MIFARE_Write(9, value1Block, 16);
 †if (status != MFRC522::STATUS_OK) {
 † † † † † Serial.print("MIFARE_Write() failed: ");
 † † † † †// Serial.println(mfrc522.GetStatusCodeName(status));
 † † † † † return 4;//return "4" as error message
 †}
 †Serial.println("block was written");
}


int readBlock(int blockNumber, byte arrayAddress[]) 
{
 †int largestModulo4Number=blockNumber/4*4;
 †int trailerBlock=largestModulo4Number+3;//determine trailer block for the sector

 †/*****************************************authentication of the desired block for access***********************************************************/
 †byte status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, trailerBlock, &key, &(mfrc522.uid));
 †//byte PCD_Authenticate(byte command, byte blockAddr, MIFARE_Key *key, Uid *uid);
 †//this method is used to authenticate a certain block for writing or reading
 †//command: See enumerations above -> PICC_CMD_MF_AUTH_KEY_A = 0x60 (=1100000), // this command performs authentication with Key A
 †//blockAddr is the number of the block from 0 to 15.
 †//MIFARE_Key *key is a pointer to the MIFARE_Key struct defined above, this struct needs to be defined for each block. New cards have all A/B= FF FF FF FF FF FF
 †//Uid *uid is a pointer to the UID struct that contains the user ID of the card.
 †if (status != MFRC522::STATUS_OK) {
 † † † † Serial.print("PCD_Authenticate() failed (read): ");
 † † † † //Serial.println(mfrc522.GetStatusCodeName(status));
 † † † † return 3;//return "3" as error message
 †}
 †//it appears the authentication needs to be made before every block read/write within a specific sector.
 †//If a different sector is being authenticated access to the previous one is lost.


 †/*****************************************reading a block***********************************************************/
 † † † †
 †byte buffersize = 18;//we need to define a variable with the read buffer size, since the MIFARE_Read method below needs a pointer to the variable that contains the size... 
 †status = mfrc522.MIFARE_Read(blockNumber, arrayAddress, &buffersize);//&buffersize is a pointer to the buffersize variable; MIFARE_Read requires a pointer instead of just a number
 †if (status != MFRC522::STATUS_OK) {
 † † † † †Serial.print("MIFARE_read() failed: ");
 † † † † †//Serial.println(mfrc522.GetStatusCodeName(status));
 † † † † †return 4;//return "4" as error message
 †}
 †Serial.println("block was read");