RFID Reader Code (how to Add more Readers in the Code)

Hello Everyone, sorry for my stupid question, but I get a RFID code Written by Daniel Smith and I just can’t figure out how to add more readers and make the code clean.

If any one could help me, I really appreciate that!
Follow the code:

/*
 * HID RFID Reader Wiegand Interface for Arduino Uno
 * Written by Daniel Smith, 2012.01.30
 * www.pagemac.com
 *
 * This program will decode the wiegand data from a HID RFID Reader (or, theoretically,
 * any other device that outputs weigand data).
 * The Wiegand interface has two data lines, DATA0 and DATA1.  These lines are normall held
 * high at 5V.  When a 0 is sent, DATA0 drops to 0V for a few us.  When a 1 is sent, DATA1 drops
 * to 0V for a few us.  There is usually a few ms between the pulses.
 *
 * Your reader should have at least 4 connections (some readers have more).  Connect the Red wire 
 * to 5V.  Connect the black to ground.  Connect the green wire (DATA0) to Digital Pin 2 (INT0).  
 * Connect the white wire (DATA1) to Digital Pin 3 (INT1).  That's it!
 *
 * Operation is simple - each of the data lines are connected to hardware interrupt lines.  When
 * one drops low, an interrupt routine is called and some bits are flipped.  After some time of
 * of not receiving any bits, the Arduino will decode the data.  I've only added the 26 bit and
 * 35 bit formats, but you can easily add more.

*/


#define MAX_BITS 100                 // max number of bits 
#define WEIGAND_WAIT_TIME  3000      // time to wait for another weigand pulse.  

unsigned char databits[MAX_BITS];    // stores all of the data bits
unsigned char bitCount;              // number of bits currently captured
unsigned char flagDone;              // goes low when data is currently being captured
unsigned int weigand_counter;        // countdown until we assume there are no more bits

unsigned long facilityCode=0;        // decoded facility code
unsigned long cardCode=0;            // decoded card code

// interrupt that happens when INTO goes low (0 bit)
void ISR_INT0()
{
  //Serial.print("0");
  bitCount++;
  flagDone = 0;
  weigand_counter = WEIGAND_WAIT_TIME;  
  
}

// interrupt that happens when INT1 goes low (1 bit)
void ISR_INT1()
{
  //Serial.print("1");
  databits[bitCount] = 1;
  bitCount++;
  flagDone = 0;
  weigand_counter = WEIGAND_WAIT_TIME;  
}

void setup()
{
  pinMode(13, OUTPUT);    // LED
  pinMode(2, INPUT);      // DATA0 (INT0)
  pinMode(3, INPUT);      // DATA1 (INT1)
  
  Serial.begin(9600);
  Serial.println("RFID Readers");
  
  // binds the ISR functions to the falling edge of INTO and INT1
  attachInterrupt(0, ISR_INT0, FALLING);  
  attachInterrupt(1, ISR_INT1, FALLING);
  

  weigand_counter = WEIGAND_WAIT_TIME;
}

void loop()
{
  // This waits to make sure that there have been no more data pulses before processing data
  if (!flagDone) {
    if (--weigand_counter == 0)
      flagDone = 1;	
  }
  
  // if we have bits and we the weigand counter went out
  if (bitCount > 0 && flagDone) {
    unsigned char i;
    
    Serial.print("Read ");
    Serial.print(bitCount);
    Serial.print(" bits. ");
    
    // we will decode the bits differently depending on how many bits we have
    // see www.pagemac.com/azure/data_formats.php for mor info
    if (bitCount == 35)
    {
      // 35 bit HID Corporate 1000 format
      // facility code = bits 2 to 14
      for (i=2; i<14; i++)
      {
         facilityCode <<=1;
         facilityCode |= databits[i];
      }
      
      // card code = bits 15 to 34
      for (i=14; i<34; i++)
      {
         cardCode <<=1;
         cardCode |= databits[i];
      }
      
      printBits();
    }
    else if (bitCount == 26)
    {
      // standard 26 bit format
      // facility code = bits 2 to 9
      for (i=1; i<9; i++)
      {
         facilityCode <<=1;
         facilityCode |= databits[i];
      }
      
      // card code = bits 10 to 23
      for (i=9; i<25; i++)
      {
         cardCode <<=1;
         cardCode |= databits[i];
      }
      
      printBits();  
    }
    else {
      // you can add other formats if you want!
     Serial.println("Unable to decode."); 
    }

     // cleanup and get ready for the next card
     bitCount = 0;
     facilityCode = 0;
     cardCode = 0;
     for (i=0; i<MAX_BITS; i++) 
     {
       databits[i] = 0;
     }
  }
}

void printBits()
{
      // I really hope you can figure out what this function does
      Serial.print("FC = ");
      Serial.print(facilityCode);
      Serial.print(", CC = ");
      Serial.println(cardCode); 
}

That code is for a Wiegand reader, the way he dose it you can not expand the number of readers because there are not enough direct interrupt pins on the Arduino.

So you have to use the pin change interrupt which is a bit more complicated. This project has three Wiegand readers connected to it, look at the code and see how it is done. http://www.thebox.myzen.co.uk/Hardware/Crazy_People.html

That code is using external interrupts to read the data. The 328-based Arduinos only have 2 external interrupts, limiting the number of devices that need to use to 2 interrupts to 1.

The Mega has 5 external interrupts, allowing you to use 2 devices that need to use two interrupts.

Or, you could get serial RFID readers and use 4 of them on a Mega or 2 of them on a 328-based Arduino, using SoftwareSerial to read one of them.

Grumpy_Mike / PaulS,

Thank you for your repply! I'll try to understand that code from Crazy_People and use some code on my application!

Thanks!

Hi Grumpy_Mike,

Just a question: On that code thay I posted, it's working perfect with Weigand26 reader. I see on your code that you alse suse a Weigand26 reader!

As I come to my home, I'll perform a test with your code! Just taking advantage, how many Weigand26 readers it's possible to connect to Arduino UNO and Arduino Mega ? And it's possible to "decode" the Card data like the other code I posted using your code as sample ?

I'm using this reader: http://dx.com/p/mini-door-access-card-reader-black-116855 and this one: http://dx.com/p/contactless-smart-id-card-reader-black-143889

Thanks in Advance!

how many Weigand26 readers it's possible to connect to Arduino UNO and Arduino Mega

Well it uses the pin change interrupt so number of readers = number of free pins / 2

And it's possible to "decode" the Card data like the other code I posted using your code as sample ?

I don't know, I never read the code past the Weagand comment and I knew what problems you were going to have. But Weagand is Weagand so there should be no incompatibility it is just a matter of gathering up the right bytes.

PaulS / Grumpy_Mike

Also I found this: http://rcarduino.blogspot.com.br/2012/03/need-more-interrupts-to-read-more.html

Also I found this

Yes that is the same technique I used in my project.

Grumpy_Mike:

Also I found this

Yes that is the same technique I used in my project.

Yeah! I see that when I take a look inside that code! Also I read about, this technique have no "cost" to the hardware related to performance!

I'm new on Arduino, and I'm enjoyng the max about the possibilities that we have on hands! I'll try to make my project works with base on your project, and if I have any doubt, I'll post back or maybe I open a new Related Topic!

Thank you all for help!

My best Regards!