// Libraries
#include <LiquidCrystal.h> // library for using LCD
LiquidCrystal lcd (12,11,5,4,3,2); // initialize LCD
// Variable Initialization
int value=0; // Variable to store byte of tag ID
int bytesread=0; // variable to store the number of Bytes Read
char tag[11]; // variable to store read value for the TAG
boolean tag1status=false; // Initialize status flag for TAG 1
boolean tag2status=false; // Initialize status flag for TAG 2
boolean tag3status=false; // Initialize status flag for TAG 3
boolean ws1=false; // Initialize status flag for workstation 1
boolean ws2=false; // Initialize status flag for workstation 2
int ta1=0;
int ta2=0;
int ta3=0;
// Stored TAGS
char tag1[11] = "06008F6747";
char tag2[11] = "06008F5E38";
char tag3[11] = "840034A11B";
char taggy[11];
// Pin assignment
int rfpin=7; // Set Arduino Pin 7 for RFID reader
int doorpin=8; // Set Arduino Pin 8 for Door Strike
int sound=9; // Set Arduino Pin 9 for buzzer
String msg;
// Setup
void setup()
{
Serial.begin(2400); // Set Baud rate and enable Serial communication for Xbee
Serial3.begin(2400); // Set Baud rate for RFID Reader
pinMode(sound, OUTPUT); // Set buzzer pin as output
pinMode(rfpin, OUTPUT); // set RFID pin as output
pinMode(doorpin, OUTPUT); // set Door pin as output
pinMode(A8,INPUT);
}
// Main
void loop()
{
int val=analogRead(A8);
if (val>700)
{
reset();
lcd.clear();
lcd.begin(16,2); // Initiliaize LCD for display
lcd.setCursor(0,0);
lcd.print("Swipe Card");
lcd.setCursor(0,1);
lcd.print("For Access");
rfidenable();
readTAG();
if (codevalid())
{
rfiddisable();
checkTAG(tag);
}
else
{
rfiddisable();
}
while(Serial.available()>0)
{
Serial.read();
}
reset();
}
else
{
reset();
lcd.clear();
lcd.begin(16,2); // Initiliaize LCD for display
lcd.setCursor(0,0);
lcd.print("Attendance Mode");
lcd.setCursor(0,1);
lcd.print("Swipe Card");
rfidenable();
readTAG();
if (codevalid())
{
rfiddisable();
checktaga(tag);
}
else
{
rfiddisable();
}
while(Serial.available()>0)
{
Serial.read();
}
reset();
}
}
// read tag
void readTAG()
{
byte nextbyte;
while(Serial3.available() <= 0) {}
if((nextbyte = Serial3.read()) == 10) {
byte bytesread = 0;
while(bytesread < 10) {
if(Serial3.available() > 0) { //wait for the next byte
if((nextbyte = Serial3.read()) == 13) break;
tag[bytesread++] = nextbyte;
}
}
}
}
boolean codevalid() {
byte nextbyte;
int count = 0;
while (Serial3.available() < 2) {
delay(1);
if(count++ > 100) return false;
}
Serial.read();
if ((nextbyte = Serial3.read()) == 10) {
byte bytesread = 0;
while (bytesread < 10) {
if (Serial3.available() < 2) {
if ((nextbyte = Serial3.read()) == 13) break;
if (tag[bytesread++] != nextbyte) return false;
}
}
Serial.read();
}
return true;
}
// check for validity of tag
void checkTAG(char tag[])
{
if(strlen(tag) == 0) return; //empty, no need to contunue
if(comparetag(tag, tag1))
{
if (tag1status==false)
{
tag1status=true;
allocate();
}
else
{
tag1status=false;
deallocate();
}
}
else if(comparetag(tag, tag2))
{
if (tag2status==false)
{
tag2status=true;
allocate();
}
else
{
tag2status=false;
deallocate();
}
}
else if(comparetag(tag, tag3))
{
if (tag3status==false)
{
tag3status=true;
allocate();
}
else
{
tag3status=false;
deallocate();
}
}
else
{
invalidtag();
}
}
void checktaga(char tag[])
{
if(strlen(tag) == 0) return; //empty, no need to contunue
if(comparetag(tag, tag1))
{
sendtag(tag);
}
else if(comparetag(tag, tag2))
{
sendtag(tag);
}
else if(comparetag(tag, tag3))
{
sendtag(tag);
}
else
{
sendtag(tag);
}
}
void sendtag(char tag[])
{
lcd.clear();
lcd.begin(16,2);
lcd.setCursor(0,0);
lcd.print("Attendance Taken");
lcd.setCursor(0,1);
lcd.print(tag);
msg=tag;
Serial.println(msg);
analogWrite(9, 40);
delay(400);
analogWrite(9, 0);
delay(400);
return;
}
void lcd1(int station)
{
digitalWrite(doorpin,HIGH);
lcd.clear();
lcd.begin(16,2);
lcd.setCursor(0,0);
lcd.print("Access Granted");
lcd.setCursor(0,1);
if ( station==0)
{
lcd.print("No Free Station");
}
else
{
lcd.print("Station #");
lcd.print(station);
}
analogWrite(9, 40);
delay(200);
analogWrite(9, 0);
delay(1500);
digitalWrite(doorpin,LOW);
return;
}
void lcd2(int station)
{
lcd.clear();
lcd.begin(16,2);
lcd.setCursor(0,0);
lcd.print("Workstation # ");
lcd.print(station);
lcd.setCursor(0,1);
lcd.print("Dealocated");
analogWrite(9, 40);
delay(200);
analogWrite(9, 0);
delay(800);
return;
}
void allocate()
{
if (ws1==false)
{
if (tag1status==true && ta1==0)
{
ta1=1;
lcd1(1);
msg="Station#1\tAssigned\t";
msg+=tag1;
Serial.println(msg);
ws1=true;
}
else if (tag2status==true && ta2==0)
{
ta2=1;
lcd1(1);
msg="Station#1\tAssigned\t";
msg+=tag2;
Serial.println(msg);
ws1=true;
}
else if (tag3status==true && ta3==0)
{
ta3=1;
lcd1(1);
msg="Station#1\tAssigned\t";
msg+=tag3;
Serial.println(msg);
ws1=true;
}
}
else if (ws2==false)
{
if (tag1status==true && ta1==0)
{
ta1=2;
lcd1(2);
msg="Station#2\tAssigned\t";
msg+=tag1;
Serial.println(msg);
ws2=true;
}
else if (tag2status==true && ta2==0)
{
ta2=2;
lcd1(2);
msg="Station#2\tAssigned\t";
msg+=tag2;
Serial.println(msg);
ws2=true;
}
else if (tag3status==true && ta3==0)
{
ta3=2;
lcd1(2);
msg="Station#2\tAssigned\t";
msg+=tag3;
Serial.println(msg);
ws2=true;
}
}
else
{
for(int i = 0; i < 11; i++)
{
taggy[i]=tag[i];
}
if (ta1==0)
{
tag1status=false;
}
if (ta2==0)
{
tag2status=false;
}
if (ta3==0)
{
tag3status=false;
}
lcd1(0);
msg="No_Station\tAccess_Granted\t";
msg+=taggy;
Serial.println(msg);
}
}
void deallocate()
{
if (ws1==true)
{
if (tag1status==false && ta1==1)
{
ta1=0;
ws1=false;
msg="Station#1\tDeallocated\t";
msg+=tag1;
Serial.println(msg);
lcd2(1);
}
else if (tag2status==false && ta2==1)
{
ta2=0;
ws1=false;
msg="Station#1\tDeallocated\t";
msg+=tag2;
Serial.println(msg);
lcd2(1);
}
else if (tag3status==false && ta3==1)
{
ta3=0;
ws1=false;
msg="Station#1\tDeallocated\t";
msg+=tag3;
Serial.println(msg);
lcd2(1);
}
}
else if (ws2==true)
{
if (tag1status==false && ta1==2)
{
ta1=0;
ws2=false;
msg="Station#2\tDeallocated\t";
msg+=tag1;
Serial.println(msg);
lcd2(2);
}
else if (tag2status==false && ta2==2)
{
ta2=0;
ws2=false;
msg="Station#2\tDeallocated\t";
msg+=tag2;
Serial.println(msg);
lcd2(2);
}
else if (tag3status==false && ta3==2)
{
ta3=0;
ws2=false;
msg="Station#2\tDeallocated\t";
msg+=tag3;
Serial.println(msg);
lcd2(2);
}
}
}
void reset()
{
for(int i = 0; i < 11; i++){
tag[i] = 0;
}
}
boolean comparetag(char one[], char two[]){
if(strlen(one) == 0) return false; //empty
for(int i = 0; i < 11; i++){
if(one[i] != two[i]) return false;
}
return true; //no mismatches
}
void invalidtag()
{
msg="No_Station\tInvalid_TAG\t";
msg+=tag;
Serial.println(msg);
lcd.setCursor(0,0);
lcd.print("Invalid Tag");
lcd.setCursor(0,1);
lcd.print("Access Denied");
analogWrite(9, 40);
delay(200);
analogWrite(9, 0);
delay(200);
analogWrite(9, 40);
delay(200);
analogWrite(9, 0);
analogWrite(9, 40);
delay(200);
analogWrite(9, 0);
delay(200);
lcd.clear();
}
void rfidenable()
{
digitalWrite(rfpin, LOW); // enable RFID reader
}
void rfiddisable()
{
digitalWrite(rfpin, HIGH); // enable RFID reader
}
Login