Browse over 10,000 Electronics Projects using the Page Numbering provided at the bottom of each Page.

OpenAccess RFID functions

OpenAccess RFID functions

Here are the wiegand readers:

//RFID reader stuff
#include <WIEGAND26.h> // Wiegand 26 reader format library
byte reader1Pins[]={
19,18}; // Reader 1 connected to pins 19,18
byte reader2Pins[]={
2,3}; // Reader2 connected to pins 2,3
long reader1 = 0;
int reader1Count = 0;
long reader2 = 0;
int reader2Count = 0;
const byte reader2buzz = 6; //pin for the buzzer on Reader2
const byte reader2led = 4; //pin for led1 on Reader2
const byte reader2led2 = 5; //pin for led2 on Reader2
const byte reader1buzz = 7; //pin for the buzzer on Reader1
const byte reader1led = 8; //pin for led1 on Reader1
const byte reader1led2 = 17; //pin for led2 on Reader1
WIEGAND26 wiegand26;
//Get the interrupt number of a given digital pin on an Arduino MEGA.
const byte pinToInterrupt[22] = {
-1,-1,0,1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,5,4,3,2};

This declares all the pins used , it gives a map from pin number to interrupt numbers (I got that from Mike and I like the idea), and the global variables checked by the wiegand functions.  The pins used for the RFID stuff are defined in a function:

void RFID_init()
{
pinMode(reader2buzz, OUTPUT);
pinMode(reader2led, OUTPUT);
pinMode(reader2led2, OUTPUT);
digitalWrite(reader2led, 1); //active low
digitalWrite(reader2led2, 1); //active low
digitalWrite(reader2buzz, 1); //active low
attachInterrupt(pinToInterrupt[reader2Pins[1]], callReader2One, CHANGE);
attachInterrupt(pinToInterrupt[reader2Pins[0]], callReader2Zero, CHANGE);
wiegand26.initReaderTwo();
pinMode(reader1buzz, OUTPUT);
pinMode(reader1led, OUTPUT);
pinMode(reader1led2, OUTPUT);
digitalWrite(reader1led, 1); //active low
digitalWrite(reader1led2, 1); //active low
digitalWrite(reader1buzz, 1); //active low
attachInterrupt(pinToInterrupt[reader1Pins[1]], callReader1One, CHANGE);
attachInterrupt(pinToInterrupt[reader1Pins[0]], callReader1Zero, CHANGE);
wiegand26.initReaderOne();
}

I broke up all the init functions and stuff like that because as I built this code I changed hardware so often that knowing where to edit the code is very valuable.  The RFID readers have timeouts for when to give up after they’ve accepted a card:

//RFID timer stuff
const unsigned long RFID_time = 30000; //timeout for the RFID scan in thousandths of a second
unsigned long RFID_start_time = 0; //used for the count down timer

These two functions that reset some global variables in the case an rfid entry attempt is not successful (garbage collection).

void RFID_reset()
{
clear_pin();
//RFID_start_time = 0; //not needed
tag_read = 0;
digit = 0;
new_tag_entry = 0;
}
void RFID_error_out()
{
can_add = 0; //reset user capabilities
can_doorhold = 0; //reset user capabilities
can_doorbell = 0; //reset user capabilities
RFID_reset();
beep_repeat(2,200,3);//delay timer
LCD_default();
}

The readers can beep, that function is defined here (0 beeps both readers if you didn’t figure it out):

void beep_reader(int reader,int d)//blocking
{
switch(reader){
case 0:
{
digitalWrite(reader2led, 0);
digitalWrite(reader2buzz, 0);
digitalWrite(reader1led, 0);
digitalWrite(reader1buzz, 0);
delay(d);
digitalWrite(reader2led, 1);
digitalWrite(reader2buzz, 1);
digitalWrite(reader1led, 1);
digitalWrite(reader1buzz, 1);
}
case 1:
{
digitalWrite(reader1led, 0);
digitalWrite(reader1buzz, 0);
delay(d);
digitalWrite(reader1led, 1);
digitalWrite(reader1buzz, 1);
}
case 2:
{
digitalWrite(reader2led, 0);
digitalWrite(reader2buzz, 0);
delay(d);
digitalWrite(reader2led, 1);
digitalWrite(reader2buzz, 1);
}
default:
{
}
}
}

That beep code is yet again wrapped up here to add a duration for repeated beeps:

void beep_repeat(int reader,int time,int number)//blocking
{
/*Serial.print(“beep repeat”);
Serial.print(” “);
Serial.print(reader);
Serial.print(” “);
Serial.print(time);
Serial.print(” “);
Serial.println(number);*/
for(int i = 0;i<number;i++)
{
beep_reader(reader,time);
delay(time);
}
}

This is how I call it from the serial port:

// beeps reader<reader number> for <duration> in delay(arg) format
//format: BEEP;<reader number>;<duration>
void SCmd_beep()
{
Serial.println(“beepcommand”);
char *arg;
arg = SCmd.next();
if (arg != NULL)
{
if(atoi(arg) == 1)
{
arg = SCmd.next();
if (arg != NULL)
{
beep_reader(1,atoi(arg));
}
}
else if(atoi(arg) == 2)
{
arg = SCmd.next();
if (arg != NULL)
{
beep_reader(2,atoi(arg));
}
}
else if(atoi(arg) == 0)
{
arg = SCmd.next();
if (arg != NULL)
{
beep_reader(0,atoi(arg));
}
}
}
}

This stuff is at the bottom of the code, it is needed because the wiegand26 library is not written as well as it could be:

void callReader1Zero(){
wiegand26.reader1Zero();
}
void callReader1One(){
wiegand26.reader1One();
}
void callReader2Zero(){
wiegand26.reader2Zero();
}
void callReader2One(){
wiegand26.reader2One();
}

This is part of the greater OpenAccess project.

 

More Articles to Read

avatar
EEWeb
avatar
Dangerous Prototypes
avatar
EEWeb
avatar
Arduino Blog
avatar
Arduino Blog
avatar
Ken Shirrif
avatar
Dangerous Prototypes
avatar
EEWeb
avatar
Arduino Blog
avatar
Freetronics
avatar
Arduino Blog
avatar
Dangerous Prototypes

Top




Shares