Software Serial issue on Mega ADK and Leonardo

I am working on a project using an Arduino ADK and an Arduino Leonardo.

I am trying to get the 2 processors to talk using Software Serial.

The ADK is my primary processor and will just be transmitting data. The Leonardo will be the secondary and will be receiving data only.

I need the pins to line up between the ADK and the Leo (example pins 8 and 9 on both the ADK and Leo).

I know the Leo can use pins 8 and 9 as the RX pin on software serial, and that pins 8 and 9 are not on the ADK, but like I said, the ADK is TX only.

I have all the code setup, but the data I am getting is not what I expect.

The ADK setup is:

#include <SoftwareSerial.h>

SoftwareSerial commPort(9,8);

uint8_t msgData = 0;

void setup() {
  commPort.begin();
)

void loop{
  if ("certain stuff"){
    commPort.print(msgData);
  }
}

There is extra code to set the value of msgData and when to transmit. I have put in debug code in the sketch, and the ADK sends the data fine.

Now to the Leo.

#include <SoftwareSerial.h>

SoftwareSerial commPort(8,9);

uint8_t msgRecv = 0;

void setup() {
  commPort.begin();
)

void loop{
  if (commPort.available()) {
    msgRecv = commPort.read();
  }
}

Now when I send a 1 from the ADK to the Leo, I get 192. When I send a 2, I get 230. 3 gets 230, and 4 gets 240. Mostly. Sometimes the received number is different, but that is the typical.

I have no idea why my data received is so far off from the data sent.

I have tried using commPort.print(), commPort.println(), and commPort.write() in the ADK. All 3 I get incorrect data in the Leo.

I don't see the part of the sketch which is writing to the Serial Monitor.

I don’t know the MegaADK - doesn’t it have spare HardwareSerial ports like a regular Mega. And the Leonardo definitely has a spare HardwareSerial port. So why use SoftwareSerial?

Have a look at the examples in Serial Input Basics - simple reliable non-blocking ways to receive data. There is also a parse example to illustrate how to extract numbers from the received text.

The technique in the 3rd example will be the most reliable. It is what I use for Arduino to Arduino and Arduino to PC communication.

You can send data in a compatible format with code like this (or the equivalent in any other programming language)

Serial.print('<'); // start marker
Serial.print(value1);
Serial.print(','); // comma separator
Serial.print(value2);
Serial.println('>'); // end marker

By the way you can also use those techniques with SofwareSerial

…R

ieee488:
I don't see the part of the sketch which is writing to the Serial Monitor.

For the purpose of the post, I didn't include the serial monitor code. The ADK sketch is rather large and I just posted the stripped down version.

Robin2:
I don't know the MegaADK - doesn't it have spare HardwareSerial ports like a regular Mega. And the Leonardo definitely has a spare HardwareSerial port. So why use SoftwareSerial?

I have been using that plus other examples trying to get this to work.

Yes, you are correct. The ADK has 3 additional hardware serial ports, just like the Mega 2560. I did not know the Leonardo had a second as well.

However, the reason I am fairly married to using pins 8 and 9 on the ADK and the Leo is because I am stacking them. I purchased a Leo without headers and I have installed pass through headers on it. So now the Leo is acting like a shield to the ADK. I'm doing this due to space limitations.

So if I can make pins 8 and 9 work, I don't have to do any additional wiring. Just reverse the pins in SoftwareSerial.

If push comes to.shove, I may end up routing wires from Serial1 on the ADK to another serial port on the Leo.

On the Leonardo pins 0 and 1 are Serial1

…R

Robin2:
On the Leonardo pins 0 and 1 are Serial1

...R

But if I use 0 & 1, will I'll have to disconnect the ADK from the Leo when I do a sketch update?

vader7071:
But if I use 0 & 1, will I'll have to disconnect the ADK from the Leo when I do a sketch update?

No. That's the whole point. The USB connection is Serial not Serial1

On an Uno and Mega pins 0 and 1 are used for Serial.

...R

Oh, I missed the "One" on the end of Serial.

Well, I have ended up using Serial3 on the ADK and software serial on the Leo. Ended up this way because of wiring.

I also figured out part of the reason I was having issues with the data sent from the ADK and why it just didn't seem correct.

I had forgotten I had assigned pins 8 and 9 to control servos. So I was trying to write serial data and control analog PWM servos off the same pins. Another reason I abandoned the ADK SoftwareSerial and went to Serial3.