arduino with wiegand reader

Hello everyone!

I have rfid reader with wiegand output and I want to open the doors with arduino. I have already connect everything together and all works fine until after a few sucessful reads and unlocking the doors the arduino "frezes" and i must to reset it manually. I don't know where is the problem but I think that is in the code. Can anyone help me, please? Here is the code which I am using.
Sorry for my bad english.

rfid_odklepanje.ino (1.83 KB)

Anyone?

Welcome to the forum!

I did not see any obvious problem with the code- does it still freeze without the relay connected? Maybe you have some problem with the power supply or wiring.

In the future you should use the code tags (</> on the toolbar) to include your code like

int validSiteCodes[] = {91, 91, 426}; //tu vnesi kodo kartice (prve številke do pike tu, ostale v spodnjo vrstico)
int validSerialNumbers[] = {4646, 10899, 12130};
const int relay = 12;

volatile long reader1 = 0;
volatile int reader1Count = 0;

void reader1One(void) {
 reader1Count++;
 reader1 = reader1 << 1;
 reader1 |= 1;
}

void reader1Zero(void) {
 reader1Count++;
 reader1 = reader1 << 1;
}

void setup()
{
 Serial.begin(9600);
 pinMode(relay, OUTPUT);
 attachInterrupt(0, reader1Zero, RISING);//DATA0 to pin 2
 attachInterrupt(1, reader1One, RISING); //DATA1 to pin 3
 delay(10);
 
 for(int i = 2; i<4; i++){
 pinMode(i, OUTPUT);
 digitalWrite(i, HIGH); // enable internal pull up causing a one
 digitalWrite(i, LOW); // disable internal pull up causing zero and thus an interrupt
 pinMode(i, INPUT);
 digitalWrite(i, HIGH); // enable internal pull up
 }
 delay(10);
 // put the reader input variables to zero
 reader1 = 0;
 reader1Count = 0;
 
}

void loop()
{
 if(reader1Count >=26){
 
 int serialNumber=(reader1 >> 1) & 0x3fff;
 int siteCode= (reader1 >> 17) & 0x3ff;
 
 Serial.print(siteCode);
 Serial.print("  ");
 Serial.println(serialNumber);
 reader1 = 0;
 reader1Count = 0;
 
 if(IsTagValid(siteCode, serialNumber)
 ){
 digitalWrite(relay,HIGH);
 Serial.println("Acess granted");
 delay (4500);
 digitalWrite(relay,LOW);
 }
 else
 digitalWrite(relay,LOW);
 Serial.println("Acess denied");
 }
}

boolean IsTagValid(int siteCode, int serialNumber)
{
 boolean valid = false;
 
 int validTagCount = sizeof(validSiteCodes)/sizeof(int); //preveri koliko znakov je v kodi
 
 // Loop through the arrays to see if siteCode
 // and serialNumber are present
 
 for(int t=0; t<validTagCount; t++)
 {
 if(validSiteCodes[t] == siteCode &&
        validSerialNumbers[t] == serialNumber)
 {
 valid = true;
 break;
 }
 }
 
 return valid;
}

Thank you for the answer.
I tried without relay it works fine. I also tried another code which was posted on the internet which unlock doors with all cards and it works fine. So I think that the problem was in this code.
I will try the code again and write back.

Today I try another option without relay and instead relay i use darlington transistor BDX53c. The problem is still the same.

With this code arduino frezes and i must reset it manualy but if I use another code which detect all cards the arduino works correctly. I really don't now what to do. Any suggestion?

change the emitter and collector of the npn transistor, your wiring is not OK.

The current comes from the AVR to switch the relais.
Tis overcurrent causes the hung...

May be, the transistor can be bad now - change it also.

Sorry. It is my mistake in drawing circuit on a paper.
Arduino still freezes.
Has anyone have working code for that?

I can't believe that nobody have or can't wrote mention program.