Hey forum,
I'm busy building a gps logger. Im using an UP501 gps and a regular SD card slot.
In the code below everything works perfectly fine and without a gps connection zeros get saved on the SD card as you would expect. When the gps gets a connection nothing gets saved on the SD card anymore and I have no idea how this comes. Can you please advise?
Greets,
Leo
#include <TinyGPS.h>
#include <SoftwareSerial.h>
#include <SD.h>File GPSFile;
TinyGPS gps;
SoftwareSerial nss(2, 3);
float flat=0;
float flon=0;
int year;
byte month, day, hour, minute, second, hundredths;
byte hourDiff=1;
byte minuteDiff=0;
byte secondDiff=0;int stap=0;
int time=0;void setup()
{
pinMode(10, OUTPUT);
if (!SD.begin(10)) {
return;
}
}void loop()
{time=millis();
startGPS();
writeToSD();
gpsdump(gps);
if((gps.satellites())!=(TinyGPS::GPS_INVALID_SATELLITES)){
gpsdump(gps);
writeToSD();
}
delay(5000);
}void writeToSD(void)
{
GPSFile = SD.open("GPSFile.txt", FILE_WRITE);
if (GPSFile) {
////Serial..print("Writing to GPSFile.txt");
GPSFile.print(" ");
GPSFile.print(flat,5);
GPSFile.print(" ");
GPSFile.print(flon,5);
GPSFile.print(" ");
GPSFile.print(day);
GPSFile.print("/");
GPSFile.print(month);
GPSFile.print("/");
GPSFile.print(year);
GPSFile.print(" ");
GPSFile.print(hour+hourDiff);
GPSFile.print(":");
GPSFile.print(minute+minuteDiff);
GPSFile.print(":");
GPSFile.println(second);
// close the file:
GPSFile.close();
////Serial..println("done.");
}
else {
// if the file didn't open, print an error:
////Serial..println("error opening GPSFile.txt");
}
}static void gpsdump(TinyGPS &gps)
{unsigned long age, date, time, chars = 0;
gps.f_get_position(&flat, &flon, &age);
gps.crack_datetime(&year, &month, &day, &hour, &minute, &second, &hundredths, &age);}
void startGPS(void)
{
bool newdata = false;
unsigned long start = millis();
while (millis() - start < 1000)
{
if (feedgps())
newdata = true;
}}
static bool feedgps()
{
while (nss.available())
{
if (gps.encode(nss.read()))
return true;
}
return false;
}