Hello all,
I've been working at this project for a while in various incarnations and am running into trouble.
Here is what I'm trying to accomplish, 3 separate RFID readers able to read tag presence and output whether a tag is present or not and that tags ID string.
Where I am having trouble is in the SoftwareSerial department. It seems like lots of people have been having issues with this in similar forms to mine, but I haven't found a solution. I've got the idea that you can't have more than one softwareSerial port running at once, so initializing them in the setup loop doesn't work, which means they have to be initialized, and ended in the loop loop. I'm working with just two readers right now to get multiples to function and will add the third once I get the bugs worked out. Both readers work separately when the other reader is commented out, but when both are initialized at once, all I get is garbage and the tag aggregators and formatters don't work.
Code is below, please let me know if you've struggled with this as well and if you have any pointers.
Thanks
#include <SoftwareSerial.h>
SoftwareSerial rfid1(2,3);
SoftwareSerial rfid2(4,5);
char tag01[] = "1100A05E11FE";
char tag02[] = "1100A06B71AB";
char tagString1[13];
char tagString2[13];
byte id1 = 1;
const int resetPin = 13; //reset pin number
const int resetPin2 = 12;
int resetState = HIGH; //the state of the reader
int resetState2 = HIGH;
long prevMillis = 0; //will store last time state was updated
long interval = 500; //interval it will cycle
void setup() {
pinMode(resetPin, OUTPUT); //set the digital pin to output
pinMode(resetPin2, OUTPUT);
Serial.begin(9600);
// rfid1.begin(9600);
// rfid2.begin(9600);
}
void loop() {
unsigned long currentMillis = millis();
if(currentMillis - prevMillis > interval) {
prevMillis = currentMillis;
if(resetState == HIGH) {
resetState = LOW;
} else {
resetState = HIGH;
}
digitalWrite(resetPin, resetState);
}
if(resetState == HIGH) {
// /*
rfid1.begin(9600);
if (rfid1.available()) {
if (getTag1()) printTag1();
}
rfid1.end();
// */
// /*
rfid2.begin(9600);
if (rfid2.available()) {
if (getTag2()) printTag2();
}
rfid2.end();
// */
}
}
boolean getTag1() {
Serial.println("getTag1");
Serial.println(rfid1.read());
char startByte = rfid1.read();
delay(20);
if (startByte == 2) {
Serial.println("startByte1");
int index = 0;
while (index < 12) {
char incomingByte = rfid1.read();
tagString1[index] = incomingByte;
index++;
}
}
rfid1.flush();
return true;
}
boolean getTag2() {
Serial.println("getTag2");
Serial.println(rfid2.read());
char startByte = rfid2.read();
delay(20);
if (startByte == 2) {
Serial.println("startByte2");
int index = 0;
while (index < 12) {
char incomingByte = rfid2.read();
tagString2[index] = incomingByte;
index++;
}
}
rfid2.flush();
return true;
}
void printTag1() {
for (int i=0; i<12; i++) Serial.print(tagString1[i]);
Serial.println(compareTags1());
}
void printTag2() {
for (int i=0; i<12; i++) Serial.print(tagString2[i]);
Serial.println(compareTags2());
}
const char* compareTags1() {
if (strncmp(tag01, tagString1, 12) == 0) return " Tag 1";
else if (strncmp(tag02, tagString1, 12) == 0) return " Tag 2";
else return " Not recognized.";
}
const char* compareTags2() {
if (strncmp(tag01, tagString2, 12) == 0) return " Tag 1";
else if (strncmp(tag02, tagString2, 12) == 0) return " Tag 2";
else return " Not recognized.";
}