Skip to content

Instantly share code, notes, and snippets.

@jonmarkgo
Last active August 29, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jonmarkgo/9058825 to your computer and use it in GitHub Desktop.
Save jonmarkgo/9058825 to your computer and use it in GitHub Desktop.
// Modified by Worapoht K. and Jonathan Gottfried
#include <SoftwareSerial.h>
#include <Servo.h>
int val = 0;
char code[10];
int bytesread = 0;
int rfidPin = 2;
#define rxPin 8
#define txPin 9
SoftwareSerial RFID = SoftwareSerial(rxPin,txPin);
// RFID reader SOUT pin connected to Serial RX pin at 2400bps to pin8
Servo myservo;
int lock = 0;
int unlock = 180;
int servoPin = 12;
String keyCode;
String real = "0415D952E0"; //this is my RFID tag, make sure to insert your own here
void setup()
{
Serial.begin(2400); // Hardware serial for Monitor 2400bps
pinMode(rfidPin,OUTPUT); // Set digital pin 2 as OUTPUT to connect it to the RFID /ENABLE pin
digitalWrite(rfidPin, LOW); // Activate the RFID reader
RFID.begin(2400);
myservo.attach(servoPin);
myservo.write(lock); //my servo is locked at position 0, yours might be locked at position 180 - test this!
delay(1000); //pause while the servo moves to the locked position
}
void loop()
{
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
}
if(bytesread == 10)
{ // if 10 digit read is complete
Serial.print("TAG code is: "); // possibly a good TAG
Serial.println(code); // print the TAG code
keyCode = code;
keyCode = keyCode.substring(0,10); //turn our code into a 10-digit String
if (keyCode.equals(real)) {
Serial.println("Code Verified!");
if (myservo.read() >= 90)
{
Serial.println("Locking door!");
myservo.write(lock); //lock
}
else {
Serial.println("Unlocking door!");
myservo.write(unlock); //unlock
}
delay(3000); //wait for servo to lock/unlock
Serial.println("Finished unlocking/locking door.");
}
}
bytesread = 0;
delay(500); // wait for a second
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment