Sending an Integer via the TX1 pin of Arduino Mega2560

I am currently using an Arduino Mega2560 to send an integer value of 0-255 from a potentiometer to another Arduino Mega2560 through the TX1 and RX1 pins. Right now, I have the transmitting Mega sending the value through to the Serial Monitor and it works great. I can see the value of "potVal" on the Serial Monitor when I pull it up. But when I try sending the value through the TX1 pin using "Serial1.write(potVal)" , I can tell its not transmitting because the builtin TX LED on the Mega doesn't light up. I have looked everywhere on Google and the forums and can't seem to figure out what the problem is. I have a feeling it has something to do with my code because I think I have to modify the integer to send it serially? I have fairly little experience with serial communication so I don't really know where to start but here is the code that I have started with:

int potPin = A0;
int potVal = 0;

void setup()
{
//Serial1.begin(9600);
Serial.begin(9600);
pinMode(potPin, INPUT);
}

void loop()
{
potVal = analogRead(A0);
potVal = map(potVal, 0, 1023, 0, 255);
Serial.println(potVal);
//Serial1.write(potVal);
delay(1000);
}

PE0 and PE1 (RX0 and TX0 respectively, pins '0' and '1') are connected to the ATMEGA16 used as the USB bridge on the Mega; the bridge is what controls the RX and TX LEDs you see.

IOW, there are no LEDs on the Mega for the other serial ports.

As for sending an int, keep in mind that in Arduino-land, an int is a 16-bit (two byte) value.

You'll need to send two bytes to write an int out. Something like this:

int
    nVal;

    .
    .
    .
    nVal = 998;  //assign some value

    .
    .
    .
    Serial1.write( (nVal >> 8) & 0xff ); //send the high byte
    Serial1.write( nVal & 0xff );  //send the low byte
    .
    .
    .

e374170:
int potPin = A0;
int potVal = 0;

void setup()
{
//Serial1.begin(9600);
Serial.begin(9600);
pinMode(potPin, INPUT);
}

void loop()
{
potVal = analogRead(A0);
potVal = map(potVal, 0, 1023, 0, 255);
Serial.println(potVal);
//Serial1.write(potVal);
delay(1000);
}

It looks like you have "commented" out the serial1.write instruction with the \ ?

But when I try sending the value through the TX1 pin using "Serial1.write(potVal)" , I can tell its not transmitting because the builtin TX LED on the Mega doesn't light up.

Have you tried to read the potVal on the other Mega? What code did you use? What did you see? How were the two Mega's connected?

"Right now, I have the transmitting Mega sending the value through to the Serial Monitor and it works great."

Then that part is working. How do you have the wires between the two arduinos connected? What are you using on the receiving arduino to receive the data and display it in the receiving arduino's serial monitor. If you want to try a simple experiment, connect the grounds between the two boards. Connect a wire between the tx0 on both boards. open the serial monitor for both boards and see if the same thing is displayed on both serial monitors. Below is how you post your code.

To put your code in a code box, use the </> icon in the far left of the post tool bar and paste your code between the two bracket sets that appear.

To go back and put your code in a code box, in the bottom right of your post, select "more" and click modify. When the modify post opens, high light your code and click the </> in the far left of the post tool bar. This will put you code in code brackets. Then save the changes.

Remember .write is sending the binary values of the number,
while .print sends the ASCII text value.

1. The connection diagram between MEGA and NANO.
uartsms-meganano.png

2. The sketches:
MEGA Codes (Transmit):

int potPin = A0;
int potVal = 0;

void setup()
{
  Serial.begin(9600);
  Serial1.begin(9600);
  //pinMode(potPin, INPUT);
}

void loop()
{
  potVal = analogRead(A0);
  //potVal = map(potVal, 0, 1023, 0, 255);
  Serial.println(potVal);
  Serial1.write(highByte(potVal));
  Serial1.write(lowByte(potVal));
  delay(1000);
}

NANO/UNO (Receive) Codes:

#include<SoftwareSerial.h>
SoftwareSerial SUART(2, 3); //SRX =2, STX = 3

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

void loop()
{
  byte n = SUART.available();
  if (n == 2)
  {
    byte x = SUART.read();
    int y = x << 8 | SUART.read();
    Serial.println(y);
  }
}

3. Receiver Screen shot (test signal: 5V and 3.3V at MEGA)
sm24.png

uartsms-meganano.png

sm24.png