Combine two float separated into a single float separated by a comma

I want to combine GPS latitude and longitude into a single float separated by a comma but the code shows an error

#include <Arduino.h>
#include <TinyGPS++.h>
#include <SoftwareSerial.h>

int RXPin = 2;
int TXPin = 3;
float Latitude;
float Longitude;

TinyGPSPlus gps;
SoftwareSerial SerialGPS(RXPin, TXPin);
 
float SensorData,KalmanFilterData,SensorData2,KalmanFilterData2;
float Xt,Xt_update,Xt_prev,Xt2,Xt_update2,Xt_prev2;
float Pt,Pt_update,Pt_prev,Pt2,Pt_update2,Pt_prev2;
float Kt,R,Q,Kt2,R2,Q2;
 
void setup() {
  Serial.begin(9600);
    while (!Serial) {
    ; 
  }
  SerialGPS.begin(9600);
  R=100;
  Q=1;
  Pt_prev=1;
  
}

void obtain_data()
{
  if (gps.location.isValid())
  {
    
    Latitude = gps.location.lat();
    Longitude = gps.location.lng();
    /*Serial.print(Latitude);
    Serial.print(",");
    Serial.println(Longitude);*/
    SensorData = Latitude + "," Longitude;
    Xt_update = Xt_prev;
    Pt_update = Pt_prev + Q;
    Kt = Pt_update/(Pt_update+R);
    Xt = Xt_update + (Kt*(SensorData-Xt_update));
    Pt = (1 - Kt)*Pt_update;

    Xt_prev = Xt;
    Pt_prev = Pt;

    KalmanFilterData = Xt;

    Serial.print(SensorData,6);
    Serial.print(",");
    Serial.print(KalmanFilterData,6);
    Serial.println();
  }
  else
  {
    Serial.println("Location is not available");
  }
}

void loop() {
  while (SerialGPS.available() > 0 )
    if (gps.encode(SerialGPS.read()))
      obtain_data();
  if (millis() > 5000 && gps.charsProcessed() < 10)
  {
    Serial.println("GPS NOT DETECTED!");
    while(true);
  }
 }

this the error message

invalid operands of types 'float' and 'const char [2]' to binary 'operator+'

What do you expect from this?

1 Like

i want the latitude and langitude only on single float

You can't. A comma can be a valid part of a float number (in binary) so can't be (reliably) used as a separator. The only way to combine them is to convert each float to text and next combine them adding the comma in between.

Or you can simply sum them.

Now the question is why you want to do what you want to do?

1 Like

do you mean on a single line: 39.6659213,-78.8156087 ?

1 Like

what would it mean mathematically ?

may be you want a string?

1 Like

I doubt that based on the below

1 Like

ah yes... I had seen only Serial.print(SensorData,6);

1 Like

If you want to use composite values, like geo coordinates, you can use a struct. But you have to write your own functions to operate on them, because they're not defined in the language

// data structure to store geographical position
struct Position_t
{
  float lat = 0;
  float lon = 0;
};

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.