Trash Response from TX RX Between Uno and Mega

Hi.

Goal: Send a constant from one arduino board to another.

Connections: Uno Rx 0 pin to Mega Tx3 14 pin, Uno Tx 1 pin to Mega Rx 15 pin, Both boards powered by USB to computer.

Code:
Uno Board Sending the Constant :

int desiredRPM=1000;
int Norm_desiredRPM;
#include <SoftwareSerial.h>
SoftwareSerial mySerial1(0,1);

void setup() {
  Serial.begin(38400);
  mySerial1.begin(38400);
}

void loop() {
  //mySerial1.listen();
  Norm_desiredRPM=desiredRPM*.02125;//Sets value to 255 for 12000rpm and 0 for 0 rpm.
  mySerial1.write(Norm_desiredRPM);
delay(1000);
}

Mega Board Receiving the Constant:

#include <SoftwareSerial.h>
int Norm_desiredRPM;
int desiredRPM;
SoftwareSerial portOne(15,14);

void setup() {
  Serial.begin(38400);
  portOne.begin(38400);
  
}

void loop() {
  portOne.listen();
  Norm_desiredRPM = portOne.read();
  desiredRPM=Norm_desiredRPM/.02125;
  Serial.print("value received: ");
  Serial.println(Norm_desiredRPM);
  Serial.print("Processed: ");
  Serial.println(desiredRPM);
  delay(1000);
}

Output:
On the Serial monitor for the Mega, I am getting a value of 0. If I switch the boards so the Mega is sending the value to the Uno (I change the Rx Tx pin numbers, of course) I get what seems to be random values on the serial monitor for the Uno. I've looked at a bunch of examples and this seems like it should be really simple - just a read write situation, but nothing is working. I have my suspicions that since the 0,1 pins on the Uno are used to communicate with the computer via the USB that things are getting all messed up. Thoughts?

--Ian

You send an int from the Uno but only read one byte and display that
2)
You do not check if data has actually arrived using the available() method.
3)
Why do you use SoftwareSerial on the Mega if it has 3 additional serial ports available? Are they in use?
4)
Why do you use SoftwareSerial in the Uno code on the hardware serial port?

Please edit your post and for both codes
type
** **[code]** **
before the code
type
** **[/code]** **
after the code

So it will display in two code window

your uno code here
your mega code here

First, assume that I don't know things that are obvious to you, because I really don't know much about Arduino and programming in general.

  1. I send an int from the Uno that has a value between 0 and 255. I was under the assumption that this is covered by one byte and wouldn't be an issue.

  2. Does the data I send to the pin remain there? If so, shouldn't the value be available after I send it the first time since nothing is changing?

  3. I thought SoftwareSerial has to be used to establish the pins that you are using to send information between Arduinos. On the Mega, pins 0 and 1 will be in use, so I just picked the next ones over which were 14 and 15.

  4. Do I not have to use it on pins 0 and 1 since they are already established as serial ports?

Thanks for fixing up your post :wink:

I did not notice the write (instead of print). Sorry for that.
An int is two bytes; however write() only sends one byte. Because you only seem to have values from 0 to 255, I would change Norm_desiredRPM to a byte,

Once you call read(), one byte disappears from the buffer

3 and 4)
SoftwareSerial should only be used if you run out of serial ports. So on the Uno you have a possible need for it. But you should not use the same pins as HardwareSerial. On the Mega, you can use Serial3 (pins 14 and 15).

To solve the SoftwareSerial

On the Uno, you have two options
1)
Do not use SoftwareSerial; you don't seem to have a need (also it can be advisable to keep Srial available for debugging.

int desiredRPM = 1000;
byte Norm_desiredRPM;

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

void loop()
{
  Norm_desiredRPM = desiredRPM * .02125; //Sets value to 255 for 12000rpm and 0 for 0 rpm.
  Serial.write(Norm_desiredRPM);
  delay(1000);
}

Use SoftwareSerial on other pins (you have to rewire; below uses 2 and 3)

int desiredRPM=1000;
byte Norm_desiredRPM;
#include <SoftwareSerial.h>
SoftwareSerial mySerial1(2,3);  // RX,TX

void setup() {
  Serial.begin(38400);
  mySerial1.begin(38400);
}

void loop() {
  Norm_desiredRPM=desiredRPM*.02125;//Sets value to 255 for 12000rpm and 0 for 0 rpm.
  mySerial1.write(Norm_desiredRPM);
  delay(1000);
}

On the Mega, use Serial3 (pins 14 and 15)

int Norm_desiredRPM;
int desiredRPM;

void setup()
{
  Serial.begin(38400);
  Serial3.begin(38400);

}

void loop()
{
  Norm_desiredRPM = Serial3.read();
  desiredRPM = Norm_desiredRPM / .02125;
  Serial.print("value received: ");
  Serial.println(Norm_desiredRPM);
  Serial.print("Processed: ");
  Serial.println(desiredRPM);
  delay(1000);
}

Be aware that read will return -1 if nothing is available. You either need to check for that or user the available() method. The loop for the Mega can change to

void loop()
{
  if(Serial3.available() > 0)
  {
    Norm_desiredRPM = Serial3.read();
    desiredRPM = Norm_desiredRPM / .02125;
    Serial.print("value received: ");
    Serial.println(Norm_desiredRPM);
    Serial.print("Processed: ");
    Serial.println(desiredRPM);
    delay(1000);
  }
}

Hope this gets you on the way; if you insist on using an int (because values might be bigger than 255), you will need a slightly different approach.

Thanks!

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data. There is also a parse example.

...R