Communicate 2 arduinos using Serial

Hello once again Arduino lovers,
I have asked something similar to the past but now I have gained some knowledge on the topic.

So I have two arduinos one uno and one mega. The arduino UNO sends messages in that way below as shown in the code (why is that? because of a legacy controller sends exactly the same way the data as im emulating from uno)

So now the Mega I’m planning to use it as a rs232 to canbus converter but thats for the future. Now as you can see from the arduino MEGA code I definely receive the floats, can someone help me if i can do something to the mega and separate those values that i get and store them to different variables ??

Love you all have a nice day

// BEGIN CODE ON ARDUINO UNO

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

void loop(){
Serial.println("<");
Serial.println("FuelStatus");
Serial.println("1.4");
Serial.println("BatteryLevel");
Serial.println("2.9");
Serial.println("AIR LEVEL");
Serial.println("3.14");
Serial.println(">");

delay(100);

}

// BEGIN CODE ON ARDUINO MEGA

// RS232 TO CANBUS


float A;
float B;

void setup()
{
Serial.begin (9600);
Serial1.begin(9600);
}
void loop()
{
int incoming = Serial1.available();

if (incoming > 0)
{

A = Serial1.parseFloat(); // reads floats as floats rather than ASCI
// anything else returns 0
B = A;
Serial.println (B,2);


}
}

Please follow the advice on posting code in Read this before posting a programming question

In particular note the advice to Auto format code in the IDE and using code tags when posting code here

Thanks for your help It looks much better now :slight_smile:

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

If the sole purpose is to exchange data between two Arduinos you may find the I2C is more versatile. Have a look at this Arduino to Arduino I2C Tutorial

…R

If you decide to stay with serial, use a software serial port n the Uno to talk with one of the extra hardware serial ports of the Mega (Serial1, Serial2 or Serial3). Then you can use the (USB) hardware serial ports to upload code and monitor program flow.

SerialTransfer.h makes serial comms between 2 arduino super fast and easy (even available for SPI and I2C)

So I took at the link you guys provided and I believe example 5 here is what I need

Thanks for your help

Have a nice day