Serial.read data via Bluetooth from 2 Arduinos - convert problem

Hello,
I have a problem with sending integer value. I'm using 3 arduino's and 2 pair of HC-05 BT modules. To the first one (let's call it "Central") I connected LDR sensor and 2 BT modules which connect to 2 other Arduino's: BT1 and BT2 [just to be clear - there is a connections: Central - BT1 and Central - BT2]. Form the central I'm sending the value read by LDR sensor to BT1 and BT2 simultaneously and here everything works fine - both Arduino's receive exactly the value that the LDR sensor read. Right now I want send a specific numerical value depending on the LDR value read from BT1 and BT2 for e.g. if if the LDR value is less than 500, sent the number 100 [I will need it in the future, because I intend to additionally use the Qwiic AK9753 presence sensor]. I searched all over the internet and couldn't find a solution. On the forum I found a person with a similar problem, for which it was proposed to read values ​​as characters (link here : Sending a value between Arduinos using HC-05 Bluetooth modules ) I decided to try this way. But it dosen't help - the "Central" receives nothing. Interestingly, if one of the BT modules is turned off, the "Central" will receive a specific value, but it will not change regardless of the LDR value (for e.g If the first value was '200' it will recive only '200')

This is the code for Central:

#include <SoftwareSerial.h>

SoftwareSerial BT1(13, 12); // TX || RX
SoftwareSerial BT2(11, 10); // TX || RX

#define LDR A0

int ValueLDR = 0; 

const byte numChars1 = 32;
const byte numChars2 = 32;
char ID1[numChars1];
char ID2[numChars2];

boolean newData1 = false;
boolean newData2 = false;

void setup() 
{
  pinMode(LDR, INPUT);
  BT1.begin(9600);
  BT2.begin(9600);
  Serial.begin(9600);
}
void loop() 
{
  // sending LDR value to BT1 and BT2
  ValueLDR = analogRead(LDR); 
  BT1.println(ValueLDR); 
  BT2.println(ValueLDR); 
  //Serial.println(ValueLDR); 

  // reciving value form BT1 and BT2
  char l;
  char endMarker1 = '\n';
  static byte ndx1 = 0;
    
  while (BT1.available() > 0 && newData1 == false) 
  {
    l = BT1.read();

    if (l != endMarker1) 
    {
      ID1[ndx1] = l;
      ndx1++;
          
      if (ndx1 >= numChars1) 
      {
        ndx1 = numChars1 - 1;
      }
          
    } 
    else 
    {
      ID1[ndx1] = '\0'; // terminate the string
      ndx1 = 0;
      newData1 = true;
    }
  }

  char m;
  char endMarker2 = '\n';
  static byte ndx2 = 0;
    
  while (BT2.available() > 0 && newData2 == false) 
  {
    m = BT2.read();

    if (m != endMarker2) 
    {
      ID2[ndx2] = m;
      ndx2++;
          
      if (ndx2 >= numChars2) 
      {
        ndx2 = numChars2 - 1;
      }
          
    } 
    else
    {
      ID2[ndx2] = '\0'; // terminate the string
      ndx2 = 0;
      newData2 = true;
    }
  }

  Serial.println(ID1);
  Serial.println("   ");
  Serial.println(ID2);
  
  delay(500);
}

Code for BT1:

#include <SoftwareSerial.h>

SoftwareSerial BT1(11, 10); // RX | TX

char myData[10];
int ValueLDR; 

void setup() 
{
  BT1.begin(9600);
  Serial.begin(9600);
}


void loop() 
{
  // reciving LDR value from Central
  if (BT1.available()) 
 {
    byte m = BT1.readBytesUntil('\n', myData, 10); 
    myData[m] = '\0';
    ValueLDR = atoi(myData);  
    //Serial.println(ValueLDR); 
 }
 // sending a specific value to Central
  if (ValueLDR <= 500) 
  {
    char ID[] = "100";
    BT1.println(ID);
    Serial.println(ID);
  }
  else
  {
    char ID[] = "200";
    BT1.println(ID);
    Serial.println(ID);
  }
  delay(500); 
}

And code for BT2:

#include <SoftwareSerial.h>

SoftwareSerial BT2(11, 10); // RX | TX

char myData[10];
int ValueLDR; 

void setup() 
{
  BT2.begin(9600);
  Serial.begin(9600);
}


void loop() 
{
  // reciving LDR value from Central
  if (BT2.available()) 
 {
    byte m = BT2.readBytesUntil('\n', myData, 10); 
    myData[m] = '\0';
    ValueLDR = atoi(myData); 
    //Serial.println(ValueLDR); 
 }
 // sending a specific value to Central
  if (ValueLDR <= 500) 
  {
    char ID2[] = "100";
    BT2.println(ID);
    Serial.println(ID);
  }
  else
  {
    char ID2[] = "200";
    BT2.println(ID);
    Serial.println(ID);
  }
  delay(500); 
}

Do you have an idea how to solve this problem? I don't have much experience with Adruino, so I'm asking for help.

Thank you in advance!

did it help?

What do you mean?

did it help? did this person solved his similar problem? does it help you by your problem?

Not exactly. As I wrote before Arduino "Central" recive one of the numbers but only, when 1 od BT connection is active. If they are both, I get nothing. When I tried only his code on 2 Arduino's it worked.

then you can forget that topic.
so, seems your BT modules compromise each other.

Are you doing this with "auto connect" ?
If so, I suspect you will have to go further: PAIR, BIND, and LINK.
Otherwise - they will interfere, it's a mess.
https://www.martyncurrey.com/connecting-2-arduinos-by-bluetooth-using-a-hc-05-and-a-hc-06-pair-bind-and-link/

Check if the Arduino can handle two SoftwareSerial.

Well, I can use 2 SoftwareSerial, because i tested eariler with sending LDR value to both Arduino and it worked. I took a closer look at it and yeah - I cannot transmit and receive data at the same time. So the question is - It is there any alternative solution?

Post No.7 ?

Oh, ok. I didin't notice. I'm gonna try that.

Sending and receiving are different things. You can, to my knowledge, only listen on one SoftwareSerial at a time.

Which Arduinos are you using?

All of them are Arduino Uno's

i tried connect hc-05 moduels using page from post No. 7 but I coudn't and right now i can't even recevie LDR values. I have got this:

0
0
0
0
0
0
0
0
0
0
0

I restarted moduel many times and still it is not working. Do you have any idea why?

I have done this - once, binding an HC05 and an HC06.
Granted, the process was a pain - but it worked.
(If you don't do everything right, then it's wrong.)

The difference is a tried to connect 2 HC-05 moduels. After a failed attempt I configured both moduels in old way watching this video: HC-05 configuration. This method previously guaranteed the correct reception of the LDR value.

Yes, and that's the "auto-connect" way, which is the root cause of your conundrum.

Using two HC-05 modules - did you make one a 'master' and the other a 'slave' ?
( You can't do BIND, PAIR with two M's (or two S's). )

The youtube guy isn't doing with two pairs (either).
If there are two separate M/S Pair/Bind sets, it seems that there would not be a problem.
Only speculating - "extreme" proximity could be an issue. (I really do not know.)

The pair/bind operation would take care of that, but maybe not.

Maybe there is another way. There is a function called listen() which listens to a specific device.
I wrote a code that receives data from 2 tiles alternately adding a delay. By checking this function alone I was able to receive different single number values ​​simultaneously from 2 Arduinos. But combining this with sending LDR values ​​unfortunately I still only receive data from 1 of them.

This is the code for Central:

#include <SoftwareSerial.h>

SoftwareSerial BT1(3,2);
SoftwareSerial BT2(11,10);
#define LDR A0

int ValueLDR = 0;

void setup()
{
  Serial.begin(9600);
  pinMode(LDR, INPUT);
  BT1.begin(38400);
  BT2.begin(38400);
}

void loop()
{
  // sending LDR value
  ValueLDR = analogRead(LDR); 
  BT1.println(ValueLDR); 
  BT2.println(ValueLDR);
  //Serial.println(ValueLDR);

  // reciving data
  BT1.listen();
  delay(500);
  if (BT1.available() > 0) 
  {
    char BT1value = BT1.read();
    Serial.println("Value from BT1:");
    Serial.println(BT1value);
  }

  //delay(1000);

  BT2.listen();
  delay(500);
  if (BT2.available() > 0) 
  {
    char BT2value = BT2.read();
    Serial.println("Value form BT2");
    Serial.println(BT2value);
  }

}

Code for BT1:

#include <SoftwareSerial.h>

SoftwareSerial BT1(3, 2);

char myData[10];
int ValueLDR; 
int v1 = 1;  
int v0 = 0; 

void setup()
{
  Serial.begin(9600);
  BT1.begin(38400);
}

void loop()
{
  if (BT1.available()) 
 {
    // reciving LDR value
    byte m = BT1.readBytesUntil('\n', myData, 10); 
    myData[m] = '\0';
    ValueLDR = atoi(myData);  
    //Serial.println(ValueLDR); 

    // sending
    if (ValueLDR <= 500)
    {
      BT1.println(v0);
      Serial.println(v0);
    }
    else
    {
      BT1.println(v1);
      Serial.println(v1);
    }
    delay(100); 
 }
}

Code for BT2:

#include <SoftwareSerial.h>

SoftwareSerial BT2(3, 2);

char myData[10];
int ValueLDR; 
int v1 = 1; 
int v0 = 0; 

void setup()
{
  Serial.begin(9600);
  BT2.begin(38400);
}

void loop()
{
  if (BT2.available()) 
 {
    // reciving LDR value
    byte m = BT2.readBytesUntil('\n', myData, 10); 
    myData[m] = '\0';
    ValueLDR = atoi(myData); 
    //Serial.println(ValueLDR); 
    // sending
    if (ValueLDR <= 500)
    {
      BT2.println(v0);
      Serial.println(v0);
    }
    else
    {
      BT2.println(v1);
      Serial.println(v1);
    }
    delay(100); 
 }
}

My question is: is it possible to bypass the limitation of the SoftwareSerial() library by using the listen() function mentioned earlier?
If not I have an alternative solution, buying a second LDR sensor and mounting both of them directly on the BT1 and BT2 boards. Even so, I would love for the original plan to work.

Somebody else had doubts about having two SoftwareSerials going.

I wonder how it might work with another device, one with two UARTs. Or using one with a hardware UART and SoftwareSerial.
With a Leonardo you can keep SerialMon on the USB, use its Serial1 for one BT 'channel' and open SoftwareSerial for the other -
if that's the problem (and I don't know that it is).

If you used an LCD instead of SerialMonitor (to see your data), you could use the Uno UART on 0,1 and have one instance of SoftwareSerial. Maybe that would work - at least to find out if the two BT 'networks' can co-exist (without buying something else to find out).

Somebody posted this in another topic --
SoftwareSerial Library | Arduino Documentation