GPS Library and programming

This will probably go unanswered or at least the answer will be ambiguous as the question.

Parts:

  1. Arduino Mega.
  2. GPS sensor from Amazon. (GPS Module GPS NEO-6M(Ar duino GPS, Drone Microcontroller GPS Receiver) Compatible with 51 Microcontroller STM32 Ar duino UNO R3 with IPEX Antenna High Sensitivity for Navigation Satellite Positioning).
  3. Touch screen display ( HiLetgo 2.4" ILI9341 240X320 TFT LCD Display with Touch Panel LCD for Arduino UNO MEGA2560)

When using just the Mega by itself, (or with the TFT installed) and uploading a blank sketch, with the GPS sensor pin TXD on Pin19 of the Mega and the GPS sensor is flashing the red LED at a once a second rate, I recieve GPS data while viewing the serial monitor. Good!

When I upload this Sketch, I get Position: LAT XX.XXX and LONG -XX.XXX displayed on the touch screen every second or so. Good!

// TFT SETUP START ***************************************************************
#include <Arduino.h>
#define USE_ADAFRUIT_SHIELD_PINOUT 1
#include <Adafruit_GFX.h>                                      //added Adafruit GFX with BIO Whatever also
#include <MCUFRIEND_kbv.h>                                     //added mcufriend.kbv
MCUFRIEND_kbv tft;

#define BLACK   0x0000
#define BLUE    0x001F
#define RED     0xF800
#define GREEN   0x07E0
#define CYAN    0x07FF
#define MAGENTA 0xF81F
#define YELLOW  0xFFE0
#define WHITE   0xFFFF

float B;
int delta = 50;                  // 50 was middle of screen, 0 is top of screen, 100 at bottom of screen
int clockCenterX=120;            // 120 is center, 60 shifts left, 230 shifts right
int clockCenterY=110+delta;      // 110 is center, 0 is up, 220 is down   (not all the way to the center in both up & down)

#include <Wire.h>
#include <Adafruit_Sensor.h> 
#include <TinyGPS++.h>                                                       
// TFT SETUP END ******************************************************
#include <TinyGPS.h>
//long   lat,lon; // create variable for latitude and longitude object
float lat,lon;
TinyGPS gps; // create gps object

void setup(){
  
Serial.begin(115200); // connect serial
Serial.println("The GPS Received Signal:");
Serial1.begin(9600); // connect gps sensor

// TFT VOID SETUP BEGIN *******************************************************
//Serial.begin(9600);

  uint16_t identifier = tft.readID();

    tft.begin(identifier);
    tft.fillScreen(BLACK);                                     // Unsure, seemed to have no effect
    tft.setRotation(0);                                        // Flips Rotation 180 from 0 to 2

    return;
      }
  void drawDisplay(float B)
{
  drawDisplay(B);
  delay (1000);
   }
      
// TFT VOID SETUP END ***********************************************************
void loop(){
    while(Serial1.available()){ // check for gps data
    if(gps.encode(Serial1.read()))// encode gps data
    { 
    gps.f_get_position(&lat,&lon); // get latitude and longitude

    tft.setTextColor(GREEN,BLACK);  tft.setTextSize(3);
    tft.setCursor(5, 10);tft.println("Position: ");
 
    //Latitude
    tft.print("LAT    ");
    tft.println(lat,3);             //Limited to 3 digits past decimal point
    
    Serial.print(",");
    
    //Longitude
    tft.print("LONG  ");
    tft.println(lon,3); 

    gps.f_speed_mph();             // SPEED!!!!!
    tft.println("SPEED    ");
//    tft.print(gps.speed.mph();
     delay(2000);                      // Otherwise it erases right away "flashes"
 tft.fillScreen(BLACK);
   }
  }
 } 

When I upload this next Sketch, I get a display with a compass and headers for Lat, Long, Alt, Sat, Date time, Speed, But there's no data! When I go to Serial Monitor, it prints check wiring. So I've tried to change some values Pin19 to 2 or 3, or changed things like SoftwareSerial... and had instead added ...
static const int RXPin = 4, TXPin = 3;
static const uint32_t GPSBaud = 4800;

And other attempts at removing or adding code to make it see the GPS data, I keep getting errors of all kinds.

**COMPASS**
#include <TinyGPSPlus.h>
#include <SoftwareSerial.h>
static const int RXPin = 0, TXPin = 1;
static const uint32_t GPSBaud = 9600;
#include <Arduino.h>
#define USE_ADAFRUIT_SHIELD_PINOUT 1
#include <Adafruit_GFX.h> 
#include <Adafruit_Sensor.h> 
#include <MCUFRIEND_kbv.h>                                     //added mcufriend.kbv
MCUFRIEND_kbv tft;

//#include "TinyGPS++.h"

TinyGPSPlus gps;

SoftwareSerial ss(RXPin, TXPin);

#include <SPI.h>
#include "Adafruit_GFX.h"
#include "Adafruit_ILI9341.h"

// Assign names to common 16-bit color values:
#define   BLACK  0x0000
#define   BLUE   0x001F
#define   RED    0xF800
#define   GREEN  0x07E0
#define   CYAN   0x07FF
#define   YELLOW 0xFFE0
#define   ORANGE 0xFD20
#define   WHITE  0xFFFF

String    Time, Date;
float     NumberSats, Latitude, Longitude, Bearing;
float     AltitudeMETRES, AltitudeMILES, AltitudeKM, AltitudeFEET;
float     SpeedKPH, SpeedMPH, SpeedKNOTS, SpeedMPS;

const int centreX  = 230; // Location of the compass display on screen
const int centreY  = 120;
const int diameter = 70; // Size of the compass
int       dx = centreX, dy = centreY;
int       last_dx = centreX, last_dy = centreY - diameter * 0.85;

// For stats that happen every 5 seconds
unsigned long last = 0UL;

void setup()

{
  Serial.begin(115200);
  //Serial1.begin(9600); // connect gps sensor
  ss.begin(GPSBaud);
  Serial.print("What the Heck!");

 // Serial.begin(115200);
  //ss.begin(9600);         // This opens up communications to the GPS
  tft.begin();            // Start the TFT display
  tft.setRotation(3);     // Rotate screen by 90°
  tft.setTextSize(2);     // Set medium text size
  tft.setTextColor(YELLOW);
  tft.fillScreen(BLUE);
}

void loop() {
  
  Latitude       = gps.location.lat();
  Longitude      = gps.location.lng();
  Date           = String(gps.date.day() < 10 ? "0" : "") + String(gps.date.day()) + "/" + String(gps.date.month() < 10 ? "0" : "") + String(gps.date.month()) + "/" + String(gps.date.year());
  //Date           = String(gps.date.month()<10?"0":"") + String(gps.date.month()) + "/" + String(gps.date.day()<10?"0":"") + String(gps.date.day()) + "/" + String(gps.date.year());
  Time           = String(gps.time.hour() < 10 ? "0" : "")   + String(gps.time.hour())   + ":" +
                   String(gps.time.minute() < 10 ? "0" : "") + String(gps.time.minute()) + ":" +  String(gps.time.hour() < 10 ? "0" : "") + String(gps.time.second() < 10 ? "0" : "") + String(gps.time.second());
  Bearing        = gps.course.deg();
  SpeedKPH       = gps.speed.kmph();
  SpeedMPH       = gps.speed.mph();
  SpeedKNOTS     = gps.speed.knots();
  SpeedMPS       = gps.speed.mps();
  NumberSats     = gps.satellites.value();
  AltitudeMETRES = gps.altitude.meters();
  AltitudeKM     = gps.altitude.kilometers();
  AltitudeMILES  = gps.altitude.miles();
  AltitudeFEET   = gps.altitude.feet();


  while (ss.available() > 0)
    gps.encode(ss.read());


  if (gps.location.isUpdated())
  {
  Serial.print(F("LOCATION   Fix Age="));
    Serial.print(gps.location.age());
    Serial.print(F("ms Raw Lat="));
    Serial.print(gps.location.rawLat().negative ? "-" : "+");
    Serial.print(gps.location.rawLat().deg);
    Serial.print("[+");
    Serial.print(gps.location.rawLat().billionths);
    Serial.print(F(" billionths],  Raw Long="));
    Serial.print(gps.location.rawLng().negative ? "-" : "+");
    Serial.print(gps.location.rawLng().deg);
    Serial.print("[+");
    Serial.print(gps.location.rawLng().billionths);
    Serial.print(F(" billionths],  Lat="));
    Serial.print(gps.location.lat(), 6);
    Serial.print(F(" Long="));
    Serial.println(gps.location.lng(), 6);
  }
    
  //Serial.println("Time\t\tDate\t\tLAT\tLON\tSATS\tAlt\tBearing\tSpeed(KPH)");
  //Serial.println("----------------------------------------------------------------------------------");
  //Serial.print(Time                   + "\t");
  //Serial.print(Date                   + "\t");
  //Serial.print(String(Latitude, 3)    + "\t");
  //Serial.print(String(Longitude, 3)   + "\t");
  //Serial.print(String(NumberSats)     + "\t");
  //Serial.print(String(AltitudeMETRES) + "\t"); // Select as required
  //Serial.print(String(Bearing)        + "\t");
  //Serial.print(String(SpeedKPH)       + "\t"); // Select as required
  //Serial.print(String(SpeedMPH)     + "\t"); // Select as required
  //Serial.println("\n");
  DisplayGPSdata(NumberSats, Latitude, Longitude, AltitudeMETRES, SpeedKPH, Bearing); // Select units as required
  smartDelay(1000);
  if (millis() > 5000 && gps.charsProcessed() < 10)  Serial.println(F("No GPS data received: check wiring"));
}
//####################################
void DisplayGPSdata(float dNumberSats, float dLatitude, float dLongitude, float dAltitude, float dSpeed, float dBearing) {
  PrintText(60, 0, "   GPS Compass", CYAN, 2);
  tft.fillRect(45, 40, 90, 19 * 4, BLUE);
  PrintText(0, 45, "LAT:" + String(dLatitude), YELLOW, 2);
  PrintText(0, 63, "LON:" + String(dLongitude), YELLOW, 2);
  PrintText(0, 81, "ALT:" + String(dAltitude, 1) + "M", YELLOW, 2);
  PrintText(0, 99, "SAT:" + String(dNumberSats, 0), YELLOW, 2);
  tft.fillRect(80, 220, 120, 18, BLUE);
  PrintText(10, 220, "Speed:" + String(dSpeed) + "kph", YELLOW, 2);
  tft.fillRect(240, 220, 120, 18, BLUE);
  tft.setCursor(200, 220);
  tft.print("Azi: " + Bearing_to_Ordinal(dBearing));
  Display_Compass(dBearing);
  Display_Date_Time();
}
//#########################################
void Display_Compass(float dBearing) {
  int dxo, dyo, dxi, dyi;
  tft.setCursor(0, 0);
  tft.drawCircle(centreX, centreY, diameter, WHITE); // Draw compass circle
  for (float i = 0; i < 360; i = i + 22.5) {
    dxo = diameter * cos((i - 90) * 3.14 / 180);
    dyo = diameter * sin((i - 90) * 3.14 / 180);
    dxi = dxo * 0.9;
    dyi = dyo * 0.9;
    tft.drawLine(dxo + centreX, dyo + centreY, dxi + centreX, dyi + centreY, WHITE);
  }
  PrintText((centreX - 5), (centreY - diameter - 18), "N", GREEN, 2);
  PrintText((centreX - 5), (centreY + diameter + 5) , "S", GREEN, 2);
  PrintText((centreX + diameter + 5),  (centreY - 5), "E", GREEN, 2);
  PrintText((centreX - diameter - 15), (centreY - 5), "W", GREEN, 2);
  dx = (0.85 * diameter * cos((dBearing - 90) * 3.14 / 180)) + centreX; // calculate X position
  dy = (0.85 * diameter * sin((dBearing - 90) * 3.14 / 180)) + centreY; // calculate Y position
  draw_arrow(last_dx, last_dy, centreX, centreY, 5, 5, BLUE);   // Erase last arrow
  draw_arrow(dx, dy, centreX, centreY, 5, 5, YELLOW);           // Draw arrow in new position
  last_dx = dx;
  last_dy = dy;
}
//############################################
void draw_arrow(int x2, int y2, int x1, int y1, int alength, int awidth, int colour) {
  float distance;
  int dx, dy, x2o, y2o, x3, y3, x4, y4, k;
  distance = sqrt(pow((x1 - x2), 2) + pow((y1 - y2), 2));
  dx = x2 + (x1 - x2) * alength / distance;
  dy = y2 + (y1 - y2) * alength / distance;
  k = awidth / alength;
  x2o = x2 - dx;
  y2o = dy - y2;
  x3 = y2o * k + dx;
  y3 = x2o * k + dy;
  x4 = dx - y2o * k;
  y4 = dy - x2o * k;
  tft.drawLine(x1, y1, x2, y2, colour);
  tft.drawLine(x1, y1, dx, dy, colour);
  tft.drawLine(x3, y3, x4, y4, colour);
  tft.drawLine(x3, y3, x2, y2, colour);
  tft.drawLine(x2, y2, x4, y4, colour);
}
//#############################################
void Display_Date_Time() {
  PrintText(0, 150, "Date/Time:", CYAN, 2);
  tft.fillRect(0, 165, 130, 19 * 2, BLUE);
  PrintText(0, 168, Time, GREEN, 2);
  PrintText(0, 188, Date, GREEN, 2);
}
//###############################################
String Bearing_to_Ordinal(float bearing) {
  if (bearing >= 348.75 || bearing < 11.25)  return "N";
  if (bearing >=  11.25 && bearing < 33.75)  return "NNE";
  if (bearing >=  33.75 && bearing < 56.25)  return "NE";
  if (bearing >=  56.25 && bearing < 78.75)  return "ENE";
  if (bearing >=  78.75 && bearing < 101.25) return "E";
  if (bearing >= 101.25 && bearing < 123.75) return "ESE";
  if (bearing >= 123.75 && bearing < 146.25) return "SE";
  if (bearing >= 146.25 && bearing < 168.75) return "SSE";
  if (bearing >= 168.75 && bearing < 191.25) return "S";
  if (bearing >= 191.25 && bearing < 213.75) return "SSW";
  if (bearing >= 213.75 && bearing < 236.25) return "SW";
  if (bearing >= 236.25 && bearing < 258.75) return "WSW";
  if (bearing >= 258.75 && bearing < 281.25) return "W";
  if (bearing >= 281.25 && bearing < 303.75) return "WNW";
  if (bearing >= 303.75 && bearing < 326.25) return "NW";
  if (bearing >= 326.25 && bearing < 348.75) return "NNW";
  return "?";
}
//##################################################
void PrintText(int x, int y, String text, int colour, byte text_size) {
  tft.setCursor(x, y);
  tft.setTextColor(colour);
  tft.setTextSize(text_size);
  tft.print(text);
  tft.setTextColor(YELLOW); // Default colour
  tft.setTextSize(2);       // Default Text Size
}
//###################################################
static void smartDelay(unsigned long ms) {
  unsigned long start = millis();
  do {
    while (ss.available()) gps.encode(ss.read());
  } while (millis() - start < ms);
}

So what I need, is to be able to decode the Library? or .cpp? or .h? or a combination of these files to enable the Compass Sketch to "see" the GPS data. I can read the files, just can't make sense of them, although I've tried editing the Sketch using code in the library, and Verifying, but then get errors, which I try to fix, but then more errors. So...

How to use an existing library to write a sketch. I assume this is beyond anyone to "teach" over this forum. Thanks for listening to my rant. :slight_smile:

Last sketch post#1 being uploaded to a Mega, then why use software serial? Why not use a hardware serial port for receipt of GPS data?

What kind of errors are you getting?

And please, there are children here!

"it" is what and check what wiring?

An UNO you cannot use

for serial comms. An Uno or a Mega?

Using software serial on an Uno's pin 0 and pin 1 will result in wonkies.

Different pins and different baud rate.

Why use software serial? Because I have another Sketch that seems to work when I put that in there, and I don't know what a hardware serial port is except maybe it's just using pin 19, if I declared it somewhere, but I've tried several revisions of the code and can't get it to work, so I opened the Libraries to read them and try figure out the code to get more than just the Laat and Long to display. Was trying for speed, so when looking at possibly TinyGPS.h, it says

#define _GPS_MPH_PER_KNOT 1.15077945
#define _GPS_MPS_PER_KNOT 0.51444444
#define _GPS_KMPH_PER_KNOT 1.852
#define _GPS_MILES_PER_METER 0.00062137112
#define _GPS_KM_PER_METER 0.001

So I tried entering some of those values in the Sketch, like swapped out Serial.print(gps.speed.mph()); to GPS_MPH_PER_KNOT, then Gps.mph..... I can't seem to figure out how to write the Sketch to read from the Library correctly. I'm a child when it comes to this.

And speaking of children, Oops! Edited that out.

Hey,
"It" refers to the Mega on Serial Monitor. Check wiring from the MEGA to the GPS sensor for TX. Probably Vcc and Grd also, all wiring I guess.
I guess by the "Uno I can't use", means the code
static const int RXPin = 0, TXPin = 1; is only for UNO's?
Software serial ss..... I'm using a Mega.

I think this is all based on the sensor I'm using. Not sure what sensor it was for other than a GPS sensor. That's why I need to figure out how the Libraries or .h file, or .cpp file relates to the sketchs code so I can edit using actual terms I'm allowed to, instead of getting all the errors. What errors? I had them all, trying one thing at a time for days. F("something here"((); improper use of (), TinyGPS does not ugh what was it? recognize gps.speed.mph? GPS was not declared... mph was not... the list goes on depending what edit I had done at the time.

Sorry, the GPS sensor is a GT-U7. I listed it up at the top, but it didn't say GT-U7.

I tried Pin19, 20, went up and down the board trying every pin, started with the TX and RX pins, then SOA and SCL pins, and then every pin available. All while changing the baud rate, 9600, 115200. and monitoring the Serial monitor and the TFT. Then I started editing other code trying to see if from a sketch that displays data, if I could make something work in this sketch... It just gets worse from there!

I'm pinned out!

On an UNO, blank sketch 9600 baud, I get a ton of GPS data! I'd think it should be rather straight forward to edit a sketch and get it to read the data I am receiving. Or even see the same data on the MEGA on the proper pin. Ya know, I don't think I've tried that..... though I have data on the one sketch...

So with the MEGA, I get GPS data with a blank Sketch, uploaded and my GPS sensor connected to Pin1 also. So... how can I transfer that data to another pin ? The TFT display uses up or blocks the pin. OK, more research!

Did you use the associated Serial port name in the sketch (Serial1, Serial2, Serial3) when you tried the associated Rx and Tx pins ?

Arduino - Serial for the Mega.

Use Multiple Serial Ports on the Arduino Mega | Arduino Documentation | Arduino Documentation

I did not! So I should try something like Serial1.begin(9600); but then I also have to call out the port "pins"?

Thanks I will try this!

If you use the Tx1 and Rx1 pins then you need to use

Serial1.begin(baudRate);
if (Serial1.available())
char inChar = Serial1.read();

etc

Oh Yes! It worked!!! At least the MEGA now is able to read the GPS chip on Pin19. I'll continue on and see what else I can get.

Mostly now need to figure out how to read this data and display it on the TFT. I can read Lat and Lon, and they display the data o the TFT, but that's all I can configure to get to display on the TFT.

Thanks!!!

So I played around some and got it to work??? Still testing, but I have data displayed, N,S,E,W, Lat, Lon, Date.....

/*
 * MEGA
 * RED TFT
 * Pin19  to 4th pin on sensor
 * GPS Sensor GT-U7
 * Displays Compass and GPS headers, SEEMS TO WORK!
*/

#include <TinyGPSPlus.h>
#include <SoftwareSerial.h>
static const int RXPin = 0, TXPin = 19;
static const uint32_t GPSBaud = 9600;
#include <Arduino.h>
#define USE_ADAFRUIT_SHIELD_PINOUT 1
#include <Adafruit_GFX.h> 
#include <Adafruit_Sensor.h> 
#include <MCUFRIEND_kbv.h>                                     //added mcufriend.kbv
MCUFRIEND_kbv tft;

//#include "TinyGPS++.h"

TinyGPSPlus gps;


//SoftwareSerial ss(RXPin, TXPin);


#include <SPI.h>
#include "Adafruit_GFX.h"
#include "Adafruit_ILI9341.h"

// Assign names to common 16-bit color values:
#define   BLACK  0x0000
#define   BLUE   0x001F
#define   RED    0xF800
#define   GREEN  0x07E0
#define   CYAN   0x07FF
#define   YELLOW 0xFFE0
#define   ORANGE 0xFD20
#define   WHITE  0xFFFF

String    Time, Date;
float     NumberSats, Latitude, Longitude, Bearing;
float     AltitudeMETRES, AltitudeMILES, AltitudeKM, AltitudeFEET;
float     SpeedKPH, SpeedMPH, SpeedKNOTS, SpeedMPS;

const int centreX  = 230; // Location of the compass display on screen
const int centreY  = 120;
const int diameter = 70; // Size of the compass
int       dx = centreX, dy = centreY;
int       last_dx = centreX, last_dy = centreY - diameter * 0.85;





// For stats that happen every 5 seconds
unsigned long last = 0UL;


void setup()

{
  Serial.begin(115200);
  Serial1.begin(9600); // connect gps sensor
  //ss.begin(GPSBaud);
  Serial.print("What the HECK!");

 //}


//{
  Serial1.begin(9600);
  //ss.begin(9600);         // This opens up communications to the GPS
  tft.begin();            // Start the TFT display
  tft.setRotation(5);     // Rotate screen by 90°
  tft.setTextSize(2);     // Set medium text size
  tft.setTextColor(YELLOW);
  tft.fillScreen(BLUE);
}

void loop() {
  
  Latitude       = gps.location.lat();
  Longitude      = gps.location.lng();
  Date           = String(gps.date.day() < 10 ? "0" : "") + String(gps.date.day()) + "/" + String(gps.date.month() < 10 ? "0" : "") + String(gps.date.month()) + "/" + String(gps.date.year());
  //Date           = String(gps.date.month()<10?"0":"") + String(gps.date.month()) + "/" + String(gps.date.day()<10?"0":"") + String(gps.date.day()) + "/" + String(gps.date.year());
  Time           = String(gps.time.hour() < 10 ? "0" : "")   + String(gps.time.hour())   + ":" +
                   String(gps.time.minute() < 10 ? "0" : "") + String(gps.time.minute()) + ":" +  String(gps.time.hour() < 10 ? "0" : "") + String(gps.time.second() < 10 ? "0" : "") + String(gps.time.second());
  Bearing        = gps.course.deg();
  SpeedKPH       = gps.speed.kmph();
  SpeedMPH       = gps.speed.mph();
  SpeedKNOTS     = gps.speed.knots();
  SpeedMPS       = gps.speed.mps();
  NumberSats     = gps.satellites.value();
  AltitudeMETRES = gps.altitude.meters();
  AltitudeKM     = gps.altitude.kilometers();
  AltitudeMILES  = gps.altitude.miles();
  AltitudeFEET   = gps.altitude.feet();


  while (Serial1.available() > 0)
    gps.encode(Serial1.read());


  if (gps.location.isUpdated())
  {
  Serial.print(F("LOCATION   Fix Age="));
    Serial.print(gps.location.age());
    Serial.print(F("ms Raw Lat="));
    Serial.print(gps.location.rawLat().negative ? "-" : "+");
    Serial.print(gps.location.rawLat().deg);
    Serial.print("[+");
    Serial.print(gps.location.rawLat().billionths);
    Serial.print(F(" billionths],  Raw Long="));
    Serial.print(gps.location.rawLng().negative ? "-" : "+");
    Serial.print(gps.location.rawLng().deg);
    Serial.print("[+");
    Serial.print(gps.location.rawLng().billionths);
    Serial.print(F(" billionths],  Lat="));
    Serial.print(gps.location.lat(), 6);
    Serial.print(F(" Long="));
    Serial.println(gps.location.lng(), 6);
  }

    
  //Serial.println("Time\t\tDate\t\tLAT\tLON\tSATS\tAlt\tBearing\tSpeed(KPH)");
  //Serial.println("----------------------------------------------------------------------------------");
  //Serial.print(Time                   + "\t");
  //Serial.print(Date                   + "\t");
  //Serial.print(String(Latitude, 3)    + "\t");
  //Serial.print(String(Longitude, 3)   + "\t");
  //Serial.print(String(NumberSats)     + "\t");
  //Serial.print(String(AltitudeMETRES) + "\t"); // Select as required
  //Serial.print(String(Bearing)        + "\t");
  //Serial.print(String(SpeedKPH)       + "\t"); // Select as required
  //Serial.print(String(SpeedMPH)     + "\t"); // Select as required
  //Serial.println("\n");
  DisplayGPSdata(NumberSats, Latitude, Longitude, AltitudeMETRES, SpeedKPH, Bearing); // Select units as required
  smartDelay(1000);
  if (millis() > 5000 && gps.charsProcessed() < 10)  Serial.println(F("No GPS data received: check wiring"));
}
//#####################################################################
void DisplayGPSdata(float dNumberSats, float dLatitude, float dLongitude, float dAltitude, float dSpeed, float dBearing) {
  PrintText(60, 0, "   GPS Compass", CYAN, 2);
  tft.fillRect(45, 40, 90, 19 * 4, BLUE);
  PrintText(0, 45, "LAT:" + String(dLatitude), YELLOW, 2);
  PrintText(0, 63, "LON:" + String(dLongitude), YELLOW, 2);
  PrintText(0, 81, "ALT:" + String(dAltitude, 1) + "M", YELLOW, 2);
  PrintText(0, 99, "SAT:" + String(dNumberSats, 0), YELLOW, 2);
  tft.fillRect(80, 220, 120, 18, BLUE);
  PrintText(10, 220, "Speed:" + String(dSpeed) + "kph", YELLOW, 2);
  tft.fillRect(240, 220, 120, 18, BLUE);
  tft.setCursor(200, 220);
  tft.print("Azi: " + Bearing_to_Ordinal(dBearing));
  Display_Compass(dBearing);
  Display_Date_Time();
}
//#####################################################################
void Display_Compass(float dBearing) {
  int dxo, dyo, dxi, dyi;
  tft.setCursor(0, 0);
  tft.drawCircle(centreX, centreY, diameter, WHITE); // Draw compass circle
  for (float i = 0; i < 360; i = i + 22.5) {
    dxo = diameter * cos((i - 90) * 3.14 / 180);
    dyo = diameter * sin((i - 90) * 3.14 / 180);
    dxi = dxo * 0.9;
    dyi = dyo * 0.9;
    tft.drawLine(dxo + centreX, dyo + centreY, dxi + centreX, dyi + centreY, WHITE);
  }
  PrintText((centreX - 5), (centreY - diameter - 18), "N", GREEN, 2);
  PrintText((centreX - 5), (centreY + diameter + 5) , "S", GREEN, 2);
  PrintText((centreX + diameter + 5),  (centreY - 5), "E", GREEN, 2);
  PrintText((centreX - diameter - 15), (centreY - 5), "W", GREEN, 2);
  dx = (0.85 * diameter * cos((dBearing - 90) * 3.14 / 180)) + centreX; // calculate X position
  dy = (0.85 * diameter * sin((dBearing - 90) * 3.14 / 180)) + centreY; // calculate Y position
  draw_arrow(last_dx, last_dy, centreX, centreY, 5, 5, BLUE);   // Erase last arrow
  draw_arrow(dx, dy, centreX, centreY, 5, 5, YELLOW);           // Draw arrow in new position
  last_dx = dx;
  last_dy = dy;
}
//#####################################################################
void draw_arrow(int x2, int y2, int x1, int y1, int alength, int awidth, int colour) {
  float distance;
  int dx, dy, x2o, y2o, x3, y3, x4, y4, k;
  distance = sqrt(pow((x1 - x2), 2) + pow((y1 - y2), 2));
  dx = x2 + (x1 - x2) * alength / distance;
  dy = y2 + (y1 - y2) * alength / distance;
  k = awidth / alength;
  x2o = x2 - dx;
  y2o = dy - y2;
  x3 = y2o * k + dx;
  y3 = x2o * k + dy;
  x4 = dx - y2o * k;
  y4 = dy - x2o * k;
  tft.drawLine(x1, y1, x2, y2, colour);
  tft.drawLine(x1, y1, dx, dy, colour);
  tft.drawLine(x3, y3, x4, y4, colour);
  tft.drawLine(x3, y3, x2, y2, colour);
  tft.drawLine(x2, y2, x4, y4, colour);
}
//#####################################################################
void Display_Date_Time() {
  PrintText(0, 150, "Date/Time:", CYAN, 2);
  tft.fillRect(0, 165, 130, 19 * 2, BLUE);
  PrintText(0, 168, Time, GREEN, 2);
  PrintText(0, 188, Date, GREEN, 2);
}
//#####################################################################
String Bearing_to_Ordinal(float bearing) {
  if (bearing >= 348.75 || bearing < 11.25)  return "N";
  if (bearing >=  11.25 && bearing < 33.75)  return "NNE";
  if (bearing >=  33.75 && bearing < 56.25)  return "NE";
  if (bearing >=  56.25 && bearing < 78.75)  return "ENE";
  if (bearing >=  78.75 && bearing < 101.25) return "E";
  if (bearing >= 101.25 && bearing < 123.75) return "ESE";
  if (bearing >= 123.75 && bearing < 146.25) return "SE";
  if (bearing >= 146.25 && bearing < 168.75) return "SSE";
  if (bearing >= 168.75 && bearing < 191.25) return "S";
  if (bearing >= 191.25 && bearing < 213.75) return "SSW";
  if (bearing >= 213.75 && bearing < 236.25) return "SW";
  if (bearing >= 236.25 && bearing < 258.75) return "WSW";
  if (bearing >= 258.75 && bearing < 281.25) return "W";
  if (bearing >= 281.25 && bearing < 303.75) return "WNW";
  if (bearing >= 303.75 && bearing < 326.25) return "NW";
  if (bearing >= 326.25 && bearing < 348.75) return "NNW";
  return "?";
}
//#####################################################################
void PrintText(int x, int y, String text, int colour, byte text_size) {
  tft.setCursor(x, y);
  tft.setTextColor(colour);
  tft.setTextSize(text_size);
  tft.print(text);
  tft.setTextColor(YELLOW); // Default colour
  tft.setTextSize(2);       // Default Text Size
}
//#####################################################################
static void smartDelay(unsigned long ms) {
  unsigned long start = millis();
  do {
    while (Serial1.available()) gps.encode(Serial1.read());
  } while (millis() - start < ms);
}

Thanks for all the help!!!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.