Search code examples
arduinogpsarduino-uno

Trying to get the location of my GPS but the serial received 0.00000;0.00000


enter image description here

Arduino code:

#include <SoftwareSerial.h>
#include <TinyGPS.h>
//long   lat,lon; // create variable for latitude and longitude object
float lat,lon ; // create variable for latitude and longitude object
SoftwareSerial gpsSerial(3,4);//rx,tx

TinyGPS gps; // create gps object
void setup(){
Serial.begin(9600); // connect serial
Serial.println("The GPS Received Signal:");
gpsSerial.begin(9600); // connect gps sensor

}

void loop(){


String latitude = String(lat,6);
String longitude = String(lon,6);
Serial.println(latitude+";"+longitude);
delay(1000);

}

I'm trying to get the location of my GPS but the serial received 0.00000;0.00000 what am I doing wrong?


Solution

  • You have a big issue, you never get the data from your GPS object into your variables. Do is as follows:

    // create variable for latitude and longitude object
     double lat = 0; // The lib defines it as double!
     double lon = 0; // The lib defines it as double!
    unsigned long lastGpsCheck = 0;
    const unsigned long delayTime = 1000;
    ....
    
    void loop(){
    // Replaces the CPU stopping delay, does the same without blocking
     if(millis() - lastGpsCheck > delayTime) {
       lat = gps.location.lat(); // This is missing in your code
       lon = gps.location.lon(); // This is missing in your code
    
       Serial.println( lat,6 );
       Serial.print(";");
       Serial.print(lon,6 );
       lastGpsCkeck = millis();
      }
    }
    

    Notice: I replaced the delay, learn early to never use delay in loop, subroutines or libraries. Its ok in setup for waiting for hardware to initialize or as a temporary debug help.
    Avoid converting to String class. Always use fix char arrays. String class has bad memory mgmt and fractures your heap (memory leaks -> crash), fix char arrays are compiled to flash.