Looking to chain multiple RFID readers with the same key/tag

Hi all, first time ever posting to the forum so if anyone has any advice or feedback about whether this is in the right place or anything please let me know.

I'm trying to create a system of multiple RFID readers that after each reading a single RFID tag in a certain order, unlocks a drawer. I've found multiple examples on these forums and others of people using this type of puzzle for attaching multiple tags and leaving them to unlock a puzzle (which was my first successful attempt at working with an Arduino!) but unfortunately I do not have the knowledge to understand how to adjust the code to fit this new scenario.

One such example (Thanks Annaane!), which I've included below, is a way of getting the placement type puzzle to work, but can anyone give me some advice as to how to alter this to use one tag and save each successful read? Kind of like if you were building a puzzle to trace a tag over a map in the correct order to unlock something.

Many thanks.

/**
   --------------------------------------------------------------------------------------------------------------------
   Example sketch/program showing how to read data from more than one PICC to serial.
   --------------------------------------------------------------------------------------------------------------------
   This is a MFRC522 library example; for further details and other examples see: https://github.com/miguelbalboa/rfid

   Example sketch/program showing how to read data from more than one PICC (that is: a RFID Tag or Card) using a
   MFRC522 based RFID Reader on the Arduino SPI interface.

   Warning: This may not work! Multiple devices at one SPI are difficult and cause many trouble!! Engineering skill
            and knowledge are required!

   @license Released into the public domain.

   Typical pin layout used:
   -----------------------------------------------------------------------------------------
               MFRC522      Arduino       Arduino   Arduino    Arduino          Arduino
               Reader/PCD   Uno/101       Mega      Nano v3    Leonardo/Micro   Pro Micro
   Signal      Pin          Pin           Pin       Pin        Pin              Pin
   -----------------------------------------------------------------------------------------
   RST/Reset   RST          9             5         D9         RESET/ICSP-5     RST
   SPI SS 1    SDA(SS)      ** custom, take a unused pin, only HIGH/LOW required *
   SPI SS 2    SDA(SS)      ** custom, take a unused pin, only HIGH/LOW required *
   SPI MOSI    MOSI         11 / ICSP-4   51        D11        ICSP-4           16
   SPI MISO    MISO         12 / ICSP-1   50        D12        ICSP-1           14
   SPI SCK     SCK          13 / ICSP-3   52        D13        ICSP-3           15

*/

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

// PIN Numbers : RESET + SDAs
#define RST_PIN         9
#define SS_1_PIN        10
#define SS_2_PIN        8
#define SS_3_PIN        7
#define SS_4_PIN        6

// Led and Relay PINS
#define GreenLed        2
#define relayIN         3
#define RedLed          4


// List of Tags UIDs that are allowed to open the puzzle
byte tagarray[][4] = {
  {0x4B, 0x17, 0xBC, 0x79},
  {0x8A, 0x2B, 0xBC, 0x79}, 
  {0x81, 0x29, 0xBC, 0x79},
  {0xE6, 0xDF, 0xBB, 0x79},
};

// Inlocking status :
int tagcount = 0;
bool access = false;

#define NR_OF_READERS   4

byte ssPins[] = {SS_1_PIN, SS_2_PIN, SS_3_PIN, SS_4_PIN};

// Create an MFRC522 instance :
MFRC522 mfrc522[NR_OF_READERS];

/**
   Initialize.
*/
void setup() {

  Serial.begin(9600);           // Initialize serial communications with the PC
  while (!Serial);              // Do nothing if no serial port is opened (added for Arduinos based on ATMEGA32U4)

  SPI.begin();                  // Init SPI bus

  /* Initializing Inputs and Outputs */
  pinMode(GreenLed, OUTPUT);
  digitalWrite(GreenLed, LOW);
  pinMode(relayIN, OUTPUT);
  digitalWrite(relayIN, HIGH);
  pinMode(RedLed, OUTPUT);
  digitalWrite(RedLed, LOW);


  /* looking for MFRC522 readers */
  for (uint8_t reader = 0; reader < NR_OF_READERS; reader++) {
    mfrc522[reader].PCD_Init(ssPins[reader], RST_PIN);
    Serial.print(F("Reader "));
    Serial.print(reader);
    Serial.print(F(": "));
    mfrc522[reader].PCD_DumpVersionToSerial();
    //mfrc522[reader].PCD_SetAntennaGain(mfrc522[reader].RxGain_max);
  }
}

/*
   Main loop.
*/

void loop() {

  for (uint8_t reader = 0; reader < NR_OF_READERS; reader++) {

    // Looking for new cards
    if (mfrc522[reader].PICC_IsNewCardPresent() && mfrc522[reader].PICC_ReadCardSerial()) {
      Serial.print(F("Reader "));
      Serial.print(reader);

      // Show some details of the PICC (that is: the tag/card)
      Serial.print(F(": Card UID:"));
      dump_byte_array(mfrc522[reader].uid.uidByte, mfrc522[reader].uid.size);
      Serial.println();

      for (int x = 0; x < sizeof(tagarray); x++)                  // tagarray's row
      {
        for (int i = 0; i < mfrc522[reader].uid.size; i++)        //tagarray's columns
        {
          if ( mfrc522[reader].uid.uidByte[i] != tagarray[x][i])  //Comparing the UID in the buffer to the UID in the tag array.
          {
            DenyingTag();
            break;
          }
          else
          {
            if (i == mfrc522[reader].uid.size - 1)                // Test if we browesed the whole UID.
            {
              AllowTag();
            }
            else
            {
              continue;                                           // We still didn't reach the last cell/column : continue testing!
            }
          }
        }
        if (access) break;                                        // If the Tag is allowed, quit the test.
      }


      if (access)
      {
        if (tagcount == NR_OF_READERS)
        {
          OpenDoor();
        }
        else
        {
          MoreTagsNeeded();
        }
      }
      else
      {
        UnknownTag();
      }
      /*Serial.print(F("PICC type: "));
        MFRC522::PICC_Type piccType = mfrc522[reader].PICC_GetType(mfrc522[reader].uid.sak);
        Serial.println(mfrc522[reader].PICC_GetTypeName(piccType));*/
      // Halt PICC
      mfrc522[reader].PICC_HaltA();
      // Stop encryption on PCD
      mfrc522[reader].PCD_StopCrypto1();
    } //if (mfrc522[reader].PICC_IsNewC..
  } //for(uint8_t reader..
}

/**
   Helper routine to dump a byte array as hex values to Serial.
*/
void dump_byte_array(byte * buffer, byte bufferSize) {
  for (byte i = 0; i < bufferSize; i++) {
    Serial.print(buffer[i] < 0x10 ? " 0" : " ");
    Serial.print(buffer[i], HEX);
  }
}

void printTagcount() {
  Serial.print("Tag n°");
  Serial.println(tagcount);
}

void DenyingTag()
{
  tagcount = tagcount;
  access = false;
}

void AllowTag()
{
  tagcount = tagcount + 1;
  access = true;
}

void Initialize()
{
  tagcount = 0;
  access = false;
}

void OpenDoor()
{
  Serial.println("Welcome! the door is now open");
  Initialize();
  digitalWrite(relayIN, LOW);
  digitalWrite(GreenLed, HIGH);
  delay(2000);
  digitalWrite(relayIN, HIGH);
  delay(500);
  digitalWrite(GreenLed, LOW);
}

void MoreTagsNeeded()
{
  printTagcount();
  Serial.println("System needs more cards");
  digitalWrite(RedLed, HIGH);
  delay(1000);
  digitalWrite(RedLed, LOW);
  access = false;
}

void UnknownTag()
{
  Serial.println("This Tag isn't allowed!");
  printTagcount();
  for (int flash = 0; flash < 5; flash++)
  {
    digitalWrite(RedLed, HIGH);
    delay(100);
    digitalWrite(RedLed, LOW);
    delay(100);
  }
}

just to be clear

you have on tag
you have N readers
you need to place the tag in sequence over P readers (P <= N) to trigger an action

did I get that right?

do you have one arduino per reader or are all the readers attached to the same Arduino?

create an Array where you store the right order of the readers,

for example

order[] {2,3,0,1}
use a variable currentReadIndex = 0;

now check which reader reads the right card.
if it is the Reader from order[currentReadIndex] -> 2, correct, increase currenReadIndex.
check which reader reads the right card.
if it is the Reader from order[currentReadIndex] -> 3, correct, increase currentReadIndex
check which reader reads the right card.
if is is the Reader from order[currentReadIndex] -> 0, correct, increase currentReadIndex
check which reader reads the right card.
if it is is the Reader from order[currentReadIndex] -> 1, correct and solved.

if a check doesn't fit - handle the error situation (block for a specific time, give a message, reset currentReadIndex ... what ever you think is apropriate)

Sorry for the late reply, I'm trying to have the one tag trace over all the readers in a specific order to unlock a magnetic lock powered through a relay, potentially even adding in a reader that does the opposite to stop people cheesing the puzzle. Currently all the readers are connected to one UNO R3 board.

My real problem is I am very new to coding for Arduino and while I can wire the cards up to work, I cannot get the readers to individually read the tag, record the successful step, and stop reading the tag again. Currently, I can just keep placing the tag over one reader enough times until the puzzle is complete, instead of requiring each reader individually.

Hope that helps explain the situation, thanks.

I've got it to read the tag on multiple readers and when it's read a certain number of times it successfully completes the puzzle! Unfortunately, it can read the tag multiple times on the one reader, kind of like instead of the puzzle only going (reader 1 > reader 2 > reader 3 = success), it can go (reader 1 > reader 1 > reader 1 = success). Is there any way of stopping the readers from reading the tag again if they've already successfully read it? Thanks a bunch

Post the code you used for that (make sure you gave a try to the algorithm described by @noiasca )

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