Serial connection betwen two arduinos sending measurments

Hello All

I have 2 floats and have to send them via serial port of a nano every (tx) to a port of the mega (rx).
Here i have some issues with the way is transmitted

Here an explanation of the system:

We have a system of sensors which have to be powered by batteries due to noise, and have to be powered 24/7, so monitoring on those batteries is needed. To avoid noise, there is an isolation in the comunication between the battery sensors and the relays to switch power and recharge, so we have an arduino mega conected in serial to activate the relays, which receives the battery values from an arduino nano every.

So int the project we have to read some battery values ( with an adc1115 we get values from a voltage divider and a current sensor), which are receivde by an arduino nano every, and they are sent via serial to an arduino Mega (to measure more than one battery), which activates the relays.

My current code in the nano is the following mix of the ADC with serial.prints.

String voltio;
String amperio;
int contador = 0;
// Crear objeto de la clase
Adafruit_ADS1115 ads;

// Factor de escala. Por defecto el valor de referencia es 6,144V
// El factor de escala de esta referencia es de 0,1875mV
float factorEscala = 0.1875F;

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

  // Factor de escala
  ads.setGain(GAIN_TWOTHIRDS);

  // Iniciar el ADS1115
  ads.begin();
}

void loop(void)
{
  // Obtener el diferencial
  short diferencia_0_1 = ads.readADC_Differential_0_1();
  short diferencia_2_3 = ads.readADC_Differential_2_3();
  // Convertir en voltios
  float volt = (diferencia_0_1 * factorEscala) / 1000.0;
  float amp = (diferencia_2_3 * factorEscala) / 1000.0;


  if (contador % 2 == 0) {

    // String volts = String(volt, 6);
    //dtostrf(volt,5,3,char buffer [7]);
    //Serial.print("v");
    //Serial.print(",");
    Serial.print(volt);
    Serial.write("\n");
   


  }
  else {

  
    Serial.print("i");
    Serial.print(",");
    Serial.print(amp);

I've been trying different solutions to try to serialwrite or serial.print, but the only way I can see a value in the monitor is with the serial.print(FloatValue), but i'm not sure if serial.print also sends a signal via TX pin.

In addition, here is the code of the Mega

float volt;

String Missatge;
void setup()
{
  Serial.begin(9600);
  pinMode(12, OUTPUT);
  pinMode(13, OUTPUT);
}
void loop()
{


  if (Serial.available())
  {

    //volt = Serial.parseFloat(SKIP_ALL,'\n');//
    // String Missatge = Serial.readString();
    // String Missatge =Serial.readStringUntil("\n");
    float volt = Serial.parseFloat();
    Serial.print(volt);
    Serial.print("\n");


  }

}

where I try to read as a float directly, but is continuosly showing 0.

This means that there is some issue with the sending of the float vya serial.

Note: right now i'm connecting the arduino and mega with a wire TX(NANO)-RX(MEGA) and a ground.

I would suggest to study Serial Input Basics to understand the fundamentals of Serial transmission

If you want to use parseFloat, just print the variable to serial instead of using write.

floats are four bytes long.

... but you're sending it as raw binary, not the ASCII parseFloat expects.

it already tried to, changing the code to the following:
Nano:

    Serial.print(volt);
    Serial.write("\n");

With this code in the Mega:

    float volt = Serial.parseFloat();
    Serial.print(volt);
    Serial.print("\n");

Gives no output to the monitor serie of the mega, so that means that there is an issue with the serial output in the nano, otherwise the serial.available loop would display 0, . The print does not send a message via TX maybe?

You need to connect the TX from the nano to one of the additional hardware serial port RX on the mega.

https://www.arduino.cc/en/Tutorial/BuiltInExamples/MultiSerialMega

Here is a demo of communictation from an Nano to a Mega of 2 float numbers using the methods from Robin2's serial input basics tutorial (linked in reply #2). The code uses the receive with end markers example and the uses the strtok() function to parse the data from the received string and the atof() function to convert the string data to floats. Connect the Nano RX (pin 0) to the Mega TX1 (pin 18) and Nano TX (pin 1) to Mega RX1 (pin 19) and Nano ground to Mega ground. You must disconnect the Nano serial wires from the Mega to upload code to the Nano . The code was tested on an Uno connected to a Mega.

Nano code (sender)

float number_1 = 3.14159;
float number_2 = 1234.56789;

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

void loop()
{
   static unsigned long timer = 0;
   unsigned long interval = 1000;
   if (millis() - timer >= interval)
   {
      timer = millis();
      Serial.print(number_1, 6);
      Serial.print(',');
      Serial.println(number_2, 6);
   }
}

Mega code (receiver)

// by groundfungus aka c. goulding

const byte numChars = 17;
char receivedChars[numChars];   // an array to store the received data

float number_1 = 0.0;
float number_2 = 0.0;

boolean newData = false;

void setup()
{
   Serial.begin(115200);
   Serial1.begin(115200);
   Serial.println("<Arduino is ready>");
   Serial.println("send two float numbers separated by a comma");
   Serial.println("data must be terminated with a line feed (\'\\n\')");
}

void loop()
{
   recvWithEndMarker();
   parseData();

}

void recvWithEndMarker()
{
   static byte ndx = 0;
   char endMarker = '\n';
   char rc;

   while (Serial1.available() > 0 && newData == false)
   {
      rc = Serial1.read();
      //Serial.print(rc);
      if (rc == '\r')
      {
         return;
      }
      if (rc != endMarker)
      {
         receivedChars[ndx] = rc;
         ndx++;
         if (ndx >= numChars)
         {
            ndx = numChars - 1;
         }
      }
      else
      {
         receivedChars[ndx] = '\0'; // terminate the string
         ndx = 0;
         newData = true;
      }
   }
}

void parseData()
{
   char *strings[10];
   char *ptr = NULL;
   if (newData == true)
   {
      byte index = 0;
      ptr = strtok(receivedChars, ",");
      while (ptr != NULL)
      {
         strings[index] = ptr;
         index++;
         ptr = strtok(NULL, ",");
      }
      /*
      //Serial.println(index);
      // the separate pieces
      for (int n = 0; n < index; n++)
      {
         Serial.print("piece # ");
         Serial.print(n);
         Serial.print("  ");
         Serial.println(strings[n]);
      }
      */
      Serial.print("\nfloat number one = ");
      number_1 = atof(strings[0]);
      Serial.print(number_1, 4);
      Serial.print("  float number two = ");
      number_2 = atof(strings[1]);
      Serial.println(number_2, 4);
      newData = false;
   }
}