How to get the time when Rfid leave the reader using RTC

Hello guys, I'm currently working on an RFID project, I have an RTC module, I need to get the times when the RFID MFRC522 detects that a tag is present, and the time when I get the tag of the reader.

show sketch

This one for the RTC

#include <ThreeWire.h>  
#include <RtcDS1302.h>

ThreeWire myWire(27,26,25); // IO, SCLK, CE
RtcDS1302<ThreeWire> Rtc(myWire);
String currentTime;
void setup () 
{
    Serial.begin(9600);
    Rtc.Begin();
}

void loop () 
{
    RtcDateTime now = Rtc.GetDateTime();
    printDateTime(now);
    Serial.println();

    if (!now.IsValid())
    {
        // Common Causes:
        //    1) the battery on the device is low or even missing and the power line was disconnected
        Serial.println("RTC lost confidence in the DateTime!");
    }

    delay(2000); // ten seconds
}

#define countof(a) (sizeof(a) / sizeof(a[0]))

void printDateTime(const RtcDateTime& dt)
{
    char datestring[20];
    snprintf_P(datestring, 
            countof(datestring),
            PSTR("%02u:%02u:%02u"),
            dt.Hour(),
            dt.Minute(),
            dt.Second() );
    Serial.print(datestring);
}

and this for reading the tag

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

#define RST_PIN         22           // Configurable, see typical pin layout above
#define SS_PIN          5          // Configurable, see typical pin layout above

MFRC522 mfrc522(SS_PIN, RST_PIN);   // Create MFRC522 instance
MFRC522::StatusCode status;
MFRC522::MIFARE_Key key;

byte buffer1[18];
byte buffer2[18];
byte buffer3[18];
String getTaskTypeID;
//byte len = sizeof(buffer);
byte len = 34;

//*****************************************************************************************//
void setup() {
  Serial.begin(9600);                                           // Initialize serial communications with the PC
  SPI.begin();                                                  // Init SPI bus
  mfrc522.PCD_Init();                                           // Init MFRC522 card
}

void loop() {
  readTask();
}

 void readTask(){
//*****************************************************************************************//
  // Prepare key - all keys are set to FFFFFFFFFFFFh at chip delivery from the factory.
  for (byte i = 0; i < 6; i++) key.keyByte[i] = 0xFF;

  //-------------------------------------------

  // Reset the loop if no new card present on the sensor/reader. This saves the entire process when idle.
  if ( ! mfrc522.PICC_IsNewCardPresent()) {
    return;
  }

  // Select one of the cards
  if ( ! mfrc522.PICC_ReadCardSerial()) {
    return;
  }


  Serial.println(F("**Card Detected:**"));

  //------------------------------------------- GET TASK ID
  Serial.print(F("Task ID: "));
  readBlock(buffer1,4);
  //getTaskId = String((char*)buffer1);
  //Serial.println(getTaskId);
  //---------------------------------------- GET TASK TYPE ID
  Serial.print(F("\nTask Type ID: "));
  readBlock(buffer2,5);
  getTaskTypeID = String((char*)buffer2);
  //Serial.println(getTaskName);
  //----------------------------------------
  //---------------------------------------- GET TASK DATA: NB TOURS
  Serial.print(F("\nTask Delai: "));
  readBlock(buffer3,6);
  //getTaskDelai = String((char*)buffer3);
  //Serial.println(getTaskDelai);
  //----------------------------------------
  Serial.println(F("\n**End Reading**\n"));
  delay(1000); //change value if you want to read cards faster
  
  mfrc522.PICC_HaltA();
  mfrc522.PCD_StopCrypto1();
}



void readBlock(byte *buffer,byte block){
  status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, block, &key, &(mfrc522.uid)); //line 834 of MFRC522.cpp file
  if (status != MFRC522::STATUS_OK) {
    Serial.print(F("Authentication failed: "));
    Serial.println(mfrc522.GetStatusCodeName(status));
    return;
  }

  status = mfrc522.MIFARE_Read(block, buffer, &len);
  if (status != MFRC522::STATUS_OK) {
    Serial.print(F("Reading failed: "));
    Serial.println(mfrc522.GetStatusCodeName(status));
    return;
  }

  //PRINT ...
  for (uint8_t i = 0; i < 16; i++){
      Serial.write(buffer[i]);      
  
  }
}

So you have 2 pieces of code that someone else has written, and your assignment is to combine them?

I got this code from the library, I'm trying to combine them and get the time when I put the tag on the reader and the time the tag leave the reader

So you've tried?... great! Please post your attempt.

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