Created
July 18, 2014 21:40
-
-
Save Jnesselr/b0adbe29c7e9b529b790 to your computer and use it in GitHub Desktop.
Improved Arduino code from http://theifdark.blogspot.com/2014/07/arduino-rfid-card-door-lock-system-100.html
This file contains 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
#include <SoftwareSerial.h> | |
#define CARD_CODE_LENGTH 10 | |
const int ledPin = 2; | |
const int doorLock = 4; | |
const int txPin = 6; | |
const int rxPin = 8; | |
int byteRead = 0; | |
int bytesRead = 0; | |
int wrong = 0; | |
char code[CARD_CODE_LENGTH+1] = {0}; // +1 for the null terminator used when printing the code | |
const char availableCodes[][CARD_CODE_LENGTH+1] = { | |
"XXXXXXXXXX", | |
"XXXXXXXXXX" | |
}; | |
const int numberOfCodes = sizeof(availableCodes)/sizeof(availableCodes[0]); | |
void setup() { | |
Serial.begin(9600); | |
pinMode(ledPin, OUTPUT); | |
digitalWrite(ledPin, LOW); | |
pinMode(doorLock, OUTPUT); | |
digitalWrite(doorLock, LOW); | |
} | |
void loop() { | |
SoftwareSerial RFID = SoftwareSerial(rxPin,txPin); | |
RFID.begin(2400); | |
if(RFID.read() == 10) { // I'm not sure what this refers to. This would imply all of the cards start with the number 10 which is the Line feed character | |
bytesRead = 0; | |
while(bytesRead<CARD_CODE_LENGTH) { | |
byteRead = RFID.read(); | |
if((byteRead == 10)||(byteRead == 13)) { // Same as above. If they do refer to the characters, replace them with '\n' or '\r' respectively | |
break; | |
} | |
code[bytesRead] = byteRead; | |
bytesRead++; | |
} | |
if(bytesRead == CARD_CODE_LENGTH) { | |
checkCard(); | |
} else { | |
wrongCard(); | |
} | |
} | |
} | |
void checkCard() { | |
for(int i=0; i<numberOfCodes; i++) { | |
Serial.print(availableCodes[i]); | |
} | |
Serial.print("Card read: "); | |
Serial.println(code); | |
for(int i=0; i<numberOfCodes; i++) { | |
if(codeMatches(code, availableCodes[i])) { | |
rightCard(); | |
} | |
} | |
wrongCard(); | |
} | |
boolean codeMatches(const char codeA[11], const char codeB[11]) { | |
for(int i=0; i<CARD_CODE_LENGTH; i++) { // Don't check the null terminator since it doesn't matter | |
if(codeA[i] != codeB[i]) { | |
return false; | |
} | |
} | |
return true; | |
} | |
void rightCard() { | |
digitalWrite(ledPin, HIGH); | |
digitalWrite(doorLock, HIGH); | |
delay(5000); | |
digitalWrite(doorLock, LOW); | |
digitalWrite(ledPin, LOW); | |
} | |
void wrongCard() { | |
Serial.print("Wrong Card"); | |
Serial.println(""); | |
delay(300); | |
digitalWrite(ledPin, HIGH); | |
delay(300); | |
digitalWrite(ledPin, LOW); | |
delay(300); | |
digitalWrite(ledPin, HIGH); | |
delay(300); | |
digitalWrite(ledPin, LOW); | |
delay(300); | |
digitalWrite(ledPin, HIGH); | |
delay(300); | |
digitalWrite(ledPin, LOW); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment