#include #include #include File myFile; static const int RXPin = 4, TXPin = 3; /* This sample code demonstrates the normal use of a TinyGPS object. */ TinyGPS gps; /* On Teensy, the UART (real serial port) is always best to use. */ /* Unlike Arduino, there's no need to use NewSoftSerial because */ /* the "Serial" object uses the USB port, leaving the UART free. */ SoftwareSerial ss(RXPin, TXPin); long lat, lon; float flat, flon; unsigned long age, date, time, chars; int year; byte month, day, hour, minute, second, hundredths; unsigned short sentences, failed; int ledPin = 7; void gpsdump(TinyGPS &gps); void printFloat(double f, int digits = 2); void setup() { Serial.begin(115200); ss.begin(9600); pinMode(ledPin, OUTPUT); while (!Serial) { ; // wait for serial port to connect. Needed for Leonardo only } Serial.print("Initializing SD card..."); // On the Ethernet Shield, CS is pin 4. It's set as an output by default. // Note that even if it's not used as the CS pin, the hardware SS pin // (10 on most Arduino boards, 53 on the Mega) must be left as an output // or the SD library functions will not work. pinMode(10, OUTPUT); if (!SD.begin(10)) { Serial.println("initialization failed!"); digitalWrite(ledPin, HIGH); return; } Serial.println("initialization done."); digitalWrite(ledPin, LOW); delay(1000); Serial.print("Testing TinyGPS library v. "); Serial.println(TinyGPS::library_version()); Serial.println("by Mikal Hart"); Serial.println(); Serial.print("Sizeof(gpsobject) = "); Serial.println(sizeof(TinyGPS)); Serial.println(); } void loop() { bool newdata = false; unsigned long start = millis(); // Every 5 seconds we print an update while (millis() - start < 5000) { if (ss.available()) { char c = ss.read(); // Serial.print(c); // uncomment to see raw GPS data if (gps.encode(c)) { newdata = true; // break; // uncomment to print new data immediately! } } } if (newdata) { Serial.println("Acquired Data"); Serial.println("-------------"); gpsdump(gps); Serial.println("-------------"); Serial.println(); } esd(); } void esd(){ myFile = SD.open("LOG.CSV", FILE_WRITE); // if the file opened okay, write to it: if (myFile) { Serial.print("Writing to LOG.CSV..."); myFile.print(static_cast(day)); myFile.print("/"); myFile.print(static_cast(month)); myFile.print("/"); myFile.print(year); myFile.print(" "); myFile.print(static_cast(hour)+2); myFile.print(":"); myFile.print(static_cast(minute)); myFile.print(","); myFile.print(flat, 5); myFile.print(", "); myFile.print(flon, 5); myFile.print(", "); myFile.print(gps.f_speed_kmph()); myFile.println("km/h"); // close the file: myFile.close(); Serial.println("done."); } else { // if the file didn't open, print an error: Serial.println("error opening test.txt"); } // re-open the file for reading: myFile = SD.open("LOG.CSV"); if (myFile) { Serial.println("LOG.CSV:"); // read from the file until there's nothing else in it: while (myFile.available()) { Serial.write(myFile.read()); } // close the file: myFile.close(); } else { // if the file didn't open, print an error: Serial.println("error opening LOG.CSV"); } delay(1000); } void gpsdump(TinyGPS &gps) { gps.get_position(&lat, &lon, &age); Serial.print("Lat/Long(10^-5 deg): "); Serial.print(lat); Serial.print(", "); Serial.print(lon); Serial.print(" Fix age: "); Serial.print(age); Serial.println("ms."); // On Arduino, GPS characters may be lost during lengthy Serial.print() // On Teensy, Serial prints to USB, which has large output buffering and // runs very fast, so it's not necessary to worry about missing 4800 // baud GPS characters. gps.f_get_position(&flat, &flon, &age); Serial.print("Lat/Long(float): "); printFloat(flat, 5); Serial.print(", "); printFloat(flon, 5); Serial.print(" Fix age: "); Serial.print(age); Serial.println("ms."); gps.get_datetime(&date, &time, &age); Serial.print("Date(ddmmyy): "); Serial.print(date); Serial.print(" Time(hhmmsscc): "); Serial.print(time); Serial.print(" Fix age: "); Serial.print(age); Serial.println("ms."); gps.crack_datetime(&year, &month, &day, &hour, &minute, &second, &hundredths, &age); Serial.print("Date: "); Serial.print(static_cast(month)); Serial.print("/"); Serial.print(static_cast(day)); Serial.print("/"); Serial.print(year); Serial.print(" Time: "); Serial.print(static_cast(hour)); Serial.print(":"); Serial.print(static_cast(minute)); Serial.print(":"); Serial.print(static_cast(second)); Serial.print("."); Serial.print(static_cast(hundredths)); Serial.print(" Fix age: "); Serial.print(age); Serial.println("ms."); Serial.print("Alt(cm): "); Serial.print(gps.altitude()); Serial.print(" Course(10^-2 deg): "); Serial.print(gps.course()); Serial.print(" Speed(10^-2 knots): "); Serial.println(gps.speed()); Serial.print("Alt(float): "); printFloat(gps.f_altitude()); Serial.print(" Course(float): "); printFloat(gps.f_course()); Serial.println(); Serial.print("Speed(knots): "); printFloat(gps.f_speed_knots()); Serial.print(" (mph): "); printFloat(gps.f_speed_mph()); Serial.print(" (mps): "); printFloat(gps.f_speed_mps()); Serial.print(" (kmph): "); printFloat(gps.f_speed_kmph()); Serial.println(); gps.stats(&chars, &sentences, &failed); Serial.print("Stats: characters: "); Serial.print(chars); Serial.print(" sentences: "); Serial.print(sentences); Serial.print(" failed checksum: "); Serial.println(failed); } void printFloat(double number, int digits) { // Handle negative numbers if (number < 0.0) { Serial.print('-'); number = -number; } // Round correctly so that print(1.999, 2) prints as "2.00" double rounding = 0.5; for (uint8_t i=0; i 0) Serial.print("."); // Extract digits from the remainder one at a time while (digits-- > 0) { remainder *= 10.0; int toPrint = int(remainder); Serial.print(toPrint); remainder -= toPrint; } }