RDM6300 + 2 antennas + SoftwareSerial

Hello, I'm building a door lock in which I want to use only the RFID tag. For security purposes, if someone gets in, the tag still needed to getting out. In other words, stay there until police arrive.

I build my system using RDM6300.

It works! However, from SoftwareSerial library, it take some time to switch from one port to another using lsiten() function. To solve that problem I just put a delay before the serial check.

I dislike delay solution and want to know if there is an elegant solution like:
isSerailSwitchok? I've checked SoftwareSerial and couldn't find the solution.

#include <SoftwareSerial.h>
#include "RDM6300.h"

SoftwareSerial in_rdm_serial(8, 9);
SoftwareSerial ex_rdm_serial(2, 3);



RDM6300<SoftwareSerial> in_rdm(&in_rdm_serial);
RDM6300<SoftwareSerial> ex_rdm(&ex_rdm_serial);

int led_pin = 13;
int doorpin = 7;

void blink(int n = 1) 
{
  for(int i = 0; i < n; i++) {
    digitalWrite(led_pin, HIGH);
    delay(200);
    digitalWrite(led_pin, LOW);
    delay(200);
  }
}

void opendoor(){
  digitalWrite(doorpin, HIGH);
  delay(1000);
  digitalWrite(doorpin, LOW);
  delay(4000);
}
  

void setup()
{
  pinMode(led_pin, OUTPUT);
  pinMode(doorpin, OUTPUT);
  
  
  digitalWrite(led_pin, LOW);
  digitalWrite(doorpin, LOW);
  
  Serial.begin(115200);
  //Serial.println("SETUP");
  //blink(5);
}

void loop()
{
  uint8_t NTAG = 6; 
  unsigned long long my_id[NTAG] = {
    0xA002A71CF,
    0x25005A9D0B,
    0x2500AEA722,
    0x25005A5942,
    0x25005B23A2,
    0x250058DACE
  };
  unsigned long long last_id_ex = 0;
  unsigned long long last_id_in = 0;



  // Check if external reader has tag
  ex_rdm.listen();
  delay(200);
  if ( ex_rdm.available() ){
    last_id_ex = ex_rdm.read();
    Serial.print("RFID Externo: 0x");
    ex_rdm.print_int64(last_id_ex);
    Serial.println();
  }
  
  // Check if reader internal has tag
  in_rdm.listen();
  delay(200);
  if ( in_rdm.available() ){
    last_id_in = in_rdm.read();
    Serial.print("RFID Interno: 0x");
    in_rdm.print_int64(last_id_in);
    Serial.println();
  }
 
  for (uint8_t i_aux = 0; i_aux < NTAG; i_aux++){
    if(last_id_ex == my_id[i_aux]){
      blink(2);
      opendoor;
    }
    if(last_id_in == my_id[i_aux]){
      blink(2);
      opendoor;
    }
  }
  

}

I dislike delay solution and want to know if there is an elegant solution like:
isSerailSwitchok? I've checked SoftwareSerial and couldn't find the solution.

Don't use two SoftwareSerial instances. All the information transmitted on interface 2 while you are listening to interface 1 are lost, same vice-versa. Use a Mega2560 that has 4 hardware serial interfaces for such a system (or one of the ARM based Arduinos where you can define many more serial interfaces).

I agree! However, for this particular instance, it was on purpose. I mean, ATmega2560 has a huge board. Since this is a door lock, size matters. I can use same micro/mini chine atmega328p.

Besides that, that is no way, in all good scenarios, that two persons will try to open the door at the same time. Even if that happens, door open is what matters.

I mean, for this particular case, SoftwareSerial was my intentional choice and I want some help to understand the library.

Going a bit further, I miss something. Listen function is boolen. I will try that and let you know.

EDIT: It doesn't work! Maybe is cli() function. Is there a way to be sure it is activated?

Besides that, that is no way, in all good scenarios, that two persons will try to open the door at the same time. Even if that happens, door open is what matters.

Even you know that never both can be active you don't know if a person is using RDM1 while you're listening to RDM2 or vice-versa. If that happens you'll never know it because the other SoftwareSerial is receiving nothing while listen is not active, so there's no buffering or something similar.

I mean, for this particular case, SoftwareSerial was my intentional choice and I want some help to understand the library.

It might be intentional but it's the wrong choice, at least if you use it for both channels. You might get it running if you connect one to the hardware serial and the other to the software emulation. But on an ATmega328p you'll loose the debugging channel then. So at least for the development I strongly suggest to use a Mega2560 to get a free debugging serial interface. A Micro might be another choice as it has a USB connection in the main MCU so you have the hardware serial available.

Hello Pylon, I really appreciate your suggestions. You are correct. I've done that already. I debugged using two different serial hardware and so on.

But again, I am still aware what are the needs of my project. I've developed with an Arduino Due that also has hardware serial.

The point is, my code is working! Besides that, I want to learn a bit more, and, as well, share my code. I've saw many post without a solution for the same problem.

That being sad, I want to have an elegant code, that's all. If my project decisions are right or wrong, that's not the point of this topic. We can discuss it later and I will be glad to ask for your help.

The point is, my code is working!

I doubt that. It may work for some time but it probably often fails.

That being sad, I want to have an elegant code, that's all.

I don't think elegant is the primary priority here. You should get it reliable, at the moment your code definitely fails in this category.

BTW, you should define what "elegant" means for you. If you can live with failures about half of the time you're solution is not that "un-elegant".