Arduino HID Weigand Reader

For the arduino.cc googlers and because I get tired of reading the chest thumping bs, here is a working example; feel free to critique.

No interrupts and works perfectly.

/*
HID Site Code 868 with Card# 14062788
Binary String - 111011001001101011010010100110001001

First & last bit are Parity bits
1-1101100100110101101001010011000100-1

next 10 bits are site code; remaining is the Card#
1-1101100100-110101101001010011000100-1

Card# in HEX {D694C4}
1101 (13 - D)
0110 (6)
1001 (9)
0100 (4)
1100  (12-C)
0100 (4)
(4*16^0)+(12*16^1)+(4*16^2)+(9*16^3)+(6*16^4)+(13*16^5) = 14062788
*/
int bits_CARD = 36;
const int bits_SITE = 10;
const int bits_CARDid = 26;

char BINchar[36]; // = "111011001001101011010010100110001001";
char SITEchar[bits_SITE +1];
char CARDchar[bits_CARDid + 1];

bool READING;

int green = 10; //4;  //Green Wire Input (or Weigand Data0)
int white = 11; //5; //White Wire Input (or Weigand Data1)

int relay = 12;  // Pin Controlling Relay
int led = 13;

int data0;
int data1;
int bit_cnt;
const char* _rev = "Rev1";

long int lastMillis;

void setup(){
  pinMode(green, INPUT);  pinMode(white, INPUT);
  pinMode(8, OUTPUT); digitalWrite(8,HIGH);
  pinMode(led, OUTPUT);
  Serial.begin(115200);
  Serial.println("Booting...");

}


void loop(){
    data0 = digitalRead(green);  //Green Wire Input (or Weigand Data0))
    data1 = digitalRead(white);  //White Wire Input (or Weigand Data1))

   if(data0==0){BINchar[bit_cnt] = '0'; bit_cnt ++;}
   if(data1==0){BINchar[bit_cnt] = '1'; bit_cnt ++;}
   if(data1==0 || data0 == 0){ lastMillis = millis();  READING = true;}
   
   while(data0 == 0){data0 = digitalRead(green); }
   while(data1 == 0){data1 = digitalRead(white); }

   if(lastMillis + 250 < millis() && READING == true){ clearMem();}

} // End Loop


void clearMem(){
  READING = false;
  if(bit_cnt == bits_CARD){decodeBITS();}else{bit_cnt = 0; Serial.println("oops");}
  bit_cnt = 0;
}


void decodeBITS(){

    for(int i=0; i<bits_SITE; i++){
        SITEchar[i]= BINchar[i+1];
    }

    for(int i= (bits_SITE+1); i<bits_CARD-1; i++){
        CARDchar[i-(bits_SITE+1)]= BINchar[i];
    }
    SITEchar[bits_SITE] = '\0';
    CARDchar[bits_CARDid] = '\0';

    long int svalue = strtol(String(SITEchar).c_str(), NULL, 2);
    Serial.println(svalue);
  
    long int value = strtol(String(CARDchar).c_str(), NULL, 2);
    Serial.println(value);
}

Who is Weigand and what did he write?

...R

Well, cuss -lol. I don't know who Weigand is either. But, Wiegand's outer personality was said to have a high level of coercivity... 10 levels. However, at his core, he behaved differently.

"Just another card reader"

ballscrewbob:
"Just another card reader"

Like this?

...R

LOL R2

I know her! She had a sign in her window last month - "Closed due to unforeseen circumstances."

Coercivity is a measure of the ability of a permanent magnet to resist demagnetising fields.... if you gave him too much, did Wiegand flip poles?

A new circus act. I'll patent it.

Allan