Read RFID every time it is in proximity

It’s time to call in the experts. I have spent way too long trying to figure out why my RFID thing only reads once. I was even (gasp) considering firing off a relay to just reset the arduino after reading.

Basically I need my arduino to send the number 6 every time it detects an rfid card. Unfortunately, it only does it once and then has to be rebooted. I don’t see documentation on how to “reset” so that every card is “new” and I don’t see how to detect the presence of any rfid card. Please help!

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

#define RST_PIN   3     // Configurable, see typical pin layout above
#define SS_PIN    53   // Configurable, see typical pin layout above

MFRC522 mfrc522(SS_PIN, RST_PIN);   // Create MFRC522 instance

/* Set your new UID here! */
#define NEW_UID {0xDE, 0xAD, 0xBE, 0xEF}

MFRC522::MIFARE_Key key;

void setup() {
  Serial.begin(9600);  // Initialize serial communications with the PC

  SPI.begin();         // Init SPI bus
  mfrc522.PCD_Init();  // Init MFRC522 card
  //Serial.println(F("RFID Connected"));

  for (byte i = 0; i < 6; i++) {
    key.keyByte[i] = 0xFF;
  }
}
void loop() {

  // Look for new cards, and select one if present
  if ( ! mfrc522.PICC_IsNewCardPresent() || ! mfrc522.PICC_ReadCardSerial() ) {
    delay(50);
    return;
  }


  Serial.println("6");

  delay(2000);
}

Examine your "if" more closely.

Change it:

// Look for new cards, and select one if present if ( ! mfrc522.PICC_IsNewCardPresent() || ! mfrc522.PICC_ReadCardSerial() ) { Serial.println("my if is not what I think it is"); delay(50); return; }

Then see which part of the compound statement is causing the problem.

Paul

It looks to me like it’s saying if not new card present. Which seems the opposite to me because it only works if a new card is present.

I don’t see an option for ANY card being present. But I can’t find documentation.

Paul_KD7HB: Serial.println("my if is not what I think it is"); delay(50);

I'm sorry, Paul, I don't understand what you're saying.

I got it. Thank you. It's just been that kind of day.

mudmin:
I’m sorry, Paul, I don’t understand what you’re saying.

I am saying to divide your compound statement up and see which one is not doing what you think it is doing.

Paul

Paul_KD7HB: I am saying to divide your compound statement up and see which one is not doing what you think it is doing.

Actually, I thought I had it, but I don't.

If I put

  if ( mfrc522.PICC_IsNewCardPresent()) {
    Serial.println("6");

    delay(2000);
  }

I get a 6 once and only once.

If I put

  if ( mfrc522.PICC_ReadCardSerial() ) {
    Serial.println("6");

    delay(2000);
  }

I get nothing.

Try this

if ( ! mfrc522.PICC_IsNewCardPresent() || ! mfrc522.PICC_ReadCardSerial() ) {
    Serial.println("6");
  }

mfrc522.PICC_HaltA();
mfrc522.PCD_StopCrypto1();

I will definitely give that a shot. At the moment I'm simply rebooting the arduino with a relay. Thanks for following up!

You are a genius. That fixed it. The whole resetting the arduino led to some wonkiness. This is EXACTLY what I needed. Thank you!