Herrow
Not sure if it is of use to your. But the below code is used to recieve and send Wiegand signals which essentially is just sending bits back and forth.
I suggest you look at the interupt functions and research these a bit more. I can pretty much garunty you will have alot more joy from that point on.
Ive included some code below im at work and cannot explain it please advise if you have any questions and i will do my best to answer.
Code with sending an receiving
int i;
int reader1[30];
int readerTmp[30];
volatile int reader1Count = 0;
void reader1One(void) {
reader1[reader1Count] = 1;
reader1Count++;
}
void reader1Zero(void) {
reader1[reader1Count] = 0;
reader1Count++;
}
void setup()
{
Serial.begin(57600);
// Attach pin change interrupt service routines from the Wiegand RFID readers
attachInterrupt(0, reader1Zero, FALLING);//DATA0 to pin 2
attachInterrupt(1, reader1One, FALLING); //DATA1 to pin 3
delay(10);
//Set pins 10/D0 and 11/D1 to output for re transmission
pinMode(10, OUTPUT);
pinMode(11, OUTPUT);
digitalWrite(10, HIGH);
digitalWrite(11, HIGH);
// 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
for (i = 0; i < 30; i = i + 1) {
reader1[i] = 0;
}
reader1Count = 0;
digitalWrite(13, HIGH); // show Arduino has finished initilisation
}
void loop() {
if(reader1Count >= 27){
Serial.println("A");
readerTmp[0] = reader1[25] ;
readerTmp[1] = 0 ;
readerTmp[2] = 0 ;
readerTmp[3] = 0 ;
readerTmp[4] = 0 ;
readerTmp[5] = 0 ;
readerTmp[6] = 0 ;
readerTmp[7] = 0 ;
readerTmp[8] = 0 ;
readerTmp[9] = 0 ;
readerTmp[10] = 0 ;
readerTmp[11] = 0 ;
readerTmp[12] = 0 ;
readerTmp[13] = 0 ;
readerTmp[14] = 0 ;
readerTmp[15] = 0 ;
readerTmp[16] = 0 ;
readerTmp[17]= 0 ;
readerTmp[18] = 0 ;
readerTmp[19] = 0 ;
readerTmp[20] = 0 ;
readerTmp[21] = 0 ;
readerTmp[22] = 0 ;
readerTmp[23] = 0 ;
readerTmp[24] = 0 ;
readerTmp[25] = reader1[0] ;
//If sum of segments 2 to 13 even then First parity 1
if ((readerTmp[1] + readerTmp[2] + readerTmp[3] + readerTmp[4] + readerTmp[5] + readerTmp[6] + readerTmp[7] + readerTmp[8] + readerTmp[9] + readerTmp[10] + readerTmp[11] + readerTmp[12]) % 2){
readerTmp[0] = 1;
Serial.println("even = odd");
}else{
readerTmp[0] = 0;
Serial.println("even = even");
}
//If sum of segments 14 to 25 even then last parity 1
if ((readerTmp[13] + readerTmp[14] + readerTmp[15] + readerTmp[16] + readerTmp[17] + readerTmp[18] + readerTmp[19] + readerTmp[20] + readerTmp[21] + readerTmp[22] + readerTmp[23] + readerTmp[24]) % 2){
readerTmp[25] = 0;
Serial.println("odd = odd");
}else{
readerTmp[25] = 1;
Serial.println("odd = even");
}
for (i = 0; i < 26; i = i + 1) {
if(readerTmp[i] == 0){
delay(2);
digitalWrite(10, LOW);
delayMicroseconds(50);
digitalWrite(10, HIGH);
}else{
delay(2);
digitalWrite(11, LOW);
delayMicroseconds(50);
digitalWrite(11, HIGH);
}
Serial.print(readerTmp[i]);
readerTmp[i] = 0;
}
Serial.println("");
reader1Count = 0;
}
}
Code to receive 26bits
/* 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
*/
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(57600);
// Attach pin change interrupt service routines from the Wiegand RFID readers
attachInterrupt(0, reader1Zero, FALLING);//DATA0 to pin 2
attachInterrupt(1, reader1One, FALLING); //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(13, HIGH); // show Arduino has finished initilisation
}
void loop() {
if(reader1Count >= 27){
// Serial.print(" Reader 1 ");Serial.println(reader1,HEX);
Serial.println("A");
Serial.println(reader1 & 0xfffffff);
Serial.println(reader1, DEC);
Serial.println(reader1, BIN);
reader1 = 0;
reader1Count = 0;
}
}
Code to consistently send bits 0/1/0/1 etc.....
void setup(){
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
delay(5000);
}
void writeD0(){
digitalWrite(10, LOW);
delayMicroseconds(50);
digitalWrite(10, HIGH);
}
void writeD1(){
digitalWrite(11, LOW);
delayMicroseconds(50);
digitalWrite(11, HIGH);
}
void loop(){
delay(2);
digitalWrite(2, LOW);
delayMicroseconds(50);
digitalWrite(2, HIGH);
delay(2);
digitalWrite(3, LOW);
delayMicroseconds(50);
digitalWrite(3, HIGH);
}