#include
#include
#include
LiquidCrystal lcd(12, 11, 8, 7, 6, 5);
TinyGPS gps;
SoftwareSerial nss(3, 4);
static bool feedgps();
static void print_date(TinyGPS &gps);
void setup()
{
nss.begin(9600);
lcd.begin(16, 2);
lcd.setCursor(0, 0);
lcd.print(" [ GPS Clock ]");
}
void loop()
{
bool newdata = false;
unsigned long start = millis();
// Every second we print an update
while (millis() - start < 1000)
{
if (feedgps())
newdata = true;
}
print_date(gps);
}
static void print_date(TinyGPS &gps)
{
int year;
byte month, day, hour, minute, second, hundredths;
unsigned long age;
gps.crack_datetime(&year, &month, &day, &hour, &minute, &second, &hundredths, &age);
hour += 9; // GMT +9 : Korea time
if(hour > 24) hour -= 24;
if(hour == 24) hour = 0;
if (age == TinyGPS::GPS_INVALID_AGE){
lcd.setCursor(0, 1);
lcd.print("-----------------");
}
else
{
char sz[32];
if(hour >= 12)
sprintf(sz, " PM %02d:%02d:%02d ", hour-12, minute, second);
else
sprintf(sz, " AM %02d:%02d:%02d ", hour, minute, second);
lcd.setCursor(0, 1);
lcd.print(sz);
}
// feedgps();
}
static bool feedgps()
{
while (nss.available())
{
if (gps.encode(nss.read()))
return true;
}
return false;
}
|