Arduino: HELP: How to perform action if two rfid tag UIDs have been detected?

I'm using an rfid-rc522 and I want to print to the serial a string of text if two rfid tags have been read in a given time period.

Essentially this:
1. First tag is read (print to serial: Tag 1 has been read)
2. Wait 2 seconds
3. Second tag is read (print to serial: Tag 2 has been read)
4. Print to serial: Tag 1 and Tag 2 has been read)
5. Bonus: Keep this state - and then when a button is pressed (print to serial: Tag 1 and Tag 2 are finished)


I know how to read the UID and print to the serial if one is read. Thank you so much for your help, it's greatly appreciated.

Here is my code so far:

#include <Adafruit_GFX.h>    // Core graphics library
#include <Adafruit_ST7735.h> // Hardware-specific library
#include <SPI.h>
#include <MFRC522.h>

// For the breakout, you can use any 2 or 3 pins
// These pins will also work for the 1.8" TFT shield
#define sclk 4  // SainSmart: SCL
#define mosi 5  // SainSmart: SDA
#define cs   6  // SainSmart: CS
#define dc   7  // SainSmart: RS/DC
#define rst  8  // SainSmart: RES

Adafruit_ST7735 tft = Adafruit_ST7735(cs, dc, mosi, sclk, rst);

#define SS_PIN 10
#define RST_PIN 9
MFRC522 mfrc522(SS_PIN, RST_PIN);


float p = 3.1415926;

void setup(void) {
  Serial.begin(9600);
  SPI.begin();
  Serial.print("Starting cornhole leaderboard...");
  mfrc522.PCD_Init(); // Init MFRC522 card
  Serial.println("Scan PICC to see UID and type...");
  // Use this initializer if you're using a 1.8" TFT
  tft.initR(INITR_BLACKTAB);   // initialize a ST7735S chip, black tab

  // Use this initializer (uncomment) if you're using a 1.44" TFT
  //tft.initR(INITR_144GREENTAB);   // initialize a ST7735S chip, black tab

  Serial.println("Initialized");

  uint16_t time = millis();
  tft.fillScreen(ST7735_BLACK);
  time = millis() - time;

  Serial.println(time, DEC);

  // large block of text
  tft.fillScreen(ST7735_BLACK);
}

void loop() {

  // Look for new cards
    if ( ! mfrc522.PICC_IsNewCardPresent()) {
        return;
    }

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

    if (mfrc522.uid.uidByte[0] == 0xA4 &&
       mfrc522.uid.uidByte[1] == 0xB8 &&
       mfrc522.uid.uidByte[2] == 0xB8 &&
       mfrc522.uid.uidByte[3] == 0x96) {
          Serial.println("Nate has checked in to the game.");
          tft.fillScreen(ST7735_BLUE);
          tft.setTextSize(2);
          tft.setCursor(0, 0);
          tft.setTextColor(ST7735_YELLOW);
          tft.print("Nate ");
          tft.setTextColor(ST7735_WHITE);
          tft.println("has checked in ");
          tft.setTextSize(2);
          tft.println("to the game.");
          delay(2000);
          tft.fillScreen(ST7735_BLACK);
    }

    if (mfrc522.uid.uidByte[0] == 0x03 &&
       mfrc522.uid.uidByte[1] == 0xFD &&
       mfrc522.uid.uidByte[2] == 0xBC &&
       mfrc522.uid.uidByte[3] == 0x02) {
          Serial.println("Ted has checked in to the game.");
          tft.fillScreen(ST7735_RED);
          tft.setTextSize(2);
          tft.setCursor(0, 0);
          tft.setTextColor(ST7735_CYAN);
          tft.print("Ted ");
          tft.setTextColor(ST7735_WHITE);
          tft.println("has checked in ");
          tft.setTextSize(2);
          tft.println("to the game.");
          delay(2000);
          tft.fillScreen(ST7735_BLACK);

    }


}




Thank you!

gmoon1 year ago

I know nothing about your RFID reader, but I can comment about your code:

-- The current state isn't saved anywhere. There are no state variables. Or a counter for the RFID card order (counter variables are a common way to keep track of the state).

-- A TRUE return value for .PICC_IsNewCardPresent() must indicate it's time to call .PICC_ReadCardSerial(). But the return values for either function are never tested. The RFID variable mfrc522 has no state, so it's order, timing, etc. is unknown.

-- The following code snippets are identical, since the return values aren't used:

// Look for new cards
if ( ! mfrc522.PICC_IsNewCardPresent()) {
return;
}

Is the same as:

mfrc522.PICC_IsNewCardPresent();

-- Using "return;" inside those functions isn't helpful, 'cause it always returns the same value (I forget: default return is 0, right?) regardless of the RFID functions that are nested within.

gmoon gmoon1 year ago

Oops, I should have wrote: the return values for either function are tested, but no actions are taken. Rather than "never tested."

There are conditional statements at play here ( if (!) ) on the function return values, of course...

verence1 year ago

So you know the steps to do, you have some code, but you left out the most important thing: What is your problem?