# Problem with RFID Timer

**URL:** <https://forum.arduino.cc/t/problem-with-rfid-timer/1301344>\
**Category:** Deutsch\
**Created:** [September 13, 2024, 4:29pm UTC](https://forum.arduino.cc/t/problem-with-rfid-timer/1301344 "2024-09-13T16:29:47Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![nikl](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/nikl/32/1099154_2.png) [@nikl](https://forum.arduino.cc/u/nikl)\
**Post date:** [September 13, 2024, 4:29pm UTC](https://forum.arduino.cc/t/problem-with-rfid-timer/1301344/1 "2024-09-13T16:29:47Z")

</div>

I have the following problem: I am trying to build a machine in Arduino that measures the time when no RFID card is on the reader. However, I am encountering the following issue: when I place the card on the reader, the timer starts, but it should only start when the user removes the card from the reader. Can someone help me with this?

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

#define SS_PIN D4 // SDA Pin (Slave Select)
#define RST_PIN D3 // Reset Pin

MFRC522 mfrc522(SS_PIN, RST_PIN); // RFID-Instanz

unsigned long startTime;
bool timerActive = false;
bool cardDetected = false;

void setup() {
  Serial.begin(115200); // Initialisiere die serielle Kommunikation
  SPI.begin(); // Initialisiere den SPI-Bus
  mfrc522.PCD_Init(); // Initialisiere den RFID-Leser

  Serial.println("RFID-Leser bereit. Halten Sie eine Karte an den Leser.");
}

void loop() {
  bool cardPresent = mfrc522.PICC_IsNewCardPresent();
  
  if (cardPresent) {
    if (mfrc522.PICC_ReadCardSerial()) {
      // Karte wird erkannt
      if (timerActive) {
        // Timer stoppen und Zeit ausgeben
        unsigned long elapsedTime = millis() - startTime;
        Serial.print("Zeit gemessen: ");
        Serial.print(elapsedTime);
        Serial.println(" ms");
        timerActive = false; // Timer zurücksetzen
      }
      cardDetected = true; // Karte wurde erkannt
      mfrc522.PICC_HaltA(); // Halt das Leseverfahren
      mfrc522.PCD_StopCrypto1();
    }
  } else {
    // Wenn keine Karte mehr vorhanden ist
    if (cardDetected && !timerActive) {
      // Timer starten, wenn der Timer nicht aktiv ist
      startTime = millis();
      timerActive = true;
      Serial.println("Timer gestartet.");
    }
    cardDetected = false; // Karte wurde entfernt
  }
}

```

---

<div class="post-metadata">

**Author:** ![skippolino](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/skippolino/32/977765_2.png) [@skippolino](https://forum.arduino.cc/u/skippolino)\
**Post date:** [September 25, 2024, 4:50pm UTC](https://forum.arduino.cc/t/problem-with-rfid-timer/1301344/2 "2024-09-25T16:50:57Z")

</div>

Place some more Serial.println inside the ifs and elses to see where you get wrong and then paste the output on here and tell where you expeczed something else than that happened

---

<div class="post-metadata">

**Author:** ![ec2021](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ec2021/32/997272_2.png) [@ec2021](https://forum.arduino.cc/u/ec2021)\
**Post date:** [October 1, 2024, 11:11am UTC](https://forum.arduino.cc/t/problem-with-rfid-timer/1301344/3 "2024-10-01T11:11:33Z")

</div>

Hi @nikl,

BTW: Is your thread correctly placed in the German part of the Forum?

Anyway, here is a tutorial how to detect the removal of a SD card:

[https://highvoltages.co/tutorial/arduino-tutorial/arduino-mfrc522-tutorial-is-rfid-tag-present-or-removed/](https://highvoltages.co/tutorial/arduino-tutorial/arduino-mfrc522-tutorial-is-rfid-tag-present-or-removed/)

a further example

[https://arduino.stackexchange.com/questions/21619/detect-when-rfid-card-is-absent](https://arduino.stackexchange.com/questions/21619/detect-when-rfid-card-is-absent)

and here another example

[https://forum.arduino.cc/t/how-to-check-if-a-rfid-card-is-always-touching/1162187/6](https://forum.arduino.cc/t/how-to-check-if-a-rfid-card-is-always-touching/1162187/6)

If I am not wrong the function

> mfrc522.PICC\_IsNewCardPresent();

returns "true" only **once** when a new card is presented. On a second call it returns "false". It will only return "true" again after removal and a further presentation of a card.

Good luck  
ec2021

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [March 30, 2025, 11:12am UTC](https://forum.arduino.cc/t/problem-with-rfid-timer/1301344/4 "2025-03-30T11:12:09Z")

</div>

Closed on March 30, 2025, 11:12am UTC

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