I have a code that loops through 2 different serial inputs on the arduino mega, and sends them to serial constantly:
int RFIDResetPin = 13;
void setup()
{
Serial.begin(9600);
Serial1.begin(9600);
Serial2.begin(9600);
pinMode(RFIDResetPin, OUTPUT);
digitalWrite(RFIDResetPin, HIGH);
}
void loop() {
char tagString1[13];
int index = 0;
boolean reading1 = false;
boolean reading2 = false;
///////////////////////////////////////
//this reads from the reader installed
//on serial1, which is pin 19
//////////////////////////////////////
while (Serial1.available() > 0) {
int readByte1 = Serial1.read(); //read next available byte
if (readByte1 == 2) reading1 = true;
if (readByte1 == 3) reading1 = false;
if (reading1 && readByte1 != 2 && readByte1 != 10 && readByte1 != 13) {
tagString1[index] = readByte1;
index++;
}
}
Serial.println(String("Reader 7: ") + tagString1); //the tag itself
delay(1);
clearTag(tagString1); //Clear the char of all value
resetReader(); //reset the RFID reader
//////////////////////////////////////////////////
// This bit reads from serial2, located on pin 17
/////////////////////////////////////////////////
while (Serial2.available() > 0) {
int readByte2 = Serial2.read();
if (readByte2 == 2) reading2 = true;
if (readByte2 == 3) reading2 = false;
if (reading2 && readByte2 != 2 && readByte2 != 10 && readByte2 != 13) {
tagString1[index] = readByte2;
index++;
}
}
Serial.println(String("Reader 8: ") + tagString1);
clearTag(tagString1);
resetReader();
}
void resetReader() {
///////////////////////////////////
//Reset the RFID reader to read again.
///////////////////////////////////
digitalWrite(RFIDResetPin, LOW);
digitalWrite(RFIDResetPin, HIGH);
delay(150);
}
void clearTag(char one[]) {
///////////////////////////////////
//clear the char array by filling with null – ASCII 0
//Will think same tag has been read otherwise
///////////////////////////////////
for (int i = 0; i < strlen(one); i++) {
one[i] = 0;
}
}
when not scanning anything, the serial monitor outputs this:
Reader 1:
Reader 2:
Reader 1:
Reader 2:
When I scan a tag, lets say on reader 1, I get this:
Reader 1: 000004f5
Reader 2:
Reader 1: 000004f5
Reader 2:
it is not possible to scan more than a single scanner at a time. Is it possible to get the first line to wait until a certain number of characters have been scanned (indicating a tag has been scanned), and then output to the serial monitor?After that initial run, the code will loop as I showed above. any help is appreciated.
index
is shared between the tags, and you do a 150ms reset cycle and clear each cycle, Per Bra1n's answer, do consider a state machine, and maybe use a index1 and index2 to track the number of bytes you've read on each, and separate the reporting into something conditional on having a "certain number of characters have been scanned"