Created
December 24, 2011 03:40
-
-
Save schappim/1516172 to your computer and use it in GitHub Desktop.
LS20031 GPS Arduino
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <NewSoftSerial.h> | |
#include <TinyGPS.h> | |
TinyGPS gps; | |
NewSoftSerial nss(2, 3); | |
int year, month, day, hour, minutes, second, hundredths; | |
unsigned long fix_age; | |
void gpsdump(TinyGPS &gps); | |
bool feedgps(); | |
void printFloat(double f, int digits = 2); | |
void setup() | |
{ | |
Serial.begin(115200); | |
nss.begin(57600); | |
} | |
void loop(){ | |
bool newdata = false; | |
unsigned long start = millis(); | |
// Every 5 seconds we print an update | |
while (millis() - start < 1000){ | |
if (feedgps()) | |
newdata = true; | |
} | |
if (newdata){ | |
Serial.println("-------------"); | |
gpsdump(gps); | |
Serial.println("-------------"); | |
} | |
} | |
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<digits; ++i) | |
rounding /= 10.0; | |
number += rounding; | |
// Extract the integer part of the number and print it | |
unsigned long int_part = (unsigned long)number; | |
double remainder = number - (double)int_part; | |
Serial.print(int_part); | |
// Print the decimal point, but only if there are digits beyond | |
if (digits > 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; | |
} | |
} | |
void gpsdump(TinyGPS &gps){ | |
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; | |
feedgps(); // If we don't feed the gps during this long routine, we may drop characters and get checksum errors | |
gps.f_get_position(&flat, &flon, &age); | |
Serial.print("Lat/Long(float): "); printFloat(flat, 5); Serial.print(", "); printFloat(flon, 5); | |
feedgps(); | |
gps.crack_datetime(&year, &month, &day, &hour, &minute, &second, &hundredths, &age); | |
if(month&&day&&year){ | |
Serial.print("Date: "); Serial.print(static_cast<int>(month)); Serial.print("/"); | |
Serial.print(static_cast<int>(day)); Serial.print("/"); | |
Serial.print(year); | |
} | |
Serial.print(" Time: "); Serial.print(static_cast<int>(hour)); Serial.print(":"); | |
Serial.print(static_cast<int>(minute)); Serial.print(":"); | |
Serial.println(static_cast<int>(second)); | |
} | |
bool feedgps(){ | |
while (nss.available()){ | |
if (gps.encode(nss.read())) | |
return true; | |
} | |
return false; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment