I wrote a program for a door lock that would add tags using a master tag. It stores the tags in a text file on an SD card. I'll post the code and you can rip whatever you want from it. It works perfectly. You can also wipe the database by swiping the master card twice.
#include <SoftwareSerial.h>
#include <SD.h>
SoftwareSerial mySerial(2, 4);
int resetpin = 3, green = 7, red = 8, blue = 9, CS_pin = 10, relay = 6;
void setup() {
Serial.begin(9600); //Initialize main serial connection.
mySerial.begin(9600); //Initialize reader serial connection.
pinMode(resetpin, OUTPUT); //Define reset pin as an output.
pinMode(CS_pin, OUTPUT);
pinMode(green, OUTPUT);
pinMode(red, OUTPUT);
pinMode(blue, OUTPUT);
pinMode(relay, OUTPUT);
digitalWrite(resetpin, HIGH); //Pull the reset pin high.
Serial.println("Initializing SD Card");
if (!SD.begin(CS_pin)) {
Serial.println("Card Failed. Quitting");
return;
}
Serial.println("SD Card Ready");
Serial.println("Waiting for Tag");
}
void loop() {
char readByte, tempByte, addByte[13], tagString[13], mastertag[13] = {
'4', 'E', '0', '0', '0', '4', '3', 'D', '3', '8', '4', 'F' };
int index = 0, filesize;
boolean reading = false, cardreading = false, cardready = false;
while (mySerial.available() > 0) {
readByte = mySerial.read();
if(readByte == 2) reading = true;
if(readByte == 3) reading = false;
if(reading && readByte != 2 && readByte != 10 && readByte != 13){
tagString[index] = readByte;
index ++;
}
}
printtag(tagString);
if (strlen(tagString) != 0) {
if (database(tagString) == true) {
Serial.println("Match Found");
digitalWrite(relay, HIGH);
digitalWrite(green, HIGH);
delay(3000);
digitalWrite(relay, LOW);
digitalWrite(green, LOW);
}
else {
digitalWrite(red, HIGH);
Serial.println("No Match Found");
delay(1000);
digitalWrite(red, LOW);
}
}
if (compare(mastertag, tagString)) {
Serial.println("Master Tag Found, Entering Admin Mode");
resetreader();
index = 0;
digitalWrite(blue, HIGH);
delay(500);
digitalWrite(blue, LOW);
delay(500);
delay(2000);
while (cardready == false) {
digitalWrite(blue, HIGH);
delay(100);
digitalWrite(blue, LOW);
delay(100);
while (mySerial.available() > 0) {
tempByte = mySerial.read();
if (tempByte == 2) cardreading = true;
if (tempByte == 3) cardreading = false;
if (cardreading && tempByte != 2 && tempByte != 10 && tempByte != 13) {
addByte[index] = tempByte;
index ++;
}
}
if (index == 12) {
Serial.println("Done Reading");
cardready = true;
if (database(addByte) == false) {
File logread = SD.open("log.txt", FILE_WRITE);
filesize = logread.size();
logread.seek(filesize);
logread.print(addByte[0]);
logread.print(addByte[1]);
logread.print(addByte[2]);
logread.print(addByte[3]);
logread.print(addByte[4]);
logread.print(addByte[5]);
logread.print(addByte[6]);
logread.print(addByte[7]);
logread.print(addByte[8]);
logread.print(addByte[9]);
logread.print(addByte[10]);
logread.print(addByte[11]);
logread.close();
}
if (compare(addByte, mastertag)) {
SD.remove("log.txt");
Serial.println("Database Cleared");
digitalWrite(red, HIGH);
delay(100);
digitalWrite(red, LOW);
delay(100);
digitalWrite(red, HIGH);
delay(100);
digitalWrite(red, LOW);
delay(100);
digitalWrite(red, HIGH);
delay(100);
digitalWrite(red, LOW);
delay(3000);
}
}
}
}
cleartag(tagString);
resetreader();
delay(200);
}
void resetreader() {
digitalWrite(resetpin, LOW);
digitalWrite(resetpin, HIGH);
delay(150);
}
boolean compare(char one[], char two[]) {
for (int i = 0; i < 12; i++) {
if (one[i] != two[i]) {
return false;
}
}
return true;
}
void cleartag(char one[]) {
for (int i = 0; i < 13; i++) {
one[i] = 0;
}
}
void printtag(char one[]) {
int printer = 0;
if (!strlen(one) == 0) {
for (int i = 0; i < 12; i++) {
Serial.print(one[i]);
printer++;
}
if (printer == 12) {
Serial.println();
}
}
}
boolean database(char one[]) {
char sdarray[13];
boolean match = false;
File logread = SD.open("log.txt", FILE_WRITE);
if (logread) {
logread.seek(0);
while (logread.available() && match == false) {
for (int i = 0; i < 12; i++) {
sdarray[i] = logread.read();
}
if (compare(one, sdarray) && match == false) {
match = true;
logread.close();
return true;
}
}
if (match == false && !logread.available()) {
logread.close();
return false;
}
}
}