hi,
i am attempting to make an RFID controlled door lock for my home.
hardware:
125khz rdm630 rfid reader
125khz tags (12)
single opto isolated relay
12v door strike
i have created a sketch that reads a tag, compares the reading to an array of strings (my tags) stored in program memory, and if the current read is found there will activate the door strike via the relay signal pin for a few seconds.
my program compiles and runs, unlocks for about 2 valid tag reads then will no longer continue to process tag reads - kind of freezes. the rfid reader is still reading the tags as observed by it's led flashing when i bring a tag near but the door strike no longer unlocks
i have tried to minimize sram usage by storing strings in SRAM when possible and have tried commenting out all Serial calls and recompiling but still have same problem.
i read the tag into a String object which gets converted to a string array via String.toCharArray() for the comparison with the stored tags.
this is probably wasteful of resources but I can't seem to get the program to function correctly when I try and read directly into a char array.
could I please get some advice as to where I am going wrong?
the code is below:
#include <SoftwareSerial.h>
#include <avr/pgmspace.h>
SoftwareSerial RFID(6, 7);
String currentTag; //string objest to store current tag read
//pin to control electric door strike - door lock
const int lockPin=4;
// relay module is switched on when signal wire is LOW
const byte Locked=HIGH;
const byte Unlocked=!Locked;
char c;
//All Valid RFID Tags stored in SRAM
const char tag01[] PROGMEM = "111111111111\0";
const char tag02[] PROGMEM = "222222222222\0";
const char tag03[] PROGMEM = "333333333333\0";
const char tag04[] PROGMEM = "444444444444\0";
const char tag05[] PROGMEM= "555555555555\0";
const char tag06[] PROGMEM= "666666666666\0";
const char tag07[] PROGMEM= "777777777777\0";
const char tag08[] PROGMEM= "888888888888\0";
const char tag09[] PROGMEM= "999999999999\0";
const char tag10[] PROGMEM= "AAAAAAAAAAAA\0";
const char tag11[] PROGMEM= "BBBBBBBBBBBB\0";
const char tag12[] PROGMEM= "CCCCCCCCCCCC\0";
PROGMEM const char *myTagArray[] = {tag01,tag02,tag03,tag04,tag05,tag06,tag07,tag08,tag09,tag10,tag11,tag12};
void setup()
{
digitalWrite(lockPin,Locked);
pinMode(lockPin,OUTPUT);
Serial.begin(9600);
RFID.begin(9600);
Serial.print(F("Tag?\n"));
}
void loop()
{
currentTag = "";
while(RFID.available()>0)
{
while(currentTag.length()<14)
{
c=RFID.read();
currentTag += c;
}
}
if (currentTag.length() == 14)
{
currentTag = currentTag.substring(1,13); // removes first and last char
compareToValidTags();
}
}
void compareToValidTags()
{
char tagBuffer[13];
currentTag.toCharArray(tagBuffer,13);
Serial.print(F("\nTag: "));
Serial.print(currentTag);
currentTag = "";
for (int i = 0; i < 12; i++)
{
if (strcmp_P(tagBuffer, (char*)pgm_read_word(&(myTagArray*))) == 0) // Necessary casts and dereferencing, just copy.*
- {*
- Serial.println(F(" Valid. Unlocking."));*
- digitalWrite(lockPin,Unlocked);*
- delay(5000); //Unlock for 5 seconds*
- digitalWrite(lockPin,Locked);*
- Serial.print(F("ReLocked.\n"));*
- } *
- }*
}
--------------------------------------------------------------------------------------------------------------------------------
Thank you in advance for any help you can give.