Go Down

Topic: RFID door lock help/advice (Read 460 times) previous topic - next topic

fragtastic69

Jun 12, 2010, 09:16 pm Last Edit: Jun 12, 2010, 09:32 pm by fragtastic69 Reason: 1
So I managed to write up some code for a lock, and i'm looking to improve it.

I'm using the parallax RFID reader. (the blue one)
RadioShack actually had them and it was for 9.99 for the reader and two rfid cards

im hoping they have more cause they regularly cost 40

Some bits of code are scrounged up, like the reader part.

Any thoughts or different ways do do something would be appreciated.

LEDs are single color, going to change it to a tri-color LED when i get one.

I still need to get a locking mechanism of some sort to use.

I'm using software serial, imo its easier since i dont have to unplug the reader to program every time, and can use those pins for something else later

Code: [Select]

#include <SoftwareSerial.h>

int  val = 0;
char code[12];
char tag[12] = {'s', 'e', 'c', 'r', 'e', 't', 't', 'a', 'g', '!'};
int bytesread = 0;

#define rxPin 8
#define txPin 9 //dont need me

#define manual 1
#define auto 0
int automanuallock = auto;

#define unlocked 1
#define locked 0
int doorlockstatus = locked;

#define codeprintenabled 1
#define codeprintdisabled 0
int codeprintswitch = codeprintenabled;

#define powerPin    5
#define failPin     6
#define passPin     4
#define doorPin     3
#define enablePin 2
#define dooropentime 3000 // its in ms


void setup()
{
 Serial.begin(9600);  // Hardware serial for Monitor 9600bps
 pinMode(enablePin,OUTPUT);       // Set digital pin 2 as OUTPUT to connect it to the RFID /ENABLE pin
 digitalWrite(enablePin, LOW);    // Activate the RFID reader

 pinMode(powerPin, OUTPUT);      // Connected to Blue LED to indicate reader is ready
 pinMode(passPin, OUTPUT);       // Connected to Green LED to indicate user is valid
 pinMode(failPin, OUTPUT);       // Connected to Red LED to indicate user is NOT valid
 pinMode(doorPin, OUTPUT);       // Connected to relay to activate the door lock (im actually going to use a transistor instead of a relay)

 digitalWrite(powerPin, HIGH);

}


void loop()
{
 digitalWrite(enablePin, LOW);
 ReadRFID();
 codeprint(); //(un)comment for rfid code via serial
 digitalWrite(enablePin, HIGH);
 check();

}

void ReadRFID()
{
 SoftwareSerial RFID = SoftwareSerial(rxPin,txPin);
 RFID.begin(2400);

 if((val = RFID.read()) == 10)
 {   // check for header
   bytesread = 0;
   while(bytesread<10)
   {  // read 10 digit code
     val = RFID.read();
     if((val == 10)||(val == 13))
     {  // if header or stop bytes before the 10 digit reading
       break;                       // stop reading
     }
     code[bytesread] = val;         // add the digit          
     bytesread++;                   // ready to read next digit  
   }
 }
}

void check()
{
 if(bytesread >= 10) {              // if 10 digit read is complete
   if(strcmp(code, tag) == 0) {


     if (automanuallock == manual)
     {
     manualdoorlockorunlock();
     }
     else if (automanuallock == auto) {
       doorauto();
     }
   }

   else {
     failandlockplus();
   }
 }
}

void codeprint(){
 if (codeprintswitch == codeprintenabled)
 {
 Serial.println("TAG code is: ");
 Serial.print(code);
}
}



//autolocks door
void doorauto()
{
 Serial.println("Accepted");
 Serial.println("Unlocked.");
 digitalWrite(doorPin, HIGH);
 digitalWrite(passPin, HIGH);
 delay(dooropentime);
 Serial.println("Locked");
 digitalWrite(doorPin, LOW);
 digitalWrite(passPin, LOW);
}

void manualdoorlockorunlock(){
 if (doorlockstatus == locked)
 {
   doorunlock();
 }
 else if (doorlockstatus == unlocked) {
   doorlock();
 }

}

void doorunlock()
{
 Serial.println("Accepted.");
 Serial.println("Door unlocked.");
 digitalWrite(doorPin, HIGH);
 digitalWrite(passPin, HIGH);
 doorlockstatus = unlocked;
 delay(2000);
 digitalWrite(passPin, LOW);
}

void doorlock()
{
 Serial.println("Accepted.");
 Serial.println("Door locked.");
 digitalWrite(passPin, HIGH);
 digitalWrite(doorPin, LOW);
 doorlockstatus = locked;
 delay(2000);
 digitalWrite(passPin, LOW);
}


void failandlock()
{
 Serial.println("Rejected.");
 Serial.println("Door locked.");
 digitalWrite(doorPin, LOW);
 digitalWrite(failPin, HIGH);
 delay(1000);
 digitalWrite(failPin, LOW);
}

void failandlockplus()
{
 if (doorlockstatus == locked)
 {
   Serial.println("Rejected.");
   Serial.println("Door already locked.");
   digitalWrite(failPin, HIGH);
   digitalWrite(doorPin, LOW);
   delay(1000);
   digitalWrite(failPin, LOW);
 }
 else if (doorlockstatus == unlocked) {
   Serial.println("Rejected.");
   Serial.println("Door locked.");
   digitalWrite(doorPin, LOW);
   digitalWrite(failPin, HIGH);
   doorlockstatus = 0;
   delay(1000);
   digitalWrite(failPin, LOW);
 }
}

void fail()
{
 Serial.println("Rejected.");
 digitalWrite(failPin, HIGH);
 delay(1000);
 digitalWrite(failPin, LOW);
}

void pass()
{
 Serial.println("Accepted.");
 digitalWrite(passPin, HIGH);
 delay(1000);
 digitalWrite(passPin, LOW);
}

void reset()
{
 Serial.println("Reset.");
 digitalWrite(doorPin, LOW);
 digitalWrite(failPin, LOW);
 digitalWrite(passPin, LOW);
 digitalWrite(powerPin, HIGH);
}


fragtastic69

#1
Jun 12, 2010, 09:30 pm Last Edit: Jun 12, 2010, 09:56 pm by fragtastic69 Reason: 1
On a separate note. (could be a whole new thread but im gonna keep it here)

Still need to get a locking mechanism of some sort to use though.
I was thinking of using an old 3.6v drill (the motor part) to turn the bolt of an existing door knob, since i can set the tension down to super low which wont break anything it just clicks instead of turning more.

Realized i was doing it completely wrong. Going to make a transistor H-bridge.

Go Up