miguelbalboa / rfid

Arduino RFID Library for MFRC522
The Unlicense
2.73k stars 1.42k forks source link

Card Avaliable Function #606

Closed Endorfin35 closed 11 months ago

Endorfin35 commented 11 months ago

I needed an algorithm to figure out if the card was avaliable . I've gone through the past discussions and honestly I couldn't understand what the solution was. I am attaching the solution I found below. it seems to be working fine. I just want to share.

Relevant Code:

byte ReadAttemp=0;
bool CardState=false;

void loop()
{

    if (Card_Available())
    {
        // do it something...
    }

}

bool Card_Available()
{
  ReadAttemp=0;  
  while (1)
  {
      ReadAttemp++;  // +1 oldu, +2 oldu ise kart yok...
      if (ReadAttemp>2)
      {
          mfrc522.PICC_HaltA();
          mfrc522.PCD_StopCrypto1();

          if (ReadAttemp>3)
          {
            //Serial.println("NO CARD !");
            //digitalWrite(LED,LOW);
            CardState= false;
            break;
          }  
      } 

      if (mfrc522.PICC_IsNewCardPresent() and mfrc522.PICC_ReadCardSerial()) 
      {
        //Serial.println("CARD !");
        ReadAttemp=0; 
        //digitalWrite(LED,HIGH);
        CardState = true;  //return true... any function can work.
        break;
      }

  }
  return CardState;
}