myFile.println("--------------------------------------------------------------------------
-----------");
}
myFile.close(); // Close the file to properly save the data
}
else {
// if the file didn't open, print an error:
Serial.println("error opening data.txt");
}
}
void loop()
{
char gpsDate[10], gpsTime[10];
//Query module only every 10 seconds. Adjust this value for more/less frequent GPS logs.
if (millis() - lastTime > 10000)
{
lastTime = millis(); //Update the timer
long latitude = myGPS.getLatitude();
long longitude = myGPS.getLongitude();
long altitude = myGPS.getAltitude();
uint8_t fixType = myGPS.getFixType();
if(fixType != 0){
myFile = SD.open("data.txt", FILE_WRITE); // Open file "data.txt"
if(myFile)
{
// Get date and time
sprintf(gpsDate,"%d/%d/%d", myGPS.getMonth(),myGPS.getDay(),myGPS.getYear());
if(myGPS.getSecond() < 10){
sprintf(gpsTime,"%d/%d/0%d", myGPS.getHour(),myGPS.getMinute(),myGPS.getSecond());
}
else
{
sprintf(gpsTime,"%d/%d/0%d", myGPS.getHour(),myGPS.getMinute(),myGPS.getSecond());
}
// Save data to SD card
myFile.print(gpsDate);
myFile.print('\t');
myFile.print(gpsTime);
myFile.print('\t');
myFile.print(latitude);
myFile.print('\t');
myFile.print(longitude);
myFile.print('\t');
myFile.println(altitude);
}
Serial.print(gpsDate);
Serial.print('\t');
Serial.print(gpsTime);
Serial.print('\t');
Serial.print(latitude);
Serial.print('\t');