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.