0

So I have two Arduinos (Arduino Nano) and I'm trying to get them to communicate via SoftwareSerial. I have the 'sending' Arduino's D10 pin connected to the 'recieving' Arduino's D11 pin, and vice versa. On my sending Arduino, I have the following code:

#include <SoftwareSerial.h>
SoftwareSerial mySerial(10, 11);

void setup() {
  pinMode(2, OUTPUT);
  mySerial.begin(9600);
}

void loop() {
  if (mySerial.isListening()){
    digitalWrite(2, HIGH);
  }
  mySerial.write(5);
}

and on my receiving Arduino, I have:

#include <SoftwareSerial.h>
SoftwareSerial mySerial(11, 10);

void setup() {
  pinMode(2, OUTPUT);

  Serial.begin(9600);

  mySerial.begin(9600);
}

void loop() {
  mySerial.listen();
  if (mySerial.available()>0) {
    digitalWrite(2, HIGH);
    char inByte = mySerial.read();
    Serial.write(inByte);
  }
  digitalWrite(2, LOW);
}

(with D2 being hooked up to an LED for both).

I can't get it to read anything. Nothing seems to be available for the receiving Arduino (the LED is off). What am I doing wrong?

1

Sender:

SoftwareSerial mySerial(10, 11);  // Rx, Tx

Receiver:

SoftwareSerial mySerial(11, 10);  // Rx, Tx

I have the 'sending' Arduino's D10 pin connected to the 'receiving' Arduino's D11 pin, and vice versa.

So you have the sending Arduino's Rx connected to the receiving Arduino's Rx. Therefore it won't work. You need to connect Rx to Tx.


  mySerial.listen();

You don't need that in this simple example.

-1

I agree with Nick above but want to add this simplify your code like this:


if (mySerial.available()) {
    digitalWrite(2, HIGH);    
    Serial.write(mySerial.read());
  }

I have had to catch serial data from a printer feed before and this turned out to be the best way to catch all the data if you place it in a variable first it has to change the variable every time that you receive a byte but this way it places all in a buffer then sends it out

BTW I had to save to a F-RAM device and that worked the same way but instead of sending it I had it saved to a variable while using ReadString() in stead of read().

  • Re “if you place it in a variable first it has to change the variable every time that you receive a byte”: A local variable, used like in the original post, will be allocated by the compiler to a CPU register. What you are proposing is not more efficient. – Edgar Bonet Jan 4 at 20:38
  • like i said in the answer at the bottom I used a variable but nothing else and that is what made it work for simplifying the code to get it to work on the first go use the simplest version you can get then build on that. – Tjaart van aswegen Jan 4 at 20:44
  • I don't quite understand what you are writing, but the sentence starting with “if you place it in a variable first” sounds like you imply that using a temporary variable is a cause of inefficiency. If that is what you mean, it is wrong. If it's not what you mean, then you should edit the answer in order to clarify the sentence. – Edgar Bonet Jan 4 at 21:22
  • no maybe my language is really letting me down to put it in simplest terms you should try to minimize the processes happening on a serial event and you should keep away from delays and all of that and if you are purely trying something out do it in the simplest way available being the one i suggested (i believe but could be wrong) and then build on to that it just makes debugging a breeze and not a schlep – Tjaart van aswegen Jan 4 at 21:39
  • as for the variable I found it to be a good solution by itself having nothing else keeping the mcu busy while receiving data not saying that is the only function that your MCU should have but rather in the data receive event that should be kept to the minimum and that you should not even try something like a delay in there and once again keep your code simple and straight forward – Tjaart van aswegen Jan 4 at 21:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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