This will probably go unanswered or at least the answer will be ambiguous as the question.
Parts:
- Arduino Mega.
- 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).
- 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.