It works good when I use only one reader but not when I plug the second one. It seems to be a programmation problem, the readers read the tags but nothing returns on the serial monitor. I divided the code in two parts to avoid some conflicts between the two rfid readers.
But it doesn't work..
Can you help me!!?
The code is...
#include <NewSoftSerial.h>
NewSoftSerial mySerial1(2, 3); //first RFIDreader
NewSoftSerial mySerial2(4, 5); //second RFIDreader
int tagMem1[11]; //first list to stock data from rfidtag
int tagMem2[11]; //second list
boolean goPrint1 = false; //verification
boolean goPrint2 = false;
int OneByOne=1; //to read one by one
int numero[] = {67, 55, 68, 194, 205}; //number to identify RFIDtag
void setup() {
Serial.begin(57600);
mySerial1.begin(9600);
mySerial2.begin(9600);
}
void loop() {
//first RFIDreader
if(OneByOne==1){
// put tag to memory
int cnt1=0;
while (mySerial1.available()) {
int val1 = mySerial1.read();
if (cnt1<11) tagMem1[cnt1] = val1;
cnt1++;
goPrint1 = true;
}
if (goPrint1==true) { /// if there is some data from serial port
if (isTagOK1()==true) { /// check if data is OK
for(int i=0; i< 5; i++){
if(tagMem1[10]==numero[i]){
Serial.print("numero");
Serial.println(i); // print the Tag number on the serial monitor
}
}
}
goPrint1 = false;
}
delay(200);
}
// Second RFIDreader
if(OneByOne==-1){
// put tag to memory
int cnt2=0;
while (mySerial2.available()) {
int val2 = mySerial2.read();
if (cnt2<11) tagMem2[cnt2] = val2;
cnt2++;
goPrint2 = true;
}
if (goPrint2==true) { /// if there is some data from serial port
if (isTagOK2()==true) { /// check if data is OK
for(int j=0; j< 5; j++){
if(tagMem2[10]==numero[j]){
Serial.print("numero");
Serial.println(j); // print the Tag number on the serial monitor
}
}
}
goPrint2 = false;
}
delay(200);
}
OneByOne=-OneByOne; //to alternate between the two procedures of the two readers
}
boolean isTagOK1() { /// check procedure for the first reader
if ((tagMem1[0]==1) && (tagMem1[1]==11) && (tagMem1[8]==255)) {
return true;
}
else {
return false;
}
}
boolean isTagOK2() { /// check procedure for the second reader
if ((tagMem2[0]==1) && (tagMem2[1]==11) && (tagMem2[8]==255)) {
return true;
}
else {
return false;
}
}
you could try to put one RFID on the hardware port and the other one with NewSoftSerial ?
some ideas how to refactor your code (not tested) without the use of delay and minimal number of flags...
#include <NewSoftSerial.h>
NewSoftSerial mySerial1(2, 3); //first RFIDreader
NewSoftSerial mySerial2(4, 5); //second RFIDreader
int tagMem1[11]; //first list to stock data from rfidtag
int tagMem2[11]; //second list
boolean goPrint1 = false; //verification
boolean goPrint2 = false;
int OneByOne=1; //to read one by one
int numero[] = {67, 55, 68, 194, 205}; //number to identify RFIDtag
void setup()
{
Serial.begin(57600);
mySerial1.begin(9600);
mySerial2.begin(9600);
}
void loop()
{
if (rfid1() == true) // 11 chars read
{
Serial.print("RFID1: ");
for (int i=0; i< 11; i++)
{
Serial.print(tagMem1[i]);
}
Serial.println();
int person = checkCode(1);
if (person() > 0)
{
// open the door
}
}
if (rfid2() == true)
{
Serial.print("RFID2: ");
for (int i=0; i< 11; i++)
{
Serial.print(tagMem2[i]);
}
Serial.println();
int person = checkCode(2);
if (person() > 0)
{
// open the door
}
}
}
// reader help function
// every call one char/byte is read,
// after 11 calls tagMem is filled and it will return true
// the next call it will reset itself.
boolean rfid1()
{
static int i=0;
if (i==11) i=0;
if (mySerial1.Available() >0)
{
tagMem1[i] = mySerial1.Read();
i++;
}
return (i==11);
}
// idem
boolean rfid2()
{
static int i=0;
if (i==11) i=0; // reset for next RFID read
if (mySerial2.Available() >0)
{
tagMem2[i] = mySerial2.Read();
i++;
}
return (i==11);
}
// idem
int checkCode(int x)
{
if (x == 1)
{
int i=0;
while (tagMem1[i] == numero[i] && i < 11) i++;
if (i==11) return 1;
}
if (x == 2)
{
i=0;
while (tagMem2[i] == numero[i] && i < 11) i++;
if (i==11) return 2;
}
return 0;
}
Grumpy_Mike, I tried what you said but there is still nothing on the serial monitor.
Robtillart, i tried your code but still nothing. Even if there is just one rfidreader plugged on the arduino.
But your code works good when i modified it to react for only one rfidreader.
I really think there is a conflict when the arduino try to communicate with two readers.
Is it a problem about the code ? because of they communicate at the same baud (9600) ? ......
Do you have an idea?
Thanks!
Here is the simplier version of robtillart's code i used :
#include <NewSoftSerial.h>
NewSoftSerial mySerial1(2, 3); //first RFIDreader
NewSoftSerial mySerial2(4, 5); //second RFIDreader
int tagMem1[11]; //first list to stock data from rfidtag
int tagMem2[11]; //second list
void setup()
{
Serial.begin(57600);
mySerial1.begin(9600);
mySerial2.begin(9600);
}
void loop()
{
if (rfid1() == true) // 11 chars read
{
Serial.print("RFID1: ");
for (int i=0; i< 11; i++)
{
Serial.print(tagMem1[i]);
}
Serial.println();
}
if (rfid2() == true)
{
Serial.print("RFID2: ");
for (int i=0; i< 11; i++)
{
Serial.print(tagMem2[i]);
}
Serial.println();
}
}
// reader help function
// every call one char/byte is read,
// after 11 calls tagMem is filled and it will return true
// the next call it will reset itself.
boolean rfid1()
{
static int i=0;
if (i==11) i=0;
if (mySerial1.available() >0)
{
tagMem1[i] = mySerial1.read();
i++;
}
return (i==11);
}
// idem
boolean rfid2()
{
static int i=0;
if (i==11) i=0; // reset for next RFID read
if (mySerial2.available() >0)
{
tagMem2[i] = mySerial2.read();
i++;
}
return (i==11);
}
The baud-rate 9600 should not be the problem as far as I know.
Robtillart, i tried your code but still nothing. Even if there is just one rfidreader plugged on the arduino.
But your code works good when i modified it to react for only one rfidreader.
So the base-code seems to work, so lets try to modify that code so that one RFID works with NewSoftSerial and the other on the hardware Serial.
You need to connect the RFID2 to the HW serial pin 0 AFTER downloading the sketch.
give it a try (code not tested, just modified, might need some fixing still)
#include <NewSoftSerial.h>
NewSoftSerial mySerial1(2, 3); //first RFIDreader
// NewSoftSerial mySerial2(4, 5); //second RFIDreader
int tagMem1[11]; //first list to stock data from rfidtag
int tagMem2[11]; //second list
void setup()
{
Serial.begin(9600);
mySerial1.begin(9600);
// mySerial2.begin(9600);
}
void loop()
{
if (rfid1() == true) // 11 chars read
{
Serial.print("RFID1: ");
for (int i=0; i< 11; i++)
{
Serial.print(tagMem1[i]);
}
Serial.println();
}
if (rfid2() == true)
{
Serial.print("RFID2: ");
for (int i=0; i< 11; i++)
{
Serial.print(tagMem2[i]);
}
Serial.println();
}
}
// reader help function
// every call one char/byte is read,
// after 11 calls tagMem is filled and it will return true
// the next call it will reset itself.
boolean rfid1()
{
static int i=0;
if (i==11) i=0;
if (mySerial1.available() >0)
{
tagMem1[i] = mySerial1.read();
i++;
}
return (i==11);
}
// idem
boolean rfid2()
{
static int i=0;
if (i==11) i=0; // reset for next RFID read
if (Serial.available() >0)
{
tagMem2[i] = Serial.read();
i++;
}
return (i==11);
}
Thank you robtillaart.
So it was a comunication problem between the two Serial created with NewSoftSerial().
For the next step of my project, I need to communicate with 5 rfidreaders.... hmm
So i want to know if there is an other way to create new Serials ?
I may have to use an arduinoMega to have many RXpin.