I'm trying some code from a long time ago from here.
#include "Particle.h"
const int WIEGAND_D0_PIN = D2; // Green
const int WIEGAND_D1_PIN = D3; // White
void wiegandInterrupt(); // forward declaration
bool decodeWiegand(unsigned long value); // forward declaration
volatile int cardBitCount = 0;
volatile unsigned long cardValue = 0;
volatile unsigned long cardReadStart = 0;
void setup() {
Serial.begin(9600);
// Wiegand pulse are narrow, typically 100 microseconds, so interrupts are desirable to accurately
// detect them
attachInterrupt(WIEGAND_D0_PIN, wiegandInterrupt, FALLING);
attachInterrupt(WIEGAND_D1_PIN, wiegandInterrupt, FALLING);
}
void loop() {
if (cardReadStart != 0) {
if (cardBitCount == 26) {
// Got a valid 26-bit card
if (decodeWiegand(cardValue)) {
// Is valid
Particle.publish("card", String(cardValue), PRIVATE);
}
cardReadStart = 0;
cardBitCount = 0;
}
else
if (millis() - cardReadStart > 500) {
Serial.println("failed to read enough bits");
cardReadStart = 0;
cardBitCount = 0;
}
}
}
void wiegandInterrupt() {
if (cardBitCount == 0) {
cardReadStart = millis();
cardValue = 0;
}
else {
cardValue <<= 1;
}
if (pinReadFast(WIEGAND_D1_PIN) == LOW) {
cardValue |= 1;
}
cardBitCount++;
}
// Note: in the following table the bit numbers are 0-based, 0 - 25 for a 26-bit Wiegand code!
// Bit 25: Even parity, first half of bits
// Bits 24 - 17: Facility code, MSB first 0 - 255
// Bits 16 - 1: Card code, MSB first, 0 - 65536
// Bit 0: Odd parity, second half of bits
bool decodeWiegand(unsigned long value) {
bool valid = false;
int facility = (value >> 17) & 0xff;
int card = (value >> 1) & 0xffff;
unsigned long tempValue = value;
int parity = 0;
for(int ii = 0; ii < 13; ii++) {
if (tempValue & 1) {
parity++;
}
tempValue >>= 1;
}
if ((parity & 0x1) == 1) {
// First parity passed
parity = 0;
for(int ii = 0; ii < 13; ii++) {
if (tempValue & 1) {
parity++;
}
tempValue >>= 1;
}
if ((parity & 0x1) == 0) {
// Second parity passed; looks valid
Serial.printlnf("value=0x%x facility=%d card=%d", value, facility, card);
valid = true;
}
else {
Serial.printlnf("even parity error value=0x%x", value);
}
}
else {
Serial.printlnf("odd parity error value=0x%x", value);
}
return valid;
}
When I run this on a argon I'm just not getting enough bits. The reader is made for a 26bit h10301 format and that is what the card is. I'm running a i2c level shift board for now to get the 5v output to a 3.3 for the board.
I also have added system threading.