The sketch below displays Lon, Lat, and time from a VK2828U7GLF.
How do I add Altitude?
#include <SD.h>
// GPS with Date/Time/Lon/Lat 1602 LCD Display
// Board: Arduino/Genuine Uno
// Arduino GPS Tutorial: Get Latitude and Longitude Coordinates From Receiver
#include <Wire.h>
#include <SoftwareSerial.h>
#include <TinyGPS.h>
#include <SPI.h>
#include <SD.h>const int chipSelect = 10;
File dataFile;
#define LOGFILE "gpslog.txt"long lat,lon; // create variable for latitude and longitude object
SoftwareSerial gpsSerial(2, 3); // create gps sensor connection
TinyGPS gps; // create gps object
#include <LiquidCrystal_I2C.h>
// set the LCD address to 0x27 for a 16 chars 2 line display
// A FEW use address 0x3F
// Set the pins on the I2C chip used for LCD connections:
// addr, en,rw,rs,d4,d5,d6,d7,bl,blpolLiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); // Set the LCD I2C address
int gpsPin = 4; // GPS enable connected to digital pin 4
//_________________________________________
void setup(){
Serial.begin(9600); // connect serial
gpsSerial.begin(9600); // connect gps sensorlcd.begin(16,2); // initialize the lcd for 16 chars 2 lines, turn on backlight
lcd.backlight(); // finish with backlight on
Serial.println("Starting SDCard reader and card");
pinMode(chipSelect, OUTPUT);
pinMode(SS, OUTPUT);if (!SD.begin(chipSelect)) {
Serial.println("SD Card initialization failed!");
return;
}// Serial.println("Opening logfile for write.");
dataFile = SD.open(LOGFILE, FILE_WRITE); // Open up the file we're going to log to!
if (! dataFile) {
// Serial.println("error opening log file"); // Wait forever since we cant write data
while (1) ; // Wait forever since we cant write data
}pinMode(gpsPin, OUTPUT); // sets the digital pin as output
}
//_________________________________________
void loop(){
digitalWrite(gpsPin, LOW); // sets the GPS on
while(gpsSerial.available()){ // check for gps data
if(gps.encode(gpsSerial.read())) { // encode gps datagps.get_position(&lat,&lon); // get latitude and longitude
//date and time
int year;
byte month, day, hour, minute, second, hundredths;
gps.crack_datetime(&year, &month, &day, &hour, &minute, &second, &hundredths);// Print date and time
Serial.print("Date: ");
Serial.print(day, DEC); Serial.print("/");
Serial.print(month, DEC); Serial.print("/");
Serial.print(year);Serial.print(" Time: "); Serial.print(hour, DEC); Serial.print(":");
Serial.print(minute, DEC); Serial.print(":"); Serial.print(second, DEC);
// Serial.print("."); Serial.print(hundredths, DEC);
//position
float lonx = lon;
float latx = lat;Serial.print(" Lon: ");
Serial.print(lonx/1000000,5);Serial.print(" Lat: ");
Serial.println(latx/1000000,5);//_______________________________
//write gpx file
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.File dataFile = SD.open("gpslog.txt", FILE_WRITE);
// if the file is available, write to it:
if (dataFile) {
dataFile.print(" <trkpt lon="");
dataFile.print(lonx/1000000,8);
dataFile.print("" lat="");
dataFile.print(latx/1000000,8);
dataFile.println("">");dataFile.print(" ");
dataFile.print(year);
dataFile.print("-");
dataFile.print(month, DEC);
dataFile.print("-");
dataFile.print(day, DEC);
dataFile.print("T");
dataFile.print(hour, DEC);
dataFile.print(":");
dataFile.print(minute, DEC);
dataFile.print(":");
dataFile.print(second, DEC);
dataFile.print(".000Z");
dataFile.println("");
dataFile.println(" ");dataFile.close();}
// Send to LED
lcd.setCursor(0,0); //Start at character 0 on line 0
lcd.print("Lat: ");
lcd.print(latx*-1/1000000,5); // print latitudelcd.setCursor(0,1);
lcd.print("Lon: ");
lcd.print(lonx/1000000,5); // print longitude// digitalWrite(gpsPin, HIGH); // sets the GPS off
delay(29000);
}
}
}