Latitude and Longitude Values as Float

Hi, i want to convert my latitude and longitude values to float variables from NMEA. This is my code;

#include <SoftwareSerial.h>
#define NMEA_SIZE 256
 
SoftwareSerial GPS = SoftwareSerial(6, 1);
 
byte NMEA[NMEA_SIZE];
 
void getData() {
 
  byte character;
  int index = 0;
   
  do {
    if (GPS.available()) {
      character = GPS.read();
      NMEA[index] = character;
      index++;
    }
  } while(index < NMEA_SIZE && character != '

And this is the output;

GPRMC,010224.657,A,4000.0833,N,03237.4580,E,0.97,257.49,011213,,,A*6C<END>
GPRMC,010225.657,A,4000.0834,N,03237.4580,E,0.75,64.26,011213,,,A*5D<END>
GPRMC,010226.657,A,4000.0839,N,03237.4587,E,2.27,91.28,011213,,,A*55<END>

If you have any idea about converting float variables like “float latitude” from byte “NMEA”, i will be very happy. Thank you.);
 
  NMEA[index - 2] = ‘\0’;
}

void setup() {

GPS.begin(4800);
  Serial.begin(9600);
 
  int j;
  for(j = 0; j<15; j++){
  getData();
 
  if(NMEA[17] == ‘V’)
  {
    Serial.print(“NO DATA\n”);
}

else if(NMEA[2] == ‘R’ && NMEA[3] == ‘M’ && NMEA[4] == ‘C’) {
    int i = 0;
    for(i = 0; NMEA[i] != ‘\0’; i++) {
     
      Serial.write(NMEA[i]);

}
   
    Serial.print("\n");
  }
  }
}

void loop() {

}


And this is the output;

§DISCOURSE_HOISTED_CODE_1§


If you have any idea about converting float variables like "float latitude" from byte "NMEA", i will be very happy. Thank you.

There is a library to decode NMEA sentences (probably several libraries) and quite likely one of these libraries enables you to obtain lat/long values as floating point degrees or in a format that would be easy to convert to floating point degrees.

Note that the float in arduiono is a IEEE754 32 bit, having only ~7 significant digits.
a value like “03237.4587” has 8 so cannot be represented correctly with a float, and Arduino does not support double…

Consider using longs which have 9 digits of precision, or split the value in the integer part and fractional part or use less digits.
(Yes this will affect all your math)

I can take some digits of minute data like;

float lat_degree= 40
float lat_minute=25.48

I don't want to use special library for that, i just need to convert "byte" to "float"
I don't need all numbers. Do you have any idea to take numbers from "NMEA" array to float numbers?

globieai:
Do you have any idea to take numbers from "NMEA" array to float numbers?

There are libraries which will parse the NMEA output for you, as I said. Ten seconds with Google took me here:

http://arduiniana.org/libraries/tinygps/

It returns lat/long in 100000ths of a degree but you could convert those values to floating point degrees by a simple division.

I will not use TinyGPS. I used to convert it ;

e= NMEA[44]- '0';

I'm trying to solve for decimal errors.

Any particular reason you have chosen to reproduce the functionality rather than use the existing tried and tested library?

Yes

Do you think it's a good reason?

What are you going to do with these values once you have them represented as a float? They are not normal float values (they combine decimal degrees with floating point minutes) and it would be easy to mess up processing them. If you intend to pass them on to any other device then doing it textually would probably make more sense, and converting them to a single unit (e.g. degrees, minutes or radians) would be the next best thing.

If you really want to do the parsing yourself then you can use strtok() to split the sentence into fields, select the Lat field, use atof() to parse the field value to a float value, check the N/S to decide whether it should be positive or negative. Similarly for Longitude parse it to a float value and then check whether its an Easting or Westing and sign the result appropriately. If you believe the fields will always be exactly the same size then you could potentially use fixed character offsets to locate each field, but it doesn't sound as if you plan to do any validation so if you receive anything unexpected that could lead you into trouble.