@all of you Thank you for your replies. I Appreciate it. It will take me some time to study your suggestions.
@noiasca:
Arduino Uno.
your time zone adjustment should not only change the hour
Exactly.
Usually, I would use the Julian day conversion/back conversion for that, but it seems clumsy here, and may take too much time in the time loop.
What do you suggest?
I prefer methods that I can see through and understand as it is a hobby and I am a newbie in a learning situation. The code below could for sure be smarter.
Best regards,
Niels
#include <TinyGPSPlus.h> // by Mikal Hart
#include <SoftwareSerial.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2); //Arduino pins for communication
static const int RxPin = 8, TxPin = 9; // TxPin not in use.
static const uint32_t GPSBaud = 9600;
static const int TimeZone = 1; // dansk tid
// Erklæring af tider og strenge
int day, month, year, hour, minute, second;
String dateStr, dayStr, monthStr, yearStr;
String timeStr, hourStr, minuteStr, secondStr;
String oldTimeStr;
const char *monthName[] = {
"null", "jan.", "feb.", "mar.", "apr.", "maj ", "juni", "juli", "aug.", "okt.", "nov.", "dec."
};
// The TinyGPSPlus object
TinyGPSPlus gps;
// The serial connection to the GPS device
SoftwareSerial ss(RxPin, TxPin);
// For stats that happen every 5 seconds
unsigned long last = 0UL;
void setup() {
delay(1000);
Serial.begin(115200);
ss.begin(GPSBaud);
// Display setup
lcd.begin(16, 2); //start LCDlibrary and set display size
lcd.clear(); // Position (0,0)
}
void loop() {
// Dispatch incoming characters
while (ss.available() > 0) {
gps.encode(ss.read());
}
if (gps.date.isUpdated()) {
year = gps.date.year();
month = gps.date.month();
day = gps.date.day();
}
if (gps.time.isUpdated()) {
hour = gps.time.hour();
minute = gps.time.minute();
second = gps.time.second();
hour = hour + TimeZone;
if (hour >= 24) {
hour = 0;
}
}
// Times to strings
dayStr = String(day);
monthStr = String(month);
yearStr = String(year);
hourStr = String(hour);
minuteStr = String(minute);
secondStr = String(second);
// Minimum 2 digits in numbers
if (day < 10) { dayStr = '0' + dayStr; }
if (month < 10) { monthStr = ' ' + monthStr; }
if (hour < 10) { hourStr = '0' + hourStr; }
if (minute < 10) { minuteStr = '0' + minuteStr; }
if (second < 10) { secondStr = '0' + secondStr; }
// Build display strings
dateStr = dayStr + ". " + monthName[month] + " " + yearStr;
timeStr = hourStr + ":" + minuteStr + ":" + secondStr;
// To monitor
Serial.println(dateStr + " " + timeStr);
// To LCD. Cleared in setup.
if (timeStr != oldTimeStr) { //Avoid unneeded display update
lcd.setCursor(4, 0); // (position,line)
lcd.print(timeStr);
lcd.setCursor(2, 1);
lcd.print(dateStr);
oldTimeStr = timeStr;
// Mark validity
lcd.setCursor(0, 0);
if (gps.location.isValid()) {
lcd.print("*");
} else lcd.print("?");
}
}