0
votes

Im new in Arduino programming and i have quite a problem which is save longitude and latitude values of my new Adafruit Ultimate GPS Breakout. im using the tutorial giving in the website then i tried to apply parsing code in the library Open up the File→Examples→Adafruit_GPS→parsing sketch and upload it to the Arduino. Then open up the serial monitor. i got the results in my serial monitor as shown in the attached file (i highlighted my question in the snap below)

Im trying to save the values of my location in the degree which is appear in the serial monitor as Location (in degrees, works with Google Maps): 32.5011, 44.4499

A part of the standard code for display result is:

Serial.print("\nTime: ");
    Serial.print(GPS.hour, DEC); Serial.print(':');
    Serial.print(GPS.minute, DEC); Serial.print(':');
    Serial.print(GPS.seconds, DEC); Serial.print('.');
    Serial.println(GPS.milliseconds);
    Serial.print("Date: ");
    Serial.print(GPS.day, DEC); Serial.print('/');
    Serial.print(GPS.month, DEC); Serial.print("/20");
    Serial.println(GPS.year, DEC);
    Serial.print("Fix: "); Serial.print((int)GPS.fix);
    Serial.print(" quality: "); Serial.println((int)GPS.fixquality);
    if (GPS.fix) {
      Serial.print("Location: ");
      Serial.print(GPS.latitude, 4); Serial.print(GPS.lat);
      Serial.print(", ");
      Serial.print(GPS.longitude, 4); Serial.println(GPS.lon);
      Serial.print("Location (in degrees, works with Google Maps): ");
      Serial.print(GPS.latitudeDegrees, 4);
      Serial.print(", ");
      Serial.println(GPS.longitudeDegrees, 4);
      Serial.print("Speed (knots): "); Serial.println(GPS.speed);
      Serial.print("Angle: "); Serial.println(GPS.angle);
      Serial.print("Altitude: "); Serial.println(GPS.altitude);
      Serial.print("Satellites: "); Serial.println((int)GPS.satellites);

the results is shown below enter image description here next i tried to define 2 variable in order to copy the longitude and latitude value by adding simple code after the Serial.println(GPS.longitudeDegrees, 4); as shown below

Serial.print("Location (in degrees, works with Google Maps): ");
      Serial.print(GPS.latitudeDegrees, 4);
      Serial.print(", ");
      Serial.println(GPS.longitudeDegrees, 4);
      lat1= (GPS.latitudeDegrees, 4);
      lon1=(GPS.longitudeDegrees, 4);
      Serial.println (lat1);
      Serial.println (lon1);
      delay (500);

then i got only number four in my lat1 and lon1 value in serial monitor

enter image description here

then i edit the code by removing number 4 from it as shown below

Serial.print("Location (in degrees, works with Google Maps): ");
  Serial.print(GPS.latitudeDegrees, 4);
  Serial.print(", ");
  Serial.println(GPS.longitudeDegrees, 4);
  lat1= GPS.latitudeDegrees;
  lon1=GPS.longitudeDegrees;
  Serial.println (lat1);
  Serial.println (lon1);
  delay (500);

then i got only 2 number after the dot 32.5011 → 32.50 44.4499 → 44.45 as shown in the attached below enter image description here so im asking how can i save the full number in my lon1 and lat1 because i need these number for further analysis BTW i tried to defie lon1, lat1 as float, double and still same issue just 2 number after digit i got thank you all

1

1 Answers

1
votes

Floating point numbers have an infinite number of decimal places. When you print them it will only print 2 unless you specify with the second parameter how many to print.

For example:

float x = 67.1234

Serial.print(x); // will print 67.12
Serial.print(x,3); // will print 67.123
Serial.print(x, 4); // will print 67.1234
Serial.print(x,6);  // will print 67.123400

So what you are seeing is just how many places you told it to print. But the variable always has the whole number with an infinite number of decimal places.

Now with floats on an Arduino only the first 6 or 7 decimal places will be accurate. But the number itself has an infinite number of digits after the decimal if you want to print them.

Also note that the second parameter is only doing something in the print statement. Lots of people will try to do this:

float x = 67.1234 , 2;

thinking that this will somehow save a number with only 2 digits of precision, but this isn't the case. In this case it would just save the number 2. As you saw in your experiment. This is how the comma operator works. It's only in the print function that you get to specify. Again, the actual number itself has an infinite number of decimal places you can print.