Last active
December 15, 2015 07:18
-
-
Save Shadow6363/5221939 to your computer and use it in GitHub Desktop.
Arduino Code for RFID Door Lock
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int lockRelayPin = 7; | |
int lghtRelayPin = 6; | |
int rfidResetPin = 13; | |
int photocellPin = 0; | |
int photocellReading = 0; | |
int invalidCount = 0; | |
char tag1[13] = "SECRET"; | |
char tag2[13] = "SECRET"; | |
char tag3[13] = "SECRET"; | |
void setup(void) { | |
Serial.begin(9600); | |
pinMode(lockRelayPin, OUTPUT); | |
pinMode(lghtRelayPin, OUTPUT); | |
pinMode(rfidResetPin, OUTPUT); | |
pinMode(photocellPin, INPUT); | |
digitalWrite(lockRelayPin, LOW); | |
digitalWrite(lghtRelayPin, LOW); | |
digitalWrite(rfidResetPin, HIGH); | |
} | |
void loop(void) { | |
boolean reading = false; int index = 0; char tagString[13]; | |
while(Serial.available()) { | |
int readByte = Serial.read(); | |
if(readByte == 2) { reading = true; } | |
if(readByte == 3) { reading = false; } | |
if(reading && readByte != 2 && readByte != 10 && readByte != 13) { | |
tagString[index] = readByte; index++; | |
} | |
} checkTag(tagString); clearTag(tagString); resetReader(); resetRelays(); | |
} | |
void validTagDetected(void) { | |
Serial.println("Valid"); unlockDoor(); | |
photocellReading = analogRead(photocellPin); | |
if(photocellReading < 300) { turnOnLght(); lockDoor(); delay(7000); turnOffLght(); } | |
else { delay(3000); lockDoor(); } invalidCount = 0; | |
} | |
void invalidTagDetected(void) { | |
Serial.print("Invalid: "); Serial.println(invalidCount++); if(invalidCount > 3) { delay(3600000); } | |
} | |
void unlockDoor(void) { Serial.println("Unlocking Door"); digitalWrite(lockRelayPin, HIGH); } | |
void turnOnLght(void) { Serial.println("Turning Light On"); digitalWrite(lghtRelayPin, HIGH); delay(3000); } | |
void lockDoor(void) { Serial.println("Locking Door"); digitalWrite(lockRelayPin, LOW); } | |
void turnOffLght(void) { Serial.println("Turning Light Off"); digitalWrite(lghtRelayPin, LOW); } | |
void checkTag(char tagID[]) { | |
if(strlen(tagID) == 0) { return; } | |
if(compareTags(tagID, tag1) || compareTags(tagID, tag2) || compareTags(tagID, tag3)) { | |
validTagDetected(); | |
} else { invalidTagDetected(); } | |
} | |
void resetReader() { | |
digitalWrite(rfidResetPin, LOW); digitalWrite(rfidResetPin, HIGH); delay(250); | |
} | |
void resetRelays() { | |
digitalWrite(lockRelayPin, LOW); digitalWrite(lghtRelayPin, LOW); | |
} | |
void clearTag(char tagID[]) { | |
for(int i = 0; i < strlen(tagID); i++) { tagID[i] = 0; } | |
} | |
boolean compareTags(char tagOneID[], char tagTwoID[]) { | |
if(strlen(tagOneID) == 0 || strlen(tagTwoID) == 0) { return false; } | |
for(int i = 0; i < 12; i++) { | |
if(tagOneID[i] != tagTwoID[i]) { return false; } | |
} return true; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment