1

So I have been stuck with this for quite some days now and am really unable to put my finger on what's wrong here. Any help would be great. Try not to list down the disadvantages of SoftwareSerial, I know about them. Is this issue happening cause of it?

What I am trying to achieve? Two Arduino (Nanos) are connected to each other and are trying to communicate with each other via SoftwareSerial UART (multiple Arduinos are connected to multiple Arduinos but for the sake of keeping the code minimalistic, assume one-to-one communication).

Code for Arduino 1:

#include <SoftwareSerial.h>

String val = "4";

SoftwareSerial a(6, 7); // rx, tx
SoftwareSerial b(8, 9); // rx, tx
SoftwareSerial c(10, 11); // rx, tx
SoftwareSerial d(12, 13); // rx, tx

String a_s, b_s, c_s, d_s;

void setup() {
  a.begin(9600);
  b.begin(9600);
  c.begin(9600);
  d.begin(9600);
  Serial.begin(9600);
  a_s = "";
  b_s = "";
  c_s = "";
  d_s = "";
}

void loop() {
  delay(100);

  d.print(val);

  if(d_s == "") {
    if(d_s == "" && d.available()) {
      char rd = d.read();
      Serial.print("ddddddddddd ");
      Serial.println(rd);
      d_s = rd;
    }
  }  
}

Code for Arduino 2:

#include <SoftwareSerial.h>

String val = "5";

SoftwareSerial a(6, 7); // rx, tx
SoftwareSerial b(8, 9); // rx, tx
SoftwareSerial c(10, 11); // rx, tx
SoftwareSerial d(12, 13); // rx, tx

String a_s, b_s, c_s, d_s;

void setup() {
  a.begin(9600);
  b.begin(9600);
  c.begin(9600);
  d.begin(9600);
  Serial.begin(9600);
  a_s = "";
  b_s = "";
  c_s = "";
  d_s = "";
}


void loop() {
  delay(100);

  b.print(val);
  
  if(b_s == "") {
    if(b.available()) {
      char rd = b.read();
      Serial.print("bbbbbbbbbbbb ");
      Serial.println(rd);
      b_s = rd;
    }
  }
}

Actual and Expected output for Arduino 1: ddddddddddd 5

Actual output for Arduino 2: (no output!!)

Expected output for Arduino 2: bbbbbbbbbbbb 4

While debugging, I realized if I comment out c.begin(9600) and d.begin(9600) from the code for Arduino 2, the actual output is the same as the expected output.

//   c.begin(9600);
//   d.begin(9600);

Ohh and yes, the connections are as follows:

ard1 ard2
GND GND
D12 D9
D13 D8

Both the Arduinos are connected (and hence powered) via individual cables to the same laptop. Is there something I am missing out on? I know SotfwareSerial has its downsides but is this something SoftwareSerial is causing? If yes, why is the actual output of Arduino 1 is same as the expected output? Shouldn't it have the same effect on both the Arduinos?

TIA!

1 Answer 1

1

Is one of the downsides of SoftwareSerial that you know about the fact that only one SoftwareSerial can be set to receive data at a time?

You have to select one and only one instance to receive data using the .listen() method.

If you want to be able to receive from multiple devices at once you will have to use either hardware UARTs or a single SoftwareSerial and a "bus" infrastructure (RS485 or similar).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.