NFC with SD module error

I've a problem...

My project is a machine tha works like a Ticket gate with RFID cards and storage the ID, date and hour in one SD card. When I put the command "CatracaLog = SD.open ("log.txt", FILE_WRITE);" inside the block "void loop (void)" the program execute the ID only one time (if I pass the ID or another ID the program don't read it anymore). I really need help with this.

Source code:

//Placa PN532 NFC
#include <PN532.h>
#include <SPI.h>
#define PN532_CS 10
PN532 nfc(PN532_CS);
#define NFC_DEMO_DEBUG 1

//RTC
#include <Time.h>
#include <DS1307.h>
DS1307 rtc (A4, A5);

//CARTAO SD
#include <SPI.h>
#include <SD.h>
Sd2Card card;
const int chipSelect = 8;
File CatracaLog;

void setup(void) {
SD.begin (8);

#ifdef NFC_DEMO_DEBUG
Serial.begin(9600);
Serial.println("Olá");
#endif
nfc.begin();

uint32_t versiondata = nfc.getFirmwareVersion();
if (! versiondata) {
#ifdef NFC_DEMO_DEBUG
tone(5,262,200); //DO
delay(200);
tone(5,262,200); //DO
delay(200);
Serial.print("Didn't find PN53x board");
#endif
while (1); // halt
}

#ifdef NFC_DEMO_DEBUG
// Got ok data, print it out!
Serial.print("Found chip PN5");
Serial.println((versiondata>>24) & 0xFF, HEX);
Serial.print("Firmware ver. ");
Serial.print((versiondata>>16) & 0xFF, DEC);
Serial.print('.');
Serial.println((versiondata>>8) & 0xFF, DEC);
Serial.print("Supports ");
Serial.println(versiondata & 0xFF, HEX);
#endif

// configure board to read RFID tags and cards
nfc.SAMConfig();

rtc.halt(false);
//As linhas abaixo setam a data e hora do modulo
//e podem ser comentada apos a primeira utilizacao
//rtc.setDOW(SATURDAY); //Define o dia da semana
//rtc.setTime(03, 01, 0); //Define o horario
//rtc.setDate(04, 11, 2017); //Define o dia, mes e ano

//Definicoes do pino SQW/Out
rtc.setSQWRate(SQW_RATE_1);
rtc.enableSQW(true);

//Mostra as informações no Serial Monitor
Serial.print("Hora atual : ");
Serial.print(rtc.getTimeStr());
Serial.print(" ");
Serial.print("Data atual: ");
Serial.print(rtc.getDateStr(FORMAT_SHORT));
Serial.print(" ");
Serial.println(rtc.getDOWStr(FORMAT_SHORT));

}

void loop(void) {
uint32_t id;
// look for MiFare type cards
id = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A);

if (id != 0){

Serial.print("Read card #");
Serial.println(id);
Serial.println();

CatracaLog = SD.open("log.txt", FILE_WRITE);

if (CatracaLog) {
Serial.println("Hora: ");
Serial.println(rtc.getTimeStr());
Serial.println("Data:");
Serial.println(rtc.getDateStr(FORMAT_SHORT));

Serial.print("ID: #");
Serial.println(id);
Serial.println();

CatracaLog.print("ID do cartão lido: #");
CatracaLog.print(id);
CatracaLog.print(" ");

CatracaLog.print("Hora : ");
CatracaLog.print(rtc.getTimeStr());
CatracaLog.print(" ");
CatracaLog.print("Data : ");
CatracaLog.print(rtc.getDateStr(FORMAT_SHORT));
CatracaLog.println(" ");

tone(5,262,200); //DO
delay(200);

// Fechando o arquivo:
CatracaLog.close();
} else {
tone(5,262,800); //DO
delay(1000);
tone(5,262,800); //DO
delay(1000);
// if the file didn't open, print an error:
//Serial.println("Erro ao tentar abrir o arquivo log.txt para escrita");
//return;
}
}
//delay(2000);
}

Catraca_NFC_4.0.ino (2.93 KB)

Both the SD library and the PN532 library want to use SPI. The PN532 uses the old way of configuring SPI without the beginTransaction() settings stuff. I reckon, the original settings that the PN532 configures are getting changed by the use of the SD library and so the PN532 stuff fails.

Try adding a nfc.begin() in the loop() before the read card line:

void loop(void) {
 uint32_t id;
  nfc.begin();
  // look for MiFare type cards
  id = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A);