Ok, just to be clear the original code I was using is the one from "Crazy People", and I get the exact same inconsistent results with it. The first bit of code I posted was the code I was using after many hours of troubleshooting.
The code from Crazy People has the internal pull up resistor enabled as specified in the code. Yet still inconsistent results....
Just to be sure, this is the exact reader I have, as far as I can tell it will output 26 bits.
http://www.ebay.com/itm/Mini-Wiegand26-Weatherproof-RFID-Reader-125KHz-/261185317132?pt=LH_DefaultDomain_0&hash=item3ccfdba90c
//WG26 Card Reader
//12v Power to red
//Ground to Black
//Green to pin 2
//Yellow to pin 3
//Reads cards and does a serial print
//*Need to add verification to allow only certain cards
/* Crazy People
* By Mike Cook April 2009
* Three RFID readers outputing 26 bit Wiegand code to pins:-
* Reader A (Head) Pins 2 & 3
* Interrupt service routine gathers Wiegand pulses (zero or one) until 26 have been recieved
* Then a sting is sent to processing
*/
int validSiteCodes[] = {144, 360, 147, 318, 316};
int validSerialNumbers[] = {11779, 14740, 2985, 5605, 3411};// see if site code and serial number are in the lists...
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);
// Attach pin change interrupt service routines from the Wiegand RFID readers
attachInterrupt(0, reader1Zero, RISING);//DATA0 to pin 2
attachInterrupt(1, reader1One, RISING); //DATA1 to pin 3
delay(10);
// the interrupt in the Atmel processor mises out the first negitave pulse as the inputs are already high,
// so this gives a pulse to each reader input line to get the interrupts working properly.
// Then clear out the reader variables.
// The readers are open collector sitting normally at a one so this is OK
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;
//digitalWrite(8, HIGH); // show Arduino has finished initilisation
pinMode(12, OUTPUT);
}
void loop()
{
if(reader1Count >=26){
//Serial.print(" Reader 1 ");
//Serial.println(reader1,HEX);
// Serial.println("A");
//Serial.println(reader1& 0xfffffff);
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(12,HIGH);
}
else
digitalWrite(12,LOW);
delay(200);
digitalWrite(12,LOW); // Open the door. It's cold out here!
}
}
boolean IsTagValid(int siteCode, int serialNumber)
{
boolean valid = false;
// Determine how many valid tags there are
int validTagCount = sizeof(validSiteCodes)/sizeof(int);
// 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;
}