Problem writing serial RFID to SD

I am developing a stand alone RFID tag reader for an experiment. I have successfully linked a TTG0 T8 (mcu) to an RV3028 (rtc) and and RDM6300 (rfid tag reader). The code below functions and I can see both the tag id and the rtc output but I am only able to get the rtc output to write to the SD card on the mcu. I have not managed to successfully code a way to get the RFID tag number to also write to the SD. I can see the tag is being read through the serial monitor, but I am not sure how I need to amend my code to get this tag id to store on the SD. Grateful for any assistance

<#include <Melopero_RV3028.h>

#include <Wire.h>

Melopero_RV3028 rtc;

void setup() {
Serial.begin(115200);
while (!Serial);

// First initialize and create the rtc device
rtc.initDevice();

// Set the device to use the 24hour format (default) instead of the 12 hour format
rtc.set24HourMode();

// Set the date and time:
rtc.setTime(2021, 5, 3, 11, 7, 31, 0);
// year, month, weekday, date, hour, minute, second
// Note: time is always set in 24h format
// Note: month value ranges from 1 (Jan) to 12 (Dec)
// Note: date value ranges from 1 to 31

// print datetime to make sure everything works
printTime();
}

void loop() {
// print datetime every second
printTime();
delay(1000);
}

void printTime(){
Serial.print(rtc.getHour());
Serial.print(":");
Serial.print(rtc.getMinute());
Serial.print(":");
Serial.print(rtc.getSecond());
Serial.print(" ");
Serial.print(rtc.getDate());
Serial.print("/");
Serial.print(rtc.getMonth());
Serial.print("/");
Serial.println(rtc.getYear());>

Can you show which lines print the RFID tag ID or which lines do this:

Hint: Use code tags when posting code. I see a stray chevron at the end of your posted code.

Apologies - I posted the wrong code. Correct code is now posted below with code tags. The relevant print line is - Serial.println(rdm6300.get_tag_id(), DEC);

I have tried calling this print line in the SD String as follows: - int tagids = rdm6300.get_tag_id();//value occassionally displays correct tag id

but as noted in my comment - for some strange reason I occasionally get the correct tag id value appearing but it is not consistent even when I can see that it has been read from the Serial print earlier in the programme.

#include <SPI.h>
#include <mySD.h>
#include <rdm6300.h>

//code from RDM3600
#define RDM6300_RX_PIN 3
#define READ_LED_PIN 21

Rdm6300 rdm6300;

//Code from SD
File root;
const int chipSelect = 13;

//Code from RTC
#include <Melopero_RV3028.h>

#include <Wire.h>

Melopero_RV3028 rtc;



void setup()
{
  Serial.begin(115200);

  //Setting up RTC
  while (!Serial);

  // First initialize and create the rtc device
  rtc.initDevice();

  // Set the device to use the 24hour format (default) instead of the 12 hour format
  rtc.set24HourMode();

  // Set the date and time:
  rtc.setTime(2021, 5, 3, 11, 7, 31, 0);
  // year, month, weekday, date, hour, minute, second
  // Note: time is always set in 24h format
  // Note: month value ranges from 1 (Jan) to 12 (Dec)
  // Note: date value ranges from 1 to 31

  // print datetime to make sure everything works
  printTime();


  //Setting up the SD Card
  Serial.print("Initializing SD card...");
  pinMode(SS, OUTPUT);


  //SDClass::begin(uint8_t csPin, int8_t mosi, int8_t miso, int8_t sck)
  if (!SD.begin(13, 15, 2, 14)) {
    Serial.println("initialization failed!");
    return;
  }

  Serial.println("initialization done.");

  root = SD.open("/");


  Serial.println("done!");


  //RDM6300 code

  pinMode(READ_LED_PIN, OUTPUT);
  digitalWrite(READ_LED_PIN, LOW);

  rdm6300.begin(RDM6300_RX_PIN);

  Serial.println("\nPlace RFID tag near the rdm6300...");
}

void loop()
{


  //RDM6300 code

  /* if non-zero tag_id, update() returns true- a new tag is near! */
  if (rdm6300.update())
    Serial.println(rdm6300.get_tag_id(), DEC);


  digitalWrite(READ_LED_PIN, rdm6300.is_tag_near());

  delay(10);


  //new code start
  // make a string for assembling the data to log:

  String dataString = "";

  int hours = rtc.getHour();
  int minutes = rtc.getMinute();
  int seconds = rtc.getSecond();
  int date = rtc.getDate();
  int month = rtc.getMonth();
  int year = rtc.getYear();


  int tagnear = rdm6300.is_tag_near();//value = 1 when tag is near
  int tagid = rdm6300.update();//value appear to remain as zero
  int tagids = rdm6300.get_tag_id();//value occassionally displays correct tag id


  dataString += String(hours);
  dataString += ":";
  dataString += String(minutes);
  dataString += ":";
  dataString += String(seconds);
  dataString += "   ";
  dataString += String(date);
  dataString += "/";
  dataString += String(month);
  dataString += "/";
  dataString += String(year);
  dataString += "   ";
  dataString += String(tagnear);
  dataString += ",";
  dataString += String(tagid);
  dataString += ",";
  dataString += String(tagids);



  File dataFile = SD.open("datalog.txt", FILE_WRITE);

  if (dataFile) {
    dataFile.println(dataString);
    dataFile.close();
    Serial.println(dataString);
  }

  else {
    Serial.println("error opening datalog.txt");
  }

  delay(1000);
}


//new code end

void printTime() {
  Serial.print(rtc.getHour());
  Serial.print(":");
  Serial.print(rtc.getMinute());
  Serial.print(":");
  Serial.print(rtc.getSecond());
  Serial.print("   ");
  Serial.print(rtc.getDate());
  Serial.print("/");
  Serial.print(rtc.getMonth());
  Serial.print("/");
  Serial.println(rtc.getYear());
}

May be it would help using the correct type ?

Thanks - just tried amending the type to uint32_t but does not appear to have changed anything - I am not sure if you can see the image below of the serial output, but the serial print line for the tag is separate and correct, but the following line of 0,0,0 values is what is sent to the SD - changing to 1,0,0 when the tag is read. The last "0" digit should be the full tag ID.

Forgot to add that you can see the tag id 2703686 for one record but this is not consistently recorded.

Post the code

#include <SPI.h>
#include <mySD.h>
#include <rdm6300.h>

//code from RDM3600
#define RDM6300_RX_PIN 3
#define READ_LED_PIN 21

Rdm6300 rdm6300;

//Code from SD
File root;
const int chipSelect = 13;

//Code from RTC
#include <Melopero_RV3028.h>

#include <Wire.h>

Melopero_RV3028 rtc;



void setup()
{
  Serial.begin(115200);

  //Setting up RTC
  while (!Serial);

  // First initialize and create the rtc device
  rtc.initDevice();

  // Set the device to use the 24hour format (default) instead of the 12 hour format
  rtc.set24HourMode();

  // Set the date and time:
  rtc.setTime(2021, 5, 3, 11, 7, 31, 0);
  // year, month, weekday, date, hour, minute, second
  // Note: time is always set in 24h format
  // Note: month value ranges from 1 (Jan) to 12 (Dec)
  // Note: date value ranges from 1 to 31

  // print datetime to make sure everything works
  printTime();


  //Setting up the SD Card
  Serial.print("Initializing SD card...");
  pinMode(SS, OUTPUT);


  //SDClass::begin(uint8_t csPin, int8_t mosi, int8_t miso, int8_t sck)
  if (!SD.begin(13, 15, 2, 14)) {
    Serial.println("initialization failed!");
    return;
  }

  Serial.println("initialization done.");

  root = SD.open("/");


  Serial.println("done!");


  //RDM6300 code

  pinMode(READ_LED_PIN, OUTPUT);
  digitalWrite(READ_LED_PIN, LOW);

  rdm6300.begin(RDM6300_RX_PIN);

  Serial.println("\nPlace RFID tag near the rdm6300...");
}

void loop()
{


  //RDM6300 code

  /* if non-zero tag_id, update() returns true- a new tag is near! */
  if (rdm6300.update())
    Serial.println(rdm6300.get_tag_id(), DEC);


  digitalWrite(READ_LED_PIN, rdm6300.is_tag_near());

  delay(10);


  //new code start
  // make a string for assembling the data to log:

  String dataString = "";

  int hours = rtc.getHour();
  int minutes = rtc.getMinute();
  int seconds = rtc.getSecond();
  int date = rtc.getDate();
  int month = rtc.getMonth();
  int year = rtc.getYear();


  int tagnear = rdm6300.is_tag_near();//value = 1 when tag is near
  int tagid = rdm6300.update();//value appear to remain as zero
  uint32_t tagids = rdm6300.get_tag_id();//value occassionally dislays correct tag id


  dataString += String(hours);
  dataString += ":";
  dataString += String(minutes);
  dataString += ":";
  dataString += String(seconds);
  dataString += "   ";
  dataString += String(date);
  dataString += "/";
  dataString += String(month);
  dataString += "/";
  dataString += String(year);
  dataString += "   ";
  dataString += String(tagnear);
  dataString += ",";
  dataString += String(tagid);
  dataString += ",";
  dataString += String(tagids);



  File dataFile = SD.open("datalog.txt", FILE_WRITE);

  if (dataFile) {
    dataFile.println(dataString);
    dataFile.close();
    Serial.println(dataString);
  }

  else {
    Serial.println("error opening datalog.txt");
  }

  delay(1000);
}


//new code end

void printTime() {
  Serial.print(rtc.getHour());
  Serial.print(":");
  Serial.print(rtc.getMinute());
  Serial.print(":");
  Serial.print(rtc.getSecond());
  Serial.print("   ");
  Serial.print(rtc.getDate());
  Serial.print("/");
  Serial.print(rtc.getMonth());
  Serial.print("/");
  Serial.println(rtc.getYear());
}

Try storing the rdm6300.get_tag_id() once when the tag is near and not read multiple times

Thanks - tried this but does not work. I have tried reducing the RFID read frequency from 10 to 100 to see if that might resolve the issue but not go! Do you think that setting up a buffer to store and then transfer the RFID tad id might work?

Start by doing a small code handling the RFID as you want
Then add the RTC
Then add the SD card

Issue resolved by matching the duration of both delay statements - changed both to 500 and tag number records to SD perfectly.

that sounds like a perfect scientific answer to the issue you were facing...