Send integer with Mega to Uno, and see on Uno's serial monitor?

// Send
//#include <SoftwareSerial.h>
//SoftwareSerial mySerial(7, 11); // RX, TX

String incoming = "";

void setup()
{
  pinMode(14, OUTPUT);
  
  Serial.begin(19200);  // serial monitor
  Serial2.begin(9600);  // connection
  delay(500);

}

void loop()
{
  Serial.flush(); // Waits for the transmission of
                  // outgoing serial data to complete
  if (Serial.available() > 0)
  {
    incoming = Serial.readString();
//    Serial.println(incoming);   // comment out after test
    Serial2.println(incoming);
  }
}

I just want to view the integer sent plugged into the serial monitor of the first Arduino Mega display on the second Arduino, the Uno, in its serial monitor:

// Receive 
#include <SoftwareSerial.h>
SoftwareSerial mySerial(9,10); // RX, TX

int integerValue = 0;

void setup()
{
  Serial.begin(19200);  // serial monitor
  mySerial.begin(9600); // connection
}


void loop() {
  delay(500);
  if (mySerial.available() > 0)
  {
     integerValue = mySerial.read();
     
     Serial.print("Data Recieved on Pin 11: ");
     Serial.println(integerValue);
     Serial.println();      
  }
}

Any suggestions on what I’m doing wrong?

You haven't actually described the problem you are having. Are you receiving anything? Are you receiving the wrong value?

The immediate problem I see is you are transmitting a string but expecting to receive an integer. Every time through loop() on your receiver you will be overwriting integerValue with an ASCII character.

Also you don't need the delay in your receiver.

Try this for your receiver:

// Receive
#include <SoftwareSerial.h>
SoftwareSerial mySerial(9,10); // RX, TX

void setup()
{
  Serial.begin(19200);  // serial monitor
  mySerial.begin(9600); // connection
}


void loop() {
  if (mySerial.available() > 0)
  {
     Serial.print(mySerial.read());
  }
}

Thank you. Now I’m getting something (the ASCII value).

ToddL1962:
The immediate problem I see is you are transmitting a string but expecting to receive an integer. Every time through loop() on your receiver you will be overwriting integerValue with an ASCII character.

Is there a way to get the original character? Preferably as an integer? (Numbers are the only characters that will be sent.)

Try this

PerryBebbington:
Try this
https://forum.arduino.cc/index.php?topic=396450.0

Example 6? So you're basically saying there is no simple solution, and that roughly 50 lines of code need to be added to simply convert an ASCII value to integer? I'll just do nine if-else statements instead.

adamelli:
Example 6? So you're basically saying there is no simple solution, and that roughly 50 lines of code need to be added to simply convert an ASCII value to integer? I'll just do nine if-else statements instead.

There are 128 possible ASCII characters. 10 of those are common numbers 0-9. But you are only going to look at 9 of them?Aer you going to assume if the character is not 0-8, it must be 9?
Paul

adamelli:
Thank you. Now I'm getting something (the ASCII value).

Is there a way to get the original character? Preferably as an integer? (Numbers are the only characters that will be sent.)

Not sure exactly what you want. Let's say you enter the string "123" into the serial monitor of the Mega. Do you want to send the integer 123 to the Arduino or the string "123" to the Arduino? Also, what is the maximum integer you will be inputting? Will you be supporting negative integers?

All of these questions affect how the integer is converted, transmitted, and received.

I meant that it is an excellent tutorial about transferring data using serial. I am suggesting you study it, learn from it and use what you learn to solve your problem.

I can't give you (or anyone else) any better example than are in that tutorial.

I have read that tutorial, several times before actually. I just don’t see how it’s relevant to my specific question. Maybe I need to read it a third time, idk.

Only single integers 1-6 are being sent. This is part of someone else’s larger program, and I’m just trying to develop a method to emulate sending an integer. (That way, I can debug their program.)

Ok, well, over to someone else then, hopefully more helpful than me.

Only single integers 1-6 are being sent. This is part of someone else’s larger program, and I’m just trying to develop a method to emulate sending an integer. (That way, I can debug their program.)

If you are getting the ASCII value of the single digit then subtract 48 from it to turn it into the actual digit

adamelli:
Only single integers 1-6 are being sent. This is part of someone else's larger program, and I'm just trying to develop a method to emulate sending an integer. (That way, I can debug their program.)

Gotcha. So you need to determine if their program is expecting an ASCII '1' - '6' or a binary 1 - 6.

Thank you for pointing it out though.

void loop()
{
  if(mySerial.available() > 0)
  {
    readAscii = mySerial.read();
//    Serial.println(readAscii);
    if(readAscii == "49")
    {
      integerValue = 1;
      return;
    }
    if(readAscii == "50")
    {
      integerValue = 2;
      return;      
    }
    if(readAscii == "51")
    {
      integerValue = 3;
      return;      
    }
    if(readAscii == "52")
    {
      integerValue = 4;
      return;      
    }
    if(readAscii == "53")
    {
      integerValue = 5;
      return;     
    }
    Serial.println(integerValue);    
  }
}

Each integer is happening three times. If 2 is sent, it prints
2
2
2

I would like it to occur only once (since only one integer is sent at a time).

If you are getting the multiple digits of a larger integer then subtract 48 from each as they are received, multiply the total so far (starting at 0) by 10 and add the newly received digit until you have received all of the digits

You will, of course, need to know which is the first and last digit of the integer. Markers would help as in Robin's tutorial

Simple solution

// Receive 
#include <SoftwareSerial.h>
SoftwareSerial mySerial(11,7); // RX, TX
String readAscii = "";
int integerValue = 0;
bool latch = false;

void setup()
{
  Serial.begin(19200);  // serial monitor
  mySerial.begin(9600); // connection
}

void loop()
{
  if(mySerial.available() > 0)
  {
    readAscii = mySerial.read();
//    Serial.println(readAscii);
    if(readAscii == "49")
    {
      integerValue = 1;
      latch = true;
    }
    if(readAscii == "50")
    {
      integerValue = 2;
      latch = true;      
    }
    if(readAscii == "51")
    {
      integerValue = 3;
      latch = true;      
    }
    if(readAscii == "52")
    {
      integerValue = 4;
      latch = true;      
    }
    if(readAscii == "53")
    {
      integerValue = 5;
      latch = true;    
    }
    
    if(latch == true)
      Serial.println(integerValue);

    latch = false;
  }
}

The way you have that coded you will never print integerValue because you return as soon as you assign it. Here is a much simpler way:

void loop()
{
  if(mySerial.available() > 0)
  {
    integerValue = mySerial.read() - '0';
    Serial.println(integerValue);    
  }
}

Or to be safe:

void loop()
{
  if(mySerial.available() > 0)
  {
    char readVal = mySerial.read();
    if (isdigit(readVal))
    {
       integerValue = readVal - '0';
       Serial.println(integerValue);
    }
  }
}

adamelli:
Simple solution

// Receive 

#include <SoftwareSerial.h>
SoftwareSerial mySerial(11,7); // RX, TX
String readAscii = “”;
int integerValue = 0;
bool latch = false;

void setup()
{
  Serial.begin(19200);  // serial monitor
  mySerial.begin(9600); // connection
}

void loop()
{
  if(mySerial.available() > 0)
  {
    readAscii = mySerial.read();
//    Serial.println(readAscii);
    if(readAscii == “49”)
    {
      integerValue = 1;
      latch = true;
    }
    if(readAscii == “50”)
    {
      integerValue = 2;
      latch = true;     
    }
    if(readAscii == “51”)
    {
      integerValue = 3;
      latch = true;     
    }
    if(readAscii == “52”)
    {
      integerValue = 4;
      latch = true;     
    }
    if(readAscii == “53”)
    {
      integerValue = 5;
      latch = true;   
    }
   
    if(latch == true)
      Serial.println(integerValue);

latch = false;
  }
}

This will not work because mySerial.read() reads a single byte. That byte will be the ASCII value of the character ‘1’ thru ‘6’ entered in the Mega. Which can be decimal 49 thru 53.

I think you are confused about binary vs ASCII and the binary value of ASCII characters.

It worked, but I like your elegant solution better. Full sketch:

// Receive 
#include <SoftwareSerial.h>
//              receive pin11 (7 does nothing)
SoftwareSerial mySerial(11,7); // RX, TX
int integerValue = 0;

void setup()
{
  Serial.begin(19200);  // serial monitor
  mySerial.begin(9600); // connection
}

void loop()
{
  if(mySerial.available() > 0)
  {
    char readVal = mySerial.read();
    if (isdigit(readVal))
    {
       integerValue = readVal - '0';
       Serial.println(integerValue);
    }
  }
}