HID reader not outputting value constantly

hi i'm using a ProxPoint HID reader with arduino UNO to read HID cards. The reader is powered externally by a 12V power supply and the negative is connected to the ground of UNO. When i scan the card the cardcode will be displayed in the serial monitor however the cardcode is not always displayed in the serial monitor despite the HID reader beeping which indicates the card is scanned successfully. I think there's and internal counter of 3 second, so i wait 3 second in between scans but the output is not constant as it displays only every now and then. Below is the code i'm using.

#include <Wire.h>
#include <SPI.h>

#define MAX_BITS 100                 // max number of bits 
#define WEIGAND_WAIT_TIME  3000  

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;         

void ISR_INT0() 
{
  //Serial.print("0");               // uncomment this line to display raw binary
  bitCount++;
  flagDone = 0;
  weigand_counter = WEIGAND_WAIT_TIME;  
 
}
 
// interrupt that happens when INT1 goes low (1 bit)
void ISR_INT1() 
{
  //Serial.print("1");               // uncomment this line to display raw binary
  databits[bitCount] = 1;
  bitCount++;
  flagDone = 0;
  weigand_counter = WEIGAND_WAIT_TIME;  
}

void setup() {
  /* HID */
  pinMode(2, INPUT);               // DATA0 (INT0)
  pinMode(3, INPUT);               // DATA1 (INT1)
  /* HID */ 
  
  Wire.begin(); 
  Serial.begin(9600);

  /* HID */
  attachInterrupt(0, ISR_INT0, FALLING);  
  attachInterrupt(1, ISR_INT1, FALLING);
  weigand_counter = WEIGAND_WAIT_TIME;
  /* HID */
  // put your setup code here, to run once:

}

void loop() {

  /* HID */
  // 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. ");*/
 
  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 14 to 34
      for (i=14; i<34; i++) 
      {
         cardCode <<=1;
         cardCode |= databits[i];
      }
      //print card ID
      printBits();
     //temperature reader
  }
     // cleanup and get ready for the next card
     bitCount = 0;
     facilityCode = 0;
     cardCode = 0;
     for (i=0; i<MAX_BITS; i++) 
      {
         databits[i] = 0;
      }
  }/* HID */
  // put your main code here, to run repeatedly:

}


void printBits() {

    Serial.println(cardCode);

}

You don't :frowning:

3000 loop iterations are less than 3 seconds.
That's why most probably you miss bits and fail to output a message if bitCount is not 35 when flagDone.

1 Like

And as you never reset the weigand_counter variable it doesn’t even do that after the first pass of the loop.

1 Like

weigand_counter is set in the ISRs on receipt of a bit.

1 Like

no wonder it keeps reading incorrect bits. i'll have to adjust that one.

If that is the intention then it should be declared volatile.

1 Like

Does the HID reader have configuration options? You might be able to change behaviors.

1 Like

saw another example code and changed it to volatile. Thanks