I am learning how the Arduino and GPS module work so I can apply it to another project. My components are an Arduino Uno, NEO-6M GPS module, and a Adafruit 1.8" TFT display.
The code did not work as expected. It does display "waiting for sats".
However, it still displays the generic time and date, 1-1-1970 and 12:00:00 AM, for a while until the local time adisplays.
Plus the updated code takes a lot longer to display the time
Here is the code so far. What I would like it to do is:
- Display "waiting for sats"
- continue when a successful GPS signal is received
- adjust GPS time & date to local
- display date and time
Eventually this will be the basis for a Speed and GPS trackker for my RC vehicles. Any help is greatly appreciated.
#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_ST7735.h>
#include <TimeLib.h>
#define cs 10
#define dc 8
#define rst 9
#include <TinyGPS++.h>
#include <SoftwareSerial.h>
Adafruit_ST7735 tft = Adafruit_ST7735(cs, dc, rst);
static const int RXPin = 2, TXPin = 3; //GPS communication
// Change this value to suit your Time Zone
const int UTC_offset = -4; // Eastern Australia Time is 10
static const uint32_t GPSBaud = 9600;
TinyGPSPlus gps;
SoftwareSerial ss(RXPin, TXPin);
time_t prevDisplay = 0; // Count for when time last displayed
void setup()
{
Serial.begin(9600); //set baud rate for serial monitor
ss.begin(GPSBaud); //set baud rate for GPS
tft.initR(INITR_BLACKTAB); //correct INITR for my display
tft.fillScreen(ST7735_BLACK);
tft.setCursor(20, 58);
tft.setTextSize(1);
tft.setTextColor(ST7735_GREEN,ST7735_BLACK);
tft.println("GPS Date & Time");
tft.print(" Monitor");
delay(3000);
tft.fillScreen(ST7735_BLACK);
}
void loop()
{
tft.setTextSize(1);
tft.setTextColor(ST7735_GREEN,ST7735_BLACK);
if (ss.available() < 1)
{
gps.encode(ss.read());
}
GPS_Timezone_Adjust(); // Call Time Adjust Function
}
void GPS_Timezone_Adjust(){
while (ss.available() > 0) {
gps.encode(ss.read());
if (gps.location.isUpdated()) {
int Year = gps.date.year();
byte Month = gps.date.month();
byte Day = gps.date.day();
byte Hour = gps.time.hour();
byte Minute = gps.time.minute();
byte Second = gps.time.second();
//Hour = hourFormat12();
// Set Time from GPS data string
setTime(Hour, Minute, Second, Day, Month, Year);
// Calc current Time Zone time by offset value
adjustTime(UTC_offset * SECS_PER_HOUR);
}
}
if (timeStatus()!= timeNotSet) {
if (now() != prevDisplay) {
prevDisplay = now();
ClockDisplay();
}
}
}
void ClockDisplay(){
tft.setCursor(5, 8);
tft.setTextColor(ST7735_GREEN,ST7735_BLACK);
tft.print("Date: ");
tft.setTextColor(ST7735_CYAN,ST7735_BLACK);
if(month() < 10){
tft.print("0");
}
tft.print(month());
tft.print("-");
if(day() < 10){
tft.print("0");
}
tft.print(day());
tft.print("-");
tft.print(year());
tft.setCursor(4, 30);
tft.setTextColor(ST7735_GREEN,ST7735_BLACK);
tft.print("Time: ");
tft.setTextColor(ST7735_CYAN,ST7735_BLACK);
if(hourFormat12() < 10){
tft.print("0");
}
tft.print(hourFormat12());
tft.print(":");
if(minute() < 10){
tft.print("0");
}
tft.print(minute());
tft.print(":");
if(second() < 10){
tft.print("0");
}
tft.print(second());
if (isAM()) {
tft.println(" AM ");
} else {
tft.println(" PM ");
}
tft.setCursor(5,50);
tft.setTextColor(ST7735_WHITE,ST7735_RED);
tft.println("RAW GPS DATE & TIME");
tft.setCursor(5,58);
tft.print(gps.date.month());
tft.print("-");
tft.print(gps.date.day());
tft.print("-");
tft.print(gps.date.year());
tft.setCursor(5,66);
tft.print(gps.time.hour());
tft.print(":");
tft.print(gps.time.minute());
tft.print(":");
tft.print(gps.time.second());
smartDelay(400);
}
// deadline for a good recption
static void smartDelay(unsigned long ms)
{
unsigned long start = millis();
do
{
while (ss.available())
gps.encode(ss.read());
} while (millis() - start < ms);
}