RFID Reader Lift & Learn

Hi,
I am making a project of Lift & Learn in which , I use 4 RFID RC 522 reader to read the card.
The scope of this project is to get a trigger when the card is lifted or placed and I'm getting Keyboard triggered from this.
But I'm facing an issue when the loop begins it reads the card and missed some time or this is the random card.
Here is my testing Code.

#include <Keyboard.h>


#include"rfid1.h"

RFID1 rfid;

uchar serNum[5];  // array to store your ID
struct Products{
  volatile bool product1;
  volatile bool product2;
  volatile bool product3;
  volatile bool product4;
};

Products products={false,false,false,false};
struct Flag{
  volatile bool P1;
  volatile bool P2;
  volatile bool P3;
  volatile bool P4;
};
Flag flag={false,false,false,false};

bool LSP1= false;
bool LSP2= false;
bool LSP3= false;
bool LSP4= false;
void checkRFID(int i){
  if(i==0){
     // rfid.begin(irq,sck,mosi,miso,nss,rst)
    rfid.begin(2,4,5,8,3,6);
  }
  if(i==1){
    rfid.begin(2,4,5,9,3,6);
  }
  if(i==2){
    rfid.begin(2,4,5,10,3,6);
  }
  if(i==3){
    rfid.begin(2,4,5,11,3,6);
  }

  delay(100);
  rfid.init();
  uchar status;
  uchar str[MAX_LEN];
  // Search card, return card types
  status = rfid.request(PICC_REQIDL, str);
  if (status != MI_OK)
  {
    return;
  }

  status = rfid.anticoll(str);
  if (status == MI_OK)
  {
    memcpy(serNum, str, 5);
  }
  String rfid_uid = "";
if (status == MI_OK) {
    for (int i = 0; i < 5; i++) {
        String uid_part = String(serNum[i], HEX); 
        //xSerial.print(uid_part);
        rfid_uid += uid_part;
    }
}
 Serial.println(rfid_uid);
  if(rfid_uid.equals("a5dae0ca55"))
  {
   //Serial.print("product1");
    products.product1=true;
    flag.P1=true;
  }

    if(rfid_uid.equals("b551e8cbc7"))
  {
    //Serial.println("product2");
    products.product2=true;
    flag.P2=true;
  }
    if(rfid_uid.equals("d5cc59cb8b"))
  {
    //Serial.print("product3");
    products.product3=true;
    flag.P3=true;
  }
    if(rfid_uid.equals("9525c1addc"))
  {
    //Serial.print("product4");
    products.product4=true;
    flag.P4=true;
  }
  
  //delay(10);
  //rfid.halt(); //command the card into sleep mode 
}

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  Keyboard.begin();
}
void checkLIFT(){
if(flag.P1!=LSP1){
  if(products.product1==false){
    //Serial.println("LIFT");
    Keyboard.write('a');
    delay(10);//5 seconds sleep
    }
}
if(flag.P1 != LSP1){
  if(products.product1==true){
    //Serial.println("NO Lift");
    Keyboard.write('x');
    
  }
}

LSP1=flag.P1;
//////////////////////////////////////////

if(flag.P2!=LSP2){
  if(products.product2==false){
    //Serial.println("LIFT 2");
    Keyboard.write('b');
    //delay(1000);//5 seconds sleep
    }
}
if(flag.P2 != LSP2){
  if(products.product2==true){
    //Serial.println("NO Lift 2");
    Keyboard.write('x');
    
  }
}

LSP2=flag.P2;
////////////////////////////////// 
if(flag.P3!=LSP3){
  if(products.product3==false){
    Serial.println("LIFT 3");
    Keyboard.write('c');
    //delay(1000);//5 seconds sleep
    }
}
if(flag.P3 != LSP3){
  if(products.product3==true){
    Serial.println("NO Lift 3");
    Keyboard.write('x');
    
  }
}

LSP3=flag.P3;
///////////////////////////////////////////
if(flag.P4!=LSP4){
  if(products.product4==false){
    //Serial.println("LIFT 4");
    Keyboard.write('d');
    //delay(1000);//5 seconds sleep
    }
}
if(flag.P4 != LSP4){
  if(products.product4==true){
    Serial.println("NO Lift 4");
    Keyboard.write('x');
    
  }
}

LSP4=flag.P4;
  
}
void loop()
{
checkRFID(0);
delay(5);
checkRFID(1);
delay(5);
checkRFID(2);
delay(5);
checkRFID(3);
delay(5);
checkLIFT();

products={false,false,false,false};
flag={false,false,false,false};
}

And this is the serial monitor result.

.

Can you provide a link to this library? It doesn't look like a standard RFID library.

What board are you using?

Also, a picture of output text is useless. Cut and paste the actual text using code tags just like your sketch.

You should also just use the uid as a long int not as a String. Your code to create the hex string will have an issue if any byte is less than 0x10 since it won't insert a leading '0' so the String will not be 10 bytes long.

Hi,
I am using this library from this session
[https://arduino.stackexchange.com/questions/18313/issue-sharing-miso-with-multiple-rc522-rfid-readers]
the link for the library is
[https://www.sunfounder.com/learn/RFID_kit_V1_for_Arduino/lesson-29-rfid-entrance-guard-system-rfid-kit-v1-0-for-arduino.html]

And using Arduino Leonardo.

Issue I'm facing is it reads one card more than one time like


9525c1addc
a5dae0ca55


a5dae0ca55

b551e8cbc7

a5dae0ca55
b551e8cbc7
b551e8cbc7
b551e8cbc7

b551e8cbc7
b551e8cbc7
b551e8cbc7
a5dae0ca55

b551e8cbc7
b551e8cbc7
a5dae0ca55

b551e8cbc7
b551e8cbc7
a5dae0ca55
b551e8cbc7


it works proper when there is 1 or 2 cards but when I put 3 or 4 then it reads 1 card again and miss the other one.

How far apart are your readers? With the output you generate, you can't tell if one chip is being read by 2 different readers or if the same reader is reading a chip multiple times.

It appears you have commented out many of the delay() calls which will slow down loop(). It may be executing so quickly that you do actually read a chip multiple times. You could add some logic to such that if you read a chip after already reading that chip, it doesn't set you flags. Only if no chip was detected do you reset your flag.

#include <Keyboard.h>


#include"rfid1.h"

RFID1 rfid;

uchar serNum[5];  // array to store your ID
struct Products {
  volatile bool product1;
  volatile bool product2;
  volatile bool product3;
  volatile bool product4;
};

Products products = {false, false, false, false};
struct Flag {
  volatile bool P1;
  volatile bool P2;
  volatile bool P3;
  volatile bool P4;
};
Flag flag = {false, false, false, false};

bool LSP1 = false;
bool LSP2 = false;
bool LSP3 = false;
bool LSP4 = false;

void checkRFID(int i) {
  if (i == 0) {
    // rfid.begin(irq,sck,mosi,miso,nss,rst)
    rfid.begin(2, 4, 5, 8, 3, 6);
  }
  if (i == 1) {
    rfid.begin(2, 4, 5, 9, 3, 6);
  }
  if (i == 2) {
    rfid.begin(2, 4, 5, 10, 3, 6);
  }
  if (i == 3) {
    rfid.begin(2, 4, 5, 11, 3, 6);
  }

  delay(100);
  rfid.init();
  uchar status;
  uchar str[MAX_LEN];
  // Search card, return card types
  status = rfid.request(PICC_REQIDL, str);
  if (status != MI_OK)
  {
    return;
  }

  status = rfid.anticoll(str);
  if (status == MI_OK)
  {
    memcpy(serNum, str, 5);
  }
  String rfid_uid = "";
  String uid_part;
  
  if (status == MI_OK) {
    for (int i = 0; i < 5; i++) {
      uid_part = String(0x0F & (serNum[i]>>4), HEX);
      rfid_uid += uid_part;
      uid_part = String(0x0F & serNum[i], HEX);
      rfid_uid += uid_part;
    }
  }
  Serial.print("Reader ");
  Serial.print(i);
  Serial.print(" found ");
  Serial.println(rfid_uid);
  if (rfid_uid.equals("a5dae0ca55"))
  {
    //Serial.print("product1");
    products.product1 = true;
    flag.P1 = true;
  }

  if (rfid_uid.equals("b551e8cbc7"))
  {
    //Serial.println("product2");
    products.product2 = true;
    flag.P2 = true;
  }
  if (rfid_uid.equals("d5cc59cb8b"))
  {
    //Serial.print("product3");
    products.product3 = true;
    flag.P3 = true;
  }
  if (rfid_uid.equals("9525c1addc"))
  {
    //Serial.print("product4");
    products.product4 = true;
    flag.P4 = true;
  }

  //delay(10);
  //rfid.halt(); //command the card into sleep mode
}

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  Keyboard.begin();
}
void checkLIFT() {
  if (flag.P1 != LSP1) {
    if (products.product1 == false) {
      //Serial.println("LIFT");
      Keyboard.write('a');
      delay(10);//5 seconds sleep
    }
  }
  if (flag.P1 != LSP1) {
    if (products.product1 == true) {
      //Serial.println("NO Lift");
      Keyboard.write('x');

    }
  }

  LSP1 = flag.P1;
  //////////////////////////////////////////

  if (flag.P2 != LSP2) {
    if (products.product2 == false) {
      //Serial.println("LIFT 2");
      Keyboard.write('b');
      //delay(1000);//5 seconds sleep
    }
  }
  if (flag.P2 != LSP2) {
    if (products.product2 == true) {
      //Serial.println("NO Lift 2");
      Keyboard.write('x');

    }
  }

  LSP2 = flag.P2;
  //////////////////////////////////
  if (flag.P3 != LSP3) {
    if (products.product3 == false) {
      Serial.println("LIFT 3");
      Keyboard.write('c');
      //delay(1000);//5 seconds sleep
    }
  }
  if (flag.P3 != LSP3) {
    if (products.product3 == true) {
      Serial.println("NO Lift 3");
      Keyboard.write('x');

    }
  }

  LSP3 = flag.P3;
  ///////////////////////////////////////////
  if (flag.P4 != LSP4) {
    if (products.product4 == false) {
      //Serial.println("LIFT 4");
      Keyboard.write('d');
      //delay(1000);//5 seconds sleep
    }
  }
  if (flag.P4 != LSP4) {
    if (products.product4 == true) {
      Serial.println("NO Lift 4");
      Keyboard.write('x');

    }
  }

  LSP4 = flag.P4;

}
void loop()
{
  checkRFID(0);
  delay(5);
  checkRFID(1);
  delay(5);
  checkRFID(2);
  delay(5);
  checkRFID(3);
  delay(5);
  checkLIFT();

  products = {false, false, false, false};
  flag = {false, false, false, false};
}

Thanks for the help it really helps out. The problem is that the readers reads the same chip due to small distance between readers.

Thanks.

That was readers are designed to do - read any chip within their field. What exactly are you trying to do? Detect a specific tag with a certain reader? If you have 2 tags that are both close to the reader, only 1 of them will be read.

Its get resolved and thanks for your support its really appreciative.
I am reading one specific card with one reader the problem was that I put the readers close to each other which cause the repeated readings.
Once again Thanks.

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