Problem with MFRC522 and relay

Hi everyone,

Thanks for your time. I'm quite a beginner.

I want to use an electronic lock with a relay, as the good RFID tag is detected.

I seem to have electrical problem or maybe it's my sketch :slight_smile:
Sometimes it works one time, sometimes it doesn't. I don't know why !! and it's frustrating

`Texte préformaté`#include <SPI.h>
#include <MFRC522.h>
// ecrire ici le numero UID du bon badge
const byte bonUID[4] = {119, 36, 198, 61};
//
const int relais = 4; // relais
const int pinRST = 9;  // pin RST du module RC522
const int pinSDA = 10; // pin SDA du module RC522
MFRC522 rfid(pinSDA, pinRST);
void setup()
{
  SPI.begin();
  rfid.PCD_Init();
  pinMode(relais, OUTPUT);
  digitalWrite(relais, LOW);

}
void loop()
{
  int refus = 0; // quand cette variable n'est pas nulle, c'est que le code est refusé
  if (rfid.PICC_IsNewCardPresent())  // on a dédecté un tag
  {
    if (rfid.PICC_ReadCardSerial())  // on a lu avec succès son contenu
    {
      for (byte i = 0; i < rfid.uid.size; i++) // comparaison avec le bon UID
      {
        if (rfid.uid.uidByte[i] != bonUID[i]) {
          refus++;
        }
      }


  
      if (refus == 0) // UID accepté
      {
        digitalWrite(relais, HIGH);
        delay(1000);
        digitalWrite(relais, LOW);

              // Re-Init RFID
  rfid.PICC_HaltA(); // Halt PICC
  rfid.PCD_StopCrypto1(); // Stop encryption on PCD


       
      }


      }
    }
  }`Texte préformaté`

Can you help me ?

for ( uint16_t i = 0; ...

-- or --

for ( int i = 0; ...

bottom line: whatever type rfid.uid.size should be the type of i

You think this is the problem ?

Because without the lock my relay works perfectly so i m wondering

Post LOCKED
Please do NOT cross post / duplicate as it wastes peoples time and efforts to have more than one post for a single topic.

Continued cross posting could result in a time out from the forum.

Could you also take a few moments to Learn How To Use The Forum.

Other general help and troubleshooting advice can be found here.

It will help you get the best out of the forum in the future.