Hello! I'm in over my head and looking for some guidance.
I am using this code from Wookai on GitHub to read RFID tags with a RDM6300.
I am trying to modify the code so that I can use 1 reader to read 4 tags (1 at a time), but trigger a different output for each tag. Currently, no matter what I have tried, all the tags trigger the 1st output. I have no formal training in this, so please explain it to me like I'm 5. Huge thanks in advance for any help!
This is what I have so far...
// define constants for pins
int SUCCESS = 10;
int ERROR = 13;
int relay1 = 8;
int relay2 = 9;
int relay3 = 10;
int relay4 = 11;
// variables to keep state
int readVal = 0; // individual character read from serial
unsigned int readData[10]; // data read from serial
int counter = -1; // counter to keep position in the buffer
char tagId[11]; // final tag ID converted to a string
char* authorizedTags1[2]; // array to hold the list of authorized tags
char* authorizedTags2[2];
char* authorizedTags3[2];
char* authorizedTags4[2];
// fills the list of authorzied tags
void initAuthorizedTags1() {
// add your own tag IDs here
authorizedTags1[0] = "01006ACCF4";
authorizedTags1[1] = "01006AC92E";
}
void initAuthorizedTags2() {
authorizedTags2[0] = "01006BE090";
authorizedTags2[1] = "01006BED14";
}
void initAuthorizedTags3() {
authorizedTags3[0] = "01006BE25C";
authorizedTags3[1] = "01006BE5E4";
}
void initAuthorizedTags4() {
authorizedTags4[0] = "01006BE818";
authorizedTags4[1] = "01006BDF6D";
}
void setup() {
Serial.begin(9600);
pinMode(SUCCESS, OUTPUT);
pinMode(ERROR, OUTPUT);
pinMode(relay1, OUTPUT);
pinMode(relay2, OUTPUT);
pinMode(relay3, OUTPUT);
pinMode(relay4, OUTPUT);
initAuthorizedTags1();
initAuthorizedTags2();
initAuthorizedTags3();
initAuthorizedTags4();
}
// check if the tag ID we just read is any of the authorized tags
int checkTag1() {
int i;
for (i = 0; i < 4; ++i) {
if (strcmp(authorizedTags1[i], tagId) == 0) {
return 1;
}
}
return 0;
}
int checkTag2() {
int i;
for (i = 0; i < 4; ++i) {
if (strcmp(authorizedTags2[i], tagId) == 0) {
return 2;
}
}
return 0;
}
int checkTag3() {
int i;
for (i = 0; i < 4; ++i) {
if (strcmp(authorizedTags3[i], tagId) == 0) {
return 3;
}
}
return 0;
}
int checkTag4() {
int i;
for (i = 0; i < 4; ++i) {
if (strcmp(authorizedTags4[i], tagId) == 0) {
return 4;
}
}
return 0;
}
// convert the int values read from serial to ASCII chars
void parseTag() {
int i;
for (i = 0; i < 10; ++i) {
tagId[i] = readData[i];
}
tagId[10] = 0;
}
// once a whole tag is read, process it
void processTag() {
// convert id to a string
parseTag();
// print it
printTag();
// check if the tag is authorized
if (checkTag1() == 1) {
tagSuccess1(); // if so, perform an action (blink a led, open a door, etc...)
}else if (checkTag2() == 2) {
tagSuccess2();
}else if (checkTag3() == 3) {
tagSuccess3();
}else if (checkTag4() == 4) {
tagSuccess4();
}
else {
tagFailed(); // otherwise, inform user of failure
}}
void printTag() {
Serial.print("Tag value: ");
Serial.println(tagId);
}
// perform an action when an authorized tag was read
void tagSuccess1() {
Serial.println("Tag 1 authorized.");
// here, we simply turn on the success LED for 2s
digitalWrite(SUCCESS, HIGH);
digitalWrite(ERROR, LOW);
delay(2000);
}
void tagSuccess2() {
Serial.println("Tag 2 authorized.");
// here, we simply turn on the success LED for 2s
digitalWrite(SUCCESS, HIGH);
digitalWrite(ERROR, LOW);
delay(2000);
}
void tagSuccess3() {
Serial.println("Tag 3 authorized.");
// here, we simply turn on the success LED for 2s
digitalWrite(SUCCESS, HIGH);
digitalWrite(ERROR, LOW);
delay(2000);
}
void tagSuccess4() {
Serial.println("Tag 4 authorized.");
// here, we simply turn on the success LED for 2s
digitalWrite(SUCCESS, HIGH);
digitalWrite(ERROR, LOW);
delay(2000);
}
// inform the user that the tag is not authorized
void tagFailed() {
Serial.println("Unauthorized access!");
digitalWrite(SUCCESS, LOW);
digitalWrite(ERROR, HIGH);
delay(2000);
}
// this function clears the rest of data on the serial, to prevent multiple scans
void clearSerial() {
while (Serial.read() >= 0) {
; // do nothing
}
}
void loop() {
// turn LEDs off
digitalWrite(SUCCESS, LOW);
digitalWrite(ERROR, LOW);
if (Serial.available() > 0) {
// read the incoming byte:
readVal = Serial.read();
// a "2" signals the beginning of a tag
if (readVal == 2) {
counter = 0; // start reading
}
// a "3" signals the end of a tag
else if (readVal == 3) {
// process the tag we just read
processTag();
// clear serial to prevent multiple reads
clearSerial();
// reset reading state
counter = -1;
}
// if we are in the middle of reading a tag
else if (counter >= 0) {
// save valuee
readData[counter] = readVal;
// increment counter
++counter;
}
}
}