0

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);

    }


}
1

What you can do is store the current time whenever a card is swiped, and then when another card is swiped check how long it's been since the last card was swiped

first create a couple of variables

unsigned long swipedTime;
int tag1 = 0;
int tag2 = 0;
int swipeTimeOut = 10000; // how much time in MS you have to swipe both cards

Next we'll jump down to where where you read the cards.

      if (mfrc522.uid.uidByte[0] == 0xA4 && 
           mfrc522.uid.uidByte[1] == 0xB8 &&
           mfrc522.uid.uidByte[2] == 0xB8 &&
           mfrc522.uid.uidByte[3] == 0x96) {

               ........

               if (tag2 == 1 && millis() - swipedTime < swipeTimeOut ){
                   // both tags have been swiped, print serial message here
                }

                tag1 = 1;
                swipedTime = millis();     
        }

        if (mfrc522.uid.uidByte[0] == 0x03 && 
           mfrc522.uid.uidByte[1] == 0xFD &&
           mfrc522.uid.uidByte[2] == 0xBC &&
           mfrc522.uid.uidByte[3] == 0x02) {

               ........


               if (tag1 == 1 && millis() - swipedTime < swipeTimeOut ){
                   // both tags have been swiped, print serial message here
                }
                tag2 = 1;
                swipedTime = millis();     


         }

You'll also want to come up with a system for resetting tag1 and tag2 to 0 or "checking them out".

0

You could try:

byte tag1[] = {0xA4, 0xB8, 0xB8, 0x96}; 
byte tag1_read = 0;    // flag is set when tag 1 is read
byte tag2[] = {0x3, 0xFD, 0xBC, 0x02}; 
byte tag2_read = 0;    // flag is set when tag 2 is read

int BUTTON = 3;

void setup(){
    pinMode(BUTTON, INPUT_PULLUP);  //set button as input pulled high
    .... // the rest of your setup()
} 

void loop() {

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

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

    if (tag1_read == 0 && memcmp(mfrc522.uid.uidByte, tag1, 4) == 0){
          Serial.println("Tag 1 has been read.");
          tag1_read = 1;   //tag 1 has been read
          //delay(2000);   //not sure why u need the delay
    }

    if (tag1_read && tag2_read == 0 && memcmp(mfrc522.uid.uidByte, tag2, 4) == 0) {
          Serial.println("Tag 2 has been read.");
          tag2_read = 1;    // tag 2 has been read
          Serial.println("Both tags have been read.");
    }

    if (digitalRead(BUTTON) == LOW && tag1_read && tag2_read){
        Serial.println("Both tags are finished.");
        tag1_read = 0; tag2_read = 0;
    }
}

This code assumes the cards must be presented and read in order.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.