RFID removed tag x2, need guidance

The code i have here works, if, i place a tag on reader 0 OR 1, then remove it. It tells me if i have placed a tag on or taken it off.
Now how do i go about placing a tag on reader 0, then a tag on reader 1, then get notified when i take the tag off of reader 0? I seem to have got the loop stuck somewhere without breaking free. Could anyone offer me some guidance please.

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

constexpr uint8_t RST_PIN = 9;     // Configurable, see typical pin layout above
constexpr uint8_t SS_1_PIN = 10;   // Configurable, take a unused pin, only HIGH/LOW required, must be diffrent to SS 2
constexpr uint8_t SS_2_PIN = 8;    // Configurable, take a unused pin, only HIGH/LOW required, must be diffrent to SS 1

constexpr uint8_t NR_OF_READERS = 2;

byte ssPins[] = {SS_1_PIN, SS_2_PIN};

MFRC522 mfrc522[NR_OF_READERS];   // Create MFRC522 instance.

//*****************************************************************************************//
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

  for (uint8_t reader = 0; reader < NR_OF_READERS; reader++) {
    mfrc522[reader].PCD_Init(ssPins[reader], RST_PIN); // Init each MFRC522 card
    Serial.print(F("Reader "));
    Serial.print(reader);
    Serial.print(F(": "));
    mfrc522[reader].PCD_DumpVersionToSerial();

  }
}
uint8_t control = 0x00;


//*****************************************************************************************//



void loop() {

  for (uint8_t reader = 0; reader < NR_OF_READERS; reader++) {
    // Look for new cards
    if (mfrc522[reader].PICC_IsNewCardPresent() && mfrc522[reader].PICC_ReadCardSerial()) {
      Serial.println(reader);
      if (reader == 1) {
        Serial.println("ONE");
      }
      if (reader == 0) {
        Serial.println("ZERO");

      }

      bool result = true;
      uint8_t buf_len = 4;

      Serial.println("NewCard ");

      while (true) {
        control = 0;
        for (int i = 0; i < 3; i++) {
          if (!mfrc522[reader].PICC_IsNewCardPresent()) {
            if (mfrc522[reader].PICC_ReadCardSerial()) {
              //Serial.print('a');
              control |= 0x16;
            }
            if (mfrc522[reader].PICC_ReadCardSerial()) {
              //Serial.print('b');
              control |= 0x16;
            }
            //Serial.print('c');
            control += 0x1;
          }
          //Serial.print('d');
          control += 0x4;
        }

        //Serial.println(control);
        if (control == 13 || control == 14) {
          //card is still there
        } else {
          break;
        }
      }
      Serial.println("CardRemoved");
      delay(500); //change value if you want to read cards faster

      mfrc522[reader].PICC_HaltA();
      mfrc522[reader].PCD_StopCrypto1();
    }
  }
}
//*****************************************************************************************//a

Do i need a else if statement for the if, below for in the loop?

Rearranged code still with no luck.

Regards

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

constexpr uint8_t RST_PIN = 9;     // Configurable, see typical pin layout above
constexpr uint8_t SS_1_PIN = 10;   // Configurable, take a unused pin, only HIGH/LOW required, must be diffrent to SS 2
constexpr uint8_t SS_2_PIN = 8;    // Configurable, take a unused pin, only HIGH/LOW required, must be diffrent to SS 1

constexpr uint8_t NR_OF_READERS = 2;

byte ssPins[] = {SS_1_PIN, SS_2_PIN};

MFRC522 mfrc522[NR_OF_READERS];   // Create MFRC522 instance.

//*****************************************************************************************//
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

  for (uint8_t reader = 0; reader < NR_OF_READERS; reader++) {
    mfrc522[reader].PCD_Init(ssPins[reader], RST_PIN); // Init each MFRC522 card
    Serial.print(F("Reader "));
    Serial.print(reader);
    Serial.print(F(": "));
    mfrc522[reader].PCD_DumpVersionToSerial();

  }
}
uint8_t control = 0x00;


//*****************************************************************************************//



void loop() {

  for (uint8_t reader = 0; reader < NR_OF_READERS; reader++) {
    // Look for new cards
    if (mfrc522[reader].PICC_IsNewCardPresent() && mfrc522[reader].PICC_ReadCardSerial()) {
      Serial.println(reader); {


        if (reader == 0) {

          Serial.println("NewCard ");

          while (1) {
            control = 0;
            for (int i = 0; i < 3; i++) {
              if (!mfrc522[reader].PICC_IsNewCardPresent()) {
                if (mfrc522[reader].PICC_ReadCardSerial()) {
                  //Serial.print('a');
                  control |= 0x16;
                }
                if (mfrc522[reader].PICC_ReadCardSerial()) {
                  //Serial.print('b');
                  control |= 0x16;
                }
                //Serial.print('c');
                control += 0x1;
              }
              //Serial.print('d');
              control += 0x4;
            }

            //Serial.println(control);
            if (control == 13 || control == 14) {
              //card is still there
            } else {
              break;
            }

          }
          Serial.println("CardRemoved");
          delay(500); //change value if you want to read cards faster

        }
        else if (reader == 1) {


          Serial.println("NewCard ");

          while (1) {
            control = 0;
            for (int i = 0; i < 3; i++) {
              if (!mfrc522[reader].PICC_IsNewCardPresent()) {
                if (mfrc522[reader].PICC_ReadCardSerial()) {
                  //Serial.print('a');
                  control |= 0x16;
                }
                if (mfrc522[reader].PICC_ReadCardSerial()) {
                  //Serial.print('b');
                  control |= 0x16;
                }
                //Serial.print('c');
                control += 0x1;
              }
              //Serial.print('d');
              control += 0x4;
            }

            //Serial.println(control);
            if (control == 13 || control == 14) {
              //card is still there
            } else {
              break;
            }
          }
          Serial.println("CardRemoved");
          delay(500); //change value if you want to read cards faster


        }

        mfrc522[reader].PICC_HaltA();
        mfrc522[reader].PCD_StopCrypto1();
      }
    }
  }
}


//*****************************************************************************************//a

Why do you have a for loop to iterate NR_OF_READERS times when you do something completely different on each iteration?

Why are there no useful comments in the code? You REALLY should explain the infinite loops.

That code will never do what you want it to because each time it enters one of those “while true” loops it totally ignores the other reader (and anything else).

Each time through loop(), for each reader, you need to determine whether there is a new card present. If there is, print a message and record that a card is present.

If there is not, check whether there was a card on it last time through loop(). If so and the card is not still there print a message and record that a card is no longer present.

I do not have that library and so cannot compile code. There are no comments in your code and I don’t know why values of 13 or 14 mean that a card is still present. The code below might give you an ideas how to better structure your code to pay attention to more than one reader.

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

constexpr uint8_t RST_PIN = 9;     // Configurable, see typical pin layout above
constexpr uint8_t SS_1_PIN = 10;   // Configurable, take a unused pin, only HIGH/LOW required, must be diffrent to SS 2
constexpr uint8_t SS_2_PIN = 8;    // Configurable, take a unused pin, only HIGH/LOW required, must be diffrent to SS 1

constexpr uint8_t NR_OF_READERS = 2;

byte ssPins[] = {SS_1_PIN, SS_2_PIN};



MFRC522 mfrc522[NR_OF_READERS];   // Create MFRC522 instance.

bool cardPresent[NR_OF_READERS];   // true => card is present on reader

//*****************************************************************************************//
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

  for (uint8_t reader = 0; reader < NR_OF_READERS; reader++) {
    mfrc522[reader].PCD_Init(ssPins[reader], RST_PIN); // Init each MFRC522 card
    Serial.print(F("Reader "));
    Serial.print(reader);
    Serial.print(F(": "));
    mfrc522[reader].PCD_DumpVersionToSerial();

    // init card present flag
    cardPresent[reader] = false;

  }
}
uint8_t control = 0x00;


//*****************************************************************************************//



void loop() {

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

    // Look for new card on this reader
    bool newCard = mfrc522[reader].PICC_IsNewCardPresent() && mfrc522[reader].PICC_ReadCardSerial();

    // if a new card is present
    if ( newCard )
    {

      // mark this reader as having a card present
      cardPresent[reader] == true;

      Serial.println(reader);
      Serial.println("NewCard ");

    } else if ( cardPresent[reader] ) {

      // there was a card on the reader, is it still there?
      if ( !checkReader(reader) )
      {
        // no, print message and mark as not present
        Serial.print(reader);
        Serial.println("  CardRemoved");
        delay(500); //change value if you want to read cards faster

        // clear card present flag
        cardPresent[reader] = false;

      } // if

    } // else

    mfrc522[reader].PICC_HaltA();
    mfrc522[reader].PCD_StopCrypto1();
  }

}

// Return true if there is a card on the passed reader
//
bool checkReader(int reader)
{

  control = 0;
  for (int i = 0; i < 3; i++) {
    if (!mfrc522[reader].PICC_IsNewCardPresent()) {
      if (mfrc522[reader].PICC_ReadCardSerial()) {
        //Serial.print('a');
        control |= 0x16;
      }
      if (mfrc522[reader].PICC_ReadCardSerial()) {
        //Serial.print('b');
        control |= 0x16;
      }
      //Serial.print('c');
      control += 0x1;
    }
    //Serial.print('d');
    control += 0x4;
  }

  //Serial.println(control);
  return (control == 13 || control == 14);
  //card is still there

}

PaulS:
Why do you have a for loop to iterate NR_OF_READERS times when you do something completely different on each iteration?

Why are there no useful comments in the code? You REALLY should explain the infinite loops.

Because without it i would get this message

'reader' was not declared in this scope

And the code has been combined, the bit about the card/tag being absent, the guy who wrote it had no idea why it worker either, see below. And thank you Paul.

Blue Eyes, I was thinking to myself last night whilst learning a little more about the while code that maybe a do while might have been better. You have but things into perspective with the loops, i will have a look at the code you have wrote.

And like i said, the chap who wrote the code about being absent, said he didn’t know why it worked. So, hey for all i know it might be his lucky numbers lol But on a serious note, and a big thank you for replying and yes i know, if he hasn’t, and i haven’t then how will anybody else know what 13 and 14 mean, gotta hope hey ^^

oh the code for the card missing, was only meant for use with one reader, so me being a prized dick that i am thought i would have a go at combining the two… and i took out some bits that didn’t affect the running of the code for one reader, see below below

I have no idea why, but this works:

/*
 * Initial Author: ryand1011 (https://github.com/ryand1011)
 *
 * Reads data written by a program such as "rfid_write_personal_data.ino"
 *
 * See: https://github.com/miguelbalboa/rfid/tree/master/examples/rfid_write_personal_data
 *
 * Uses MIFARE RFID card using RFID-RC522 reader
 * Uses MFRC522 - Library
 * -----------------------------------------------------------------------------------------
 *             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      SDA(SS)      10            53        D10        10               10
 * 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>

#define RST_PIN         9           // Configurable, see typical pin layout above
#define SS_PIN          10          // Configurable, see typical pin layout above

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

//*****************************************************************************************//
void setup() {
  Serial.begin(9600);                                           // Initialize serial communications with the PC
  SPI.begin();                                                  // Init SPI bus
  mfrc522.PCD_Init();                                              // Init MFRC522 card
}
void PrintHex(uint8_t *data, uint8_t length) // prints 8-bit data in hex with leading zeroes
{
     char tmp[16];
       for (int i=0; i<length; i++) { 
         sprintf(tmp, "0x%.2X",data[i]); 
         Serial.print(tmp); Serial.print(" ");
       }
}
uint8_t buf[10]= {};
MFRC522::Uid id;
MFRC522::Uid id2;
bool is_card_present = false;
//*****************************************************************************************//

void cpid(MFRC522::Uid *id){
  memset(id, 0, sizeof(MFRC522::Uid));
  memcpy(id->uidByte, mfrc522.uid.uidByte, mfrc522.uid.size);
  id->size = mfrc522.uid.size;
  id->sak = mfrc522.uid.sak;
}

bool cmpid(MFRC522::Uid *id1, MFRC522::Uid *id2){
  return memcmp(id1, id2, sizeof(MFRC522::Uid));
}

void deregister_card(){
  is_card_present = false;
  memset(&id,0, sizeof(id));
}
uint8_t control = 0x00;
void loop() {

  MFRC522::MIFARE_Key key;
  for (byte i = 0; i < 6; i++) key.keyByte[i] = 0xFF;
  MFRC522::StatusCode status;

  //-------------------------------------------

  // Look for new cards
  if ( !mfrc522.PICC_IsNewCardPresent()) {
    return;
  }
  if ( !mfrc522.PICC_ReadCardSerial()) {
    return;
  }
  //PrintHex(id.uidByte, id.size);
  //Serial.println("hello");
  bool result = true;
  uint8_t buf_len=4;
  cpid(&id);
  Serial.print("NewCard ");
  qPrintHex(id.uidByte, id.size);
  Serial.println("");
  while(true){
    control=0;
    for(int i=0; i<3; i++){
      if(!mfrc522.PICC_IsNewCardPresent()){
        if(mfrc522.PICC_ReadCardSerial()){
          //Serial.print('a');
          control |= 0x16;
        }
        if(mfrc522.PICC_ReadCardSerial()){
          //Serial.print('b');
          control |= 0x16;
        }
        //Serial.print('c');
          control += 0x1;
      }
      //Serial.print('d');
      control += 0x4;
    }
    
    //Serial.println(control);
    if(control == 13 || control == 14){
      //card is still there
    } else {
      break;
    }
  }
  Serial.println("CardRemoved");
  delay(500); //change value if you want to read cards faster

  mfrc522.PICC_HaltA();
  mfrc522.PCD_StopCrypto1();
}
//*****************************************************************************************//a
`

The code after i took out some bits that didn’t really effect much.

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

#define RST_PIN         9           // Configurable, see typical pin layout above
#define SS_PIN          10          // Configurable, see typical pin layout above

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

//*****************************************************************************************//
void setup() {
  Serial.begin(9600);                                           // Initialize serial communications with the PC
  SPI.begin();                                                  // Init SPI bus
  mfrc522.PCD_Init();                                              // Init MFRC522 card
}
uint8_t control = 0x00;

//*****************************************************************************************//



void loop() {

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

  bool result = true;
  uint8_t buf_len = 4;

  Serial.println("NewCard ");
  
  while (true) {
    control = 0;
    for (int i = 0; i < 3; i++) {
      if (!mfrc522.PICC_IsNewCardPresent()) {
        if (mfrc522.PICC_ReadCardSerial()) {
          //Serial.print('a');
          control |= 0x16;
        }
        if (mfrc522.PICC_ReadCardSerial()) {
          //Serial.print('b');
          control |= 0x16;
        }
        //Serial.print('c');
        control += 0x1;
      }
      //Serial.print('d');
      control += 0x4;
    }

    //Serial.println(control);
    if (control == 13 || control == 14) {
      //card is still there
    } else {
      break;
    }
  }
  Serial.println("CardRemoved");
  delay(500); //change value if you want to read cards faster

  mfrc522.PICC_HaltA();
  mfrc522.PCD_StopCrypto1();
}
//*****************************************************************************************//a

Because without it i would get this message Code: [Select] 'reader' was not declared in this scope

Then your approach is to cripple your code because you can’t be bothered tracking down the real cause of the error?

A error message is only related to syntax, not function. Do not compromise function just because you don’t understand the syntax.

In this case if you remove that iteration loop you do not need the variable reader at all, so it shouldn’t be in the code.

Grumpy_Mike: Then your approach is to cripple your code because you can’t be bothered tracking down the real cause of the error?

A error message is only related to syntax, not function. Do not compromise function just because you don’t understand the syntax.

In this case if you remove that iteration loop you do not need the variable reader at all, so it shouldn’t be in the code.

Not because i can't be bothered mike, i have tried, but it only seemed to be happy with that in there rather then not. I'm new to coding so what might be the obvious to yourself may not be for me. But thank you for your reply.

Grumpy_Mike: In this case if you remove that iteration loop you do not need the variable reader at all, so it shouldn’t be in the code.

When you say the iteration loop, are you meaning the 'for' loop, and by 'you do not need the variable reader at all' you mean, maybe i should have declared this at the start, because anything that has reader in i.e (mfrc522[reader].PICC_ReadCardSerial()) wants reader to be understood, and without it, i get errors too.

In reply #2’ PaulS said

Why do you have a for loop to iterate NR_OF_READERS times when you do something completely different on each iteration?

The reader variable is the loop variable, so anything like mfrc522[reader].PICC_ReadCardSerial() Can have reader replaced by the appropriate number like mfrc522[0].PICC_ReadCardSerial() or mfrc522[1].PICC_ReadCardSerial()

Also stuff like the extra set of brackets that line came in, is not necessary, the fact you say you see it in examples should be a warning that the person writing such an example doesn’t know what they are talking about.

Ok got you Mike, monkey see monkey do i guess. I really don’t know enough to question someones code, i will work through it trying to understand as much as possible, and if it works then it seems good. I know bad practise right, but i only ever try to learn code once a year for something then hardly need it again. Maybe i should look at it like a marathon and train up to it rather then on the day!! Thank you

but i only ever try to learn code once a year for something then hardly need it again.

That makes it tricky. I code all the time but if I take a year off one language then I am very rusty when I go back to it.