//code sourced from PabitraKumarGhorai
#include <LiquidCrystal.h> // For LCD
#include <TinyGPSPlus.h> // For GPS
#include <SoftwareSerial.h> // For GPS
// Make sure wiring is correct!!
SoftwareSerial NEO6M(5,6); // (Rx, Tx)
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);
TinyGPSPlus gps;
volatile float minutes, seconds;
volatile int degree, secs, mins;
void setup() {
Serial.begin(9600);
NEO6M.begin(9600);
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
// Print a message to the LCD.
// lcd.print("Hello, World!"); Example of how to print to LCD
}
void loop() {
smartDelay(1000); /* Generate precise delay of 1ms */
unsigned long start;
double lat_val, lng_val, alt_m_val;
uint8_t hr_val, min_val, sec_val;
bool loc_valid, alt_valid, time_valid;
lat_val = gps.location.lat(); /* Get latitude data */
loc_valid = gps.location.isValid(); /* Check if valid location data is available */
lng_val = gps.location.lng(); /* Get longtitude data */
if (!loc_valid)
{
Serial.print("Latitude : ");
Serial.println("*****");
Serial.print("Longitude : ");
Serial.println("*****");
lcd.clear();
lcd.setCursor(0,1);
lcd.print("Location data invalid!");
}
else
{
DegMinSec(lat_val);
Serial.print("Latitude in Decimal Degrees : ");
Serial.println(lat_val, 6);
Serial.print("Latitude in Degrees Minutes Seconds : ");
Serial.print(degree);
Serial.print("\t");
Serial.print(mins);
Serial.print("\t");
Serial.println(secs);
DegMinSec(lng_val); /* Convert the decimal degree value into degrees minutes seconds form */
Serial.print("Longitude in Decimal Degrees : ");
Serial.println(lng_val, 6);
Serial.print("Longitude in Degrees Minutes Seconds : ");
Serial.print(degree);
Serial.print("\t");
Serial.print(mins);
Serial.print("\t");
Serial.println(secs);
// Print Lat/Long to LCD:
//lcd.clear();
lcd.setCursor(0,0);
lcd.print("Lat: ");
lcd.setCursor(5,0);
lcd.print(lat_val, 8);
lcd.setCursor(0,1);
lcd.print("Lon: ");
lcd.setCursor(5,1);
lcd.print(lng_val, 8);
}
}
static void smartDelay(unsigned long ms)
{
unsigned long start = millis();
do
{
while (NEO6M.available()) /* Encode data read from GPS while data is available on serial port */
gps.encode(NEO6M.read());
/* Encode basically is used to parse the string received by the GPS and to store it in a buffer so that information can be extracted from it */
} while (millis() - start < ms);
}
void DegMinSec( double tot_val) /* Convert data in decimal degrees into degrees minutes seconds form */
{
degree = (int)tot_val;
minutes = tot_val - degree;
seconds = 60 * minutes;
minutes = (int)seconds;
mins = (int)minutes;
seconds = seconds - minutes;
seconds = 60 * seconds;
secs = (int)seconds;
}