Pages: [1]   Go Down
Author Topic: Parallax RFID - Serial Sample code  (Read 786 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 15
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,

Just thought I'd contribute for once.

I just got one of these
http://www.parallax.com/Store/Microcontrollers/BASICStampModules/tabid/134/txtSearch/28140/List/1/ProductID/114/Default.aspx?SortField=ProductName%2cProductName

I'm making an RFID cat door using a Demilanove with a 328.

The code provided lets you add new tags using a 'Master Tag'. So the idea is you come along, swipe the master tag, and after the door unlocks for 10 seconds, the next tag you swipe within a 10 second period will be stored to the eeprom.

The stored tag will now have access to the door!

The master tag is hardcoded.

If you hold the master tag in front of the reader for 3 cycles (30~40 seconds) it will clear the stored memory of tags.

Enjoy.

Code:
#include <EEPROM.h>
#include <stdlib.h>
#include <NewSoftSerial.h>

int outEnable = 4; // Output enable for RFID

NewSoftSerial Rfid(2,3); 

char inByte = 0;     // incoming serial byte for 'Serial' ports
int i; // pointer for manipulating strings
int j; // pointer for manipulating strings

const char CR = 0x0D; // For GSM module
const char LF = 0x0A;

int unlockTimeout = 0;
int masterReset = 0;
int numberOfTagIDs = 0;

char foundTag[11];
char compareTag[11];
char masterTag[11] = "3D00213579";
const int sizeTag = sizeof(foundTag);

//********************************************************************//
// EEPROM address Table
const int NUM_TAG_IDS = 1;
const int TAG_START = 2;
//********************************************************************//
// Status Codes
bool MASTER_UNLOCK = false;
bool DOOR_UNLOCK = false;
bool TAG_STORE = false;
//********************************************************************//
// Initialisation... things that only happen on startup and reset
void setup()
{
// set the output enable for the RFID reader
pinMode(outEnable, OUTPUT);

// start serial at 115200 kbs
Serial.begin(115200);  

// start rfid serial at 2400 kbs
Rfid.begin(2400);

// Get the number of ID's in the table
if (EEPROM.read(NUM_TAG_IDS)!=numberOfTagIDs){
numberOfTagIDs=EEPROM.read(NUM_TAG_IDS);
}

Serial.println("HELLO!");
Serial.println();

if (numberOfTagIDs>0){
Serial.print("TAG LIST:");
Serial.print(numberOfTagIDs);
Serial.println(" Entries");
for (j=0; j<(numberOfTagIDs); j++){
for(i=0; i<(sizeTag-1); i++){
Serial.print(EEPROM.read((TAG_START+i+j*(sizeTag))));
}
Serial.println();
}
Serial.println();
}

digitalWrite(outEnable, LOW); // set output enable for RFID reader

}
//********************************************************************//
// Main loop...
//********************************************************************//
void loop()
{

if (MASTER_UNLOCK == true){ // gives us 10 seconds in which to add
delay(1000); // a new tag
unlockTimeout-=1; // decrement the time out counter
masterReset = 0; // clear the masterReset counter
if(unlockTimeout==0){
MASTER_UNLOCK=false;
Serial.println("New Tag Timeout");
Serial.println();
}
}

if (Rfid.available()>11){ // wait till there is enough data to process
if (getbyteRfid()==LF){ // check this is the start of a tag ID
// If so, get the tag data
for (i=0; i<(sizeTag-1); i++){
foundTag[i]=getbyteRfid();
}

if(getbyteRfid()!=CR){
Serial.println("Error reading Tag");
Serial.println();
}
else{
Serial.println("Found a Tag!");
if (MASTER_UNLOCK == true){
if(strcmp (foundTag,masterTag) != 0){
TAG_STORE = true;
MASTER_UNLOCK = false; // 1 key added, so thats it!
unlockTimeout = 0; // clear the timeout
masterReset = 0; // reset the master reset counter
Rfid.flush(); // clean up
}
else if(strcmp (foundTag,masterTag) == 0){
Serial.println("oooooH the Master Tag!");
unlockTimeout = 10; // reset the timeout
masterReset+=1;
Rfid.flush(); // clean up
DOOR_UNLOCK = true; // this gives us indicated of each cycle
// If we see the master tag 3 times in a row, we wipe the memory...
// sort of...
if (masterReset>=2){
Serial.println("SELFDESTRUCT INITIATED!");
EEPROM.write(NUM_TAG_IDS,0);
masterReset = 0;
MASTER_UNLOCK = false; // no more master stuff
}
}
}
else if(strcmp (foundTag,masterTag) == 0){
Serial.println("oooooH the Master Tag!");
Rfid.flush(); // clean up
MASTER_UNLOCK = true; // we can add a new tag now
DOOR_UNLOCK = true; // also unlock it for me
unlockTimeout = 10;
}
else{
Serial.println(foundTag);
if(searchFoundTag((char*)foundTag,(char*)compareTag)){
Serial.println("Tag is registered");
DOOR_UNLOCK = true;
}
else{
Serial.println("Unknown Tag");
DOOR_UNLOCK = false;
}
}
Serial.println();
}
}
}

if (DOOR_UNLOCK == true){
Serial.println("Unlock the Door");
delay(10000); // wait 10 seconds,
Rfid.flush(); // clean up
DOOR_UNLOCK = false; // door locked now
Serial.println("Lock the Door");
Serial.println();
}

if (TAG_STORE == true){
Serial.print("Storing the Tag...");
if(storeFoundTag((char*)foundTag,(char*)compareTag)){
Serial.println("Success!");
Serial.println();
}
TAG_STORE = false;
}

}
//********************************************************************//
// Function to search the eeprom for the foundTag ID
//********************************************************************//
bool searchFoundTag(char *target, char *compare){

if (numberOfTagIDs>0){
for (j=0; j<(numberOfTagIDs); j++){
for(i=0; i<(sizeTag); i++){
compare[i]=(EEPROM.read((TAG_START+i+j*(sizeTag))));
}
if (strcmp (target,compare) == 0){
return true;
}
}
}
return false;
}

//********************************************************************//
// Function to search the eeprom for the foundTag ID
//********************************************************************//
bool storeFoundTag(char *target, char *compare){

if (numberOfTagIDs>45){ // if we run out of space, no more storage...
// Anything but a Mega basically...
return false;
}

if(!searchFoundTag((char*)target,(char*)compare)){ // check if its already there
// store the tag
for(int i=0; i<(sizeTag); i++){
EEPROM.write((TAG_START+i+numberOfTagIDs*(sizeTag)),target[i]);
}
// check it was stored correctly
for(int i=0; i<(sizeTag); i++){
compare[i]=EEPROM.read((TAG_START+i+numberOfTagIDs*(sizeTag)));
}
// update the number of tags we have
if(strcmp(target,compare) == 0){
numberOfTagIDs+=1;
EEPROM.write(NUM_TAG_IDS,numberOfTagIDs);
return true;
}
else{
// report the problem
return false;
}
}
else{
// we already have it
Serial.print("have it...");
return true;
}
}
//********************************************************************//
// function to grab serial characters, forces us to wait untill more
// data arrives...
//********************************************************************//
char getbyteSerial(){

while (Serial.available() == 0) {
}
return Serial.read();
}
char getbyteRfid(){

while (Rfid.available() == 0) {
}
return Rfid.read();
}
Logged

Tennessee
Offline Offline
Newbie
*
Karma: 0
Posts: 22
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

This is for the older Parallax RFID reader as opposed to the newer read/write module.
Logged

Pages: [1]   Go Up
Jump to: