Touch screen not working on the sketch.

I have a 5" HMI crowpanel esp32 board. Version 3. I have the touch panel working on the lessons for the crowpanel board. So a small code works fine for the touchscreen. However when I try to implement it into a larger code I can not detect a touch. Below is the code. what am I doing wrong.

include "gfx_conf.h"

include "DFRobot_GNSS.h"

include <Wire.h>

include <SPI.h>

include <PCA9557.h>

include <SD.h>

//#include <Ticker.h>

define SD_CS 10 // Chip select pin

int OIL_CHANGE_MILES = 3000; // Example: miles between oil changes
int OIL_CHANGE_MONTHS = 6; // Example: months between oil changes

define STATE_SPEEDOMETER 0

define STATE_SETTINGS 1

define Oil_Change_Miles_SETTINGS 2

define Oil_Change_Months_SETTINGS 3

DFRobot_GNSS_I2C gnss(&Wire, GNSS_DEVICE_ADDR);

float LAST_OIL_ODOMETER = 0.0; // Example: odometer reading at the last oil change
int LAST_OIL_YEAR = 2024; // Example: year of the last oil change
int LAST_OIL_MONTH = 7; // Example: month of the last oil change
const float reg2rad = .017453292519943295;
const int Min_Speed = 0;
const int Max_Speed = 100;
const int LCDWidth = 800;
const int LCDHieght = 480;
const int GaugeRad = 240;
const int GaugeWidth = 60;
const int yLoc = LCDHieght / 2;
const int xLoc = LCDWidth / 2;
const int GaugeDelay = 50;
const int LCDWidth2 = 400;
const int LCDHeight2 = 350;
const int GaugeRad2 = 175;
const int GaugeWidth3 = 25;
const int GaugeWidth4 = 10;
const int xLoc2 = LCDWidth2 / 2;
const int yLoc2 = LCDHeight2 / 2;
float curSpeed = 0;
int laSpeed = 0;
float odometer = 0.0;
float Tripodometer = 0.0;
unsigned long lastUpdateTime = 0;
float lastOdometer = 0.0;
float lastTripodometer = 0.0;
unsigned long lastOilChangeTime = 0; // Store the time of last oil change
float lastOilOdometer = LAST_OIL_ODOMETER; // Store odometer reading at the last oil change
int lastOilMonth = LAST_OIL_MONTH; // Store month of last oil change
int lastOilYear = LAST_OIL_YEAR; // Store year of last oil change
int speedometernumber = 1;
uint16_t touchX=0, touchY=0;
int currentState = STATE_SPEEDOMETER;
int currentSpeedometer = 1; // 1 to 4
int currentMonth = 1;
int currentYear = 2024;
float milesSinceLastChange=0;
int monthsSinceLastChange =0;

static LGFX_Sprite dial(&tft);
static LGFX_Sprite needle(&tft);

//Ticker ticker;

volatile bool touchDetected = false;
PCA9557 Out;

// sweep gauge in beginning

void setup() {
// Configure the timer to trigger every 10ms (100Hz)
//ticker.attach(1.0, onTimer); // Call toggleLED function every 1 second
//touch timing init
#if defined (CrowPanel_50) || defined (CrowPanel_70)
Wire.begin(19, 20);
Out.reset();
Out.setMode(IO_OUTPUT);
Out.setState(IO0, IO_LOW);
Out.setState(IO1, IO_LOW);
delay(20);
Out.setState(IO0, IO_HIGH);
delay(100);
Out.setMode(IO1, IO_INPUT);
#endif
tft.begin();
tft.setRotation(0); // Optional: Set rotation
tft.fillScreen(TFT_BLACK);
Serial.begin(115200);
Wire.begin(19, 20);
while(!gnss.begin()){
// Serial.println("NO Deivces !");
}
gnss.enablePower();
gnss.setGnss(eGPS_BeiDou_GLONASS);
gnss.setRgbOn();
//Serial.println("Initializing SD card...");
// Initialize SD card
if (!SD.begin(SD_CS)) {
// Serial.println("Card initialization failed!");
}else{
// Serial.println("Card initialized successfully.");
tft.setRotation(0);
readFromSDCard("/odometer.txt", odometer);
readFromSDCard("/tripodometer.txt", Tripodometer);
readFromSDCard("/LAST_OIL_ODOMETER.txt", LAST_OIL_ODOMETER);
readFromSDCard("/LAST_OIL_YEAR.txt", LAST_OIL_YEAR);
readFromSDCard("/LAST_OIL_MONTH.txt", LAST_OIL_MONTH);
// Serial.print("Odometer: ");
//Serial.println(odometer);
//Serial.print("Tripodometer: ");
//Serial.println(Tripodometer);
sTim_t date = gnss.getDate();

uint8_t currentMonth1 = date.month;
uint8_t currentYear1 = date.year;

currentMonth = currentMonth1 ; // Placeholder for the current month, this should be dynamically calculated from GNSS or another source
currentYear = currentYear1; // Placeholder for the current year, this should be dynamically calculated from GNSS or another source

}

delay(1000);
}

void loop() {
static unsigned long lastTouchCheck = 0;
if (millis() - lastTouchCheck > 10) { // Check touch every 10ms
lastTouchCheck = millis();
handleTouch();
}

if (currentState == STATE_SPEEDOMETER){
if(currentSpeedometer==1){
speedometernumber1();
}
if(currentSpeedometer==2){
TriSweep();
}
if(currentSpeedometer==3){
speedometernumber2();
}
}
if (currentState == STATE_SETTINGS){
setup2();
}
if(currentState== Oil_Change_Miles_SETTINGS){
setMilesBetweenOilChanges();
}

// Serial.println("at top of loop");
// Calculate distance traveled in miles for the current second
float distanceThisSecond = (curSpeed / 3600.0); // Convert mph to miles per second
odometer += distanceThisSecond;
Tripodometer += distanceThisSecond;

// Log odometer when it increases by 0.1 miles
if (odometer - lastOdometer >= 0.1) {
writeToSDCard("/odometer.txt", odometer);
lastOdometer = odometer; // Update the last logged odometer value
}

// Log trip odometer when it increases by 0.1 miles
if (Tripodometer - lastTripodometer >= 0.1) {
writeToSDCard("/tripodometer.txt", Tripodometer);
lastTripodometer = Tripodometer; // Update the last logged trip odometer value
}

float currentOdometer = odometer;
checkOilChange(currentOdometer, currentMonth, currentYear);
delay(1000); // Optional delay for readability

laSpeed = curSpeed;

//Serial.println("just before detecting touch");

delay(1000); // Delay for readability
}

void arcwSpeed(int currentSpeed, int lastSpeed) {
// Serial.print("arcwSpeed");
if(curSpeed != laSpeed){
// Serial.println("fill circle");
tft.fillCircle(xLoc, yLoc, GaugeRad + 2, TFT_BLACK);
tft.fillCircle(xLoc, yLoc, GaugeRad + 2, TFT_BLACK);
}
drawGauge(currentSpeed);
int currentSpeedmap = map(currentSpeed, Min_Speed, Max_Speed, 120, 420);
arcSweep(currentSpeedmap);
}

void drawGauge(int speed) {
//Serial.print("drawGauge");
tft.drawCircle(xLoc, yLoc, GaugeRad - GaugeWidth + 1, TFT_RED);
tft.drawCircle(xLoc, yLoc, GaugeRad - GaugeWidth + 2, TFT_RED);
tft.drawCircle(xLoc, yLoc, GaugeRad, TFT_RED);
tft.drawCircle(xLoc, yLoc, GaugeRad + 1, TFT_RED);
tft.setCursor(380, 180);
tft.setTextColor(TFT_WHITE, TFT_BLACK);
tft.setTextSize(7);
tft.print(speed);
tft.setCursor(360, 250);
tft.setTextSize(4);
tft.print("MPH");
}

void arcSweep(int current_speed) {
//Serial.print("arcSweep");
byte inc = 6; // Increment in degrees for each segment
byte seg = 6; // Width of each segment in degrees
uint16_t color = TFT_RED;

for (int j =120 ; j <current_speed+seg; j += inc) {

// Calculate coordinates for the triangles
float x1 = (GaugeRad - GaugeWidth) * cos(j * reg2rad) + xLoc;
float y1 = (GaugeRad - GaugeWidth) * sin(j * reg2rad) + yLoc;

float x2 = GaugeRad * cos(j * reg2rad) + xLoc;
float y2 = GaugeRad * sin(j * reg2rad) + yLoc;

float x3 = (GaugeRad - GaugeWidth) * cos((j + seg) * reg2rad) + xLoc;
float y3 = (GaugeRad - GaugeWidth) * sin((j + seg) * reg2rad) + yLoc;

float x4 = GaugeRad * cos((j + seg) * reg2rad) + xLoc;
float y4 = GaugeRad * sin((j + seg) * reg2rad) + yLoc;

// Draw the two triangles for the current segment
tft.fillTriangle(x1, y1, x2, y2, x3, y3, color);
tft.fillTriangle(x2, y2, x3, y3, x4, y4, color);

}
}

void drawTime() {
// Get UTC time from GNSS
sTim_t utc = gnss.getUTC();
//Serial.print("utc");
//Serial.println(utc.hour);
// Example time zone offset in hours
// Replace with a dynamic calculation based on GPS location if needed
int timeZoneOffset = getTimeZoneOffsetFromGPS();
//Serial.print("Offset=");
//Serial.println(timeZoneOffset);

// Adjust hour for time zone
int localHour = (utc.hour + timeZoneOffset) % 24;
if (localHour < 0) localHour += 24; // Handle negative hour values

// Convert to 12-hour format
int hour12 = localHour % 12;
//Serial.print("hour12");
//Serial.println(hour12);
if (hour12 == 0) hour12 = 12;
const char *period = (localHour < 12) ? "AM" : "PM";

// Draw time on the TFT display
//tft.fillRect(641, 420, 100, 20, TFT_RED);
tft.setCursor(641, 420);
tft.setTextColor(TFT_WHITE, TFT_BLACK);
tft.setTextSize(4);
if (hour12 > 9) tft.setCursor(631, 420); ; // Add leading zero if needed
tft.print(hour12); // Print hour in 12-hour format
tft.setCursor(660, 420);
tft.print(":");
tft.setCursor(680, 420);
if (utc.minute < 10) tft.print("0"); // Add leading zero if needed
tft.print(utc.minute);
tft.setCursor(730, 420);
tft.print("");
tft.print(period); // Print AM or PM
}

int getTimeZoneOffsetFromGPS() {
// Retrieve latitude and longitude as sLonLat_t structures
sLonLat_t latitude = gnss.getLat(); // Retrieve latitude
sLonLat_t longitude = gnss.getLon(); // Retrieve longitude

// Extract the longitude value in degrees
float lon = longitude.lonitudeDegree; // Use the corrected member name

// Use the longitude to determine the time zone
int offset = 0;
if (lon <= 67.5 && lon > 52.5) {
offset = -4; // Atlantic Time Zone
}
if (lon <= 82.5 && lon > 67.5) {
offset = -5; // Eastern Time Zone
}
if (lon <= 97.5 && lon > 82.5) {
offset = -6; // Central Time Zone
//.print("offset=-6");
}
if (lon <= 112.5 && lon > 97.5) {
offset = -7; // Mountain Time Zone
}
if (lon <= 127.5 && lon > 112.5) {
offset = -8; // Pacific Time Zone
}

// Adjust for daylight saving time (DST)
// Get the current date and time
sTim_t date = gnss.getDate();

// Extract the month into a variable
uint8_t currentMonth1 = date.month;
// Serial.println("currentMonth");
// Serial.println(currentMonth1);
// Serial.print("gps month");
// Serial.println(date.month);
int currentMonth = currentMonth1; // Use the current month dynamically
if (currentMonth >= 3 && currentMonth <= 11) {
offset += 1; // Add 1 hour for DST during March-November
}

return offset;
}

void drawCoordinates() {
sLonLat_t latitude = gnss.getLat();
sLonLat_t longitude = gnss.getLon();
//tft.fillRect(22, 410, 120, 25, TFT_RED);
tft.setCursor(22, 410); // Lower-left corner
tft.setTextColor(TFT_WHITE, TFT_BLACK); // White text with black background
tft.setTextSize(3);
tft.print("Lat: ");
tft.print(latitude.latitudeDegree, 3);
//tft.fillRect(22, 440, 120, 25, TFT_RED);
tft.setCursor(22, 440);
tft.print("Lon: ");
tft.print(longitude.lonitudeDegree, 3);
}

void drawSatellites() {
// Get the number of satellites acquired
int numSatellites = gnss.getNumSatUsed();

// Get the altitude
float altitude = gnss.getAlt(); // Assuming getAltitude() returns altitude in meters

// Display the number of satellites in the upper-left corner
//tft.fillRect(10, 0, 250, 100, TFT_RED);
tft.setCursor(10, 10); // Upper-left corner
tft.setTextColor(TFT_WHITE, TFT_BLACK); // White text with black background
tft.setTextSize(3);
tft.print("Sat: ");
tft.print(numSatellites);

// Display the altitude below the number of satellites
tft.setCursor(10, 40); // Set the cursor for altitude display
tft.setTextSize(3); // Smaller text size for altitude
tft.print("Alt: ");
tft.print(altitude, 2); // Print altitude to 2 decimal places
}

void drawSetupIcon() {
int iconX = LCDWidth - 125; // X position for the icon (upper-right corner)
int iconY = 30; // Y position for the icon

// Draw the gear icon (circle with lines as teeth)
int gearRadius = 20; // Radius of the gear

// Draw the gear body
tft.drawCircle(iconX, iconY, gearRadius, TFT_WHITE); // Gear body

// Draw the gear teeth (small lines around the circle)
for (int i = 0; i < 8; i++) { // 8 teeth on the gear
int angle = i * 45; // 45-degree increments
int x1 = iconX + (gearRadius - 5) * cos(angle * reg2rad); // Outer X position of teeth
int y1 = iconY + (gearRadius - 5) * sin(angle * reg2rad); // Outer Y position of teeth
int x2 = iconX + (gearRadius) * cos(angle * reg2rad); // Inner X position of teeth
int y2 = iconY + (gearRadius) * sin(angle * reg2rad); // Inner Y position of teeth

tft.drawLine(x1, y1, x2, y2, TFT_WHITE);  // Draw each tooth as a line

}

// Draw the word "SETUP" to the right of the icon
tft.setCursor(iconX + gearRadius + 10, iconY - 10); // Adjust position to the right of the gear
tft.setTextSize(3); // Set text size to 3
tft.setTextColor(TFT_WHITE, TFT_BLACK); // Set text color
tft.print("SETUP"); // Print "SETUP" text
}

void drawOdometer(float odometerValue) {
// Clear previous odometer display (optional)
// tft.fillRect(xLoc - 25, 300, 162, 25, TFT_BLACK);
// Display the odometer reading (8 significant digits)
tft.setCursor(xLoc-25, 300);
tft.setTextColor(TFT_WHITE, TFT_BLACK);
tft.setTextSize(3);
tft.print(odometerValue, 1); // Display 8 significant digits
}

void drawtripOdometer(float odometertripValue) {
// Clear previous odometer display (optional)
//tft.fillRect(xLoc - 25, 330, 162, 25, TFT_BLACK);
// Display the odometer reading (8 significant digits)
tft.setCursor(xLoc-115, 330);
tft.setTextColor(TFT_WHITE, TFT_BLACK);
tft.setTextSize(3);
tft.print("Trip: ");
tft.setCursor(385, 330); // Place the odometer below MPH
tft.print(odometertripValue, 1); // Display 8 significant digits
}

void drawDirection() {
// Get heading from GNSS (assuming getHeading() returns heading in degrees)
float heading = gnss.getCog();

// Determine the direction based on the heading
const char* direction;
if ((heading >= 337.5 && heading <= 360) || (heading >= 0 && heading < 22.5)) {
direction = "N";
} else if (heading >= 22.5 && heading < 67.5) {
direction = "NE";
} else if (heading >= 67.5 && heading < 112.5) {
direction = "E";
} else if (heading >= 112.5 && heading < 157.5) {
direction = "SE";
} else if (heading >= 157.5 && heading < 202.5) {
direction = "S";
} else if (heading >= 202.5 && heading < 247.5) {
direction = "SW";
} else if (heading >= 247.5 && heading < 292.5) {
direction = "W";
} else if (heading >= 292.5 && heading < 337.5) {
direction = "NW";
} else {
direction = "--"; // Default if heading is invalid
}

// Clear previous direction display
//tft.fillRect(380, 100, 50, 50, TFT_BLACK);

// Display the direction on the TFT
tft.setCursor(380, 100);
tft.setTextColor(TFT_WHITE, TFT_BLACK); // White text with black background
tft.setTextSize(6);
tft.print(direction);
}

void writeToSDCard(const char *path, float value) {
File file = SD.open(path, FILE_WRITE);
if (file) {
file.println(value, 1); // Write the float value with up to 1 decimal places
file.close();
//.print("Written to ");
// Serial.print(path);
// Serial.print(": ");
// Serial.println(value, 1);
} else {
// Serial.print("Failed to open ");
// Serial.print(path);
// Serial.println(" for writing.");
}
// Serial.println("finished write to sd");
}

template
bool readFromSDCard(const char *path, T &value) {
File file = SD.open(path, FILE_READ);
if (file) {
String content = file.readStringUntil('\n'); // Read content as a string
if constexpr (std::is_same<T, int>::value) {
value = content.toInt(); // Convert to int if T is int
} else if constexpr (std::is_same<T, float>::value) {
value = content.toFloat(); // Convert to float if T is float
}
file.close(); // Close the file after reading
return true; // Successfully read the value
} else {
value = 0; // Default value if file does not exist or cannot be read
return false; // Reading failed
}
}

/float readFromSDCard(const char *path, float &value) {
File file = SD.open(path, FILE_READ);
if (file) {
String content = file.readStringUntil('\n');
value = content.toFloat(); // Convert the string to a float
//Serial.print("Read from ");
//Serial.print(path);
//Serial.print(": ");
// Serial.println(value, 1);
return value;
file.close();
} else {
// Serial.print("Failed to open ");
// Serial.print(path);
// Serial.println(" for reading.");
value = 0.0; // Default value if file does not exist or cannot be read
}
}
/

void checkOilChange(float currentOdometer, int currentMonth, int currentYear) {
// Serial.println("made it to checkOilChange");
// Calculate the mileage since last oil change
milesSinceLastChange = currentOdometer - lastOilOdometer;
//Serial.print("milesSinceLastChange");
//Serial.println(milesSinceLastChange);

// Calculate the months since last oil change
monthsSinceLastChange = (lastOilMonth -currentMonth);
//Serial.print("monthsSinceLastChange");
//Serial.println(monthsSinceLastChange);
// Check if either mileage or time threshold is exceeded
if (milesSinceLastChange >= OIL_CHANGE_MILES || monthsSinceLastChange >= OIL_CHANGE_MONTHS) {
//.println("made it to if statement");
// Display oil change needed message on the TFT screen
//tft.fillRect(0, 0, LCDWidth, 50, TFT_BLACK); // Clear previous message
tft.setCursor(0, 250); // Set cursor at the top-left corner
tft.setTextColor(TFT_RED, TFT_BLACK); // Red text on black background
tft.setTextSize(4); // Set text size to 5
tft.print("Change");
tft.setCursor(0, 280); // Set cursor at the top-left corner
tft.setTextColor(TFT_RED, TFT_BLACK); // Red text on black background
tft.setTextSize(4); // Set text size to 5
tft.print("Oil");
}
else {
// Clear the message if oil change is not needed
tft.fillRect(0, 0, LCDWidth, 50, TFT_BLACK); // Clear the message
}
//Serial.println("made it through check oil change");
}

void speedometernumber1(){
//Serial.println("speedometer 2");
laSpeed = curSpeed;
delay(125);
float currentOdometer = odometer;
curSpeed = gnss.getSog();
curSpeed = curSpeed * 1.15078;
arcwSpeed(curSpeed, laSpeed);
drawOdometer(odometer);
drawtripOdometer(Tripodometer);
drawDirection(); // Update direction
drawCoordinates();
drawSatellites();
drawSetupIcon();
drawTime();
}

void TriSweep(){
drawOdometer(odometer);
drawtripOdometer(Tripodometer);
drawDirection(); // Update direction
drawCoordinates();
drawSatellites();
drawSetupIcon();
drawTime();
for (int i=0; i<99; i++){
tft.setCursor(380, 210);
tft.setTextColor(TFT_WHITE, TFT_BLACK);
tft.setTextSize(7);
tft.print(i);
tft.setCursor(480, 225);
tft.setTextSize(4);
tft.print("MPH");
// Serial.println("TriSweep");
int MapWidth =map(i, Min_Speed, Max_Speed, 0, LCDWidth);
int Heightmap = (200 - MapWidth/8);
// tft.fillRectangle(0,200TFT_BLACK);
tft.fillTriangle(0, 200, MapWidth, 200, MapWidth, Heightmap, TFT_GREEN);
delay(GaugeDelay);
}
for (int i=99; i>0; i-=1){
tft.setCursor(380, 210);
tft.fillRect(380,210,80,60, TFT_BLACK);
tft.setTextColor(TFT_WHITE, TFT_BLACK);
tft.setTextSize(7);
tft.print(i);
// Serial.print("i=");
// Serial.println(i);
tft.setCursor(480, 225);
tft.setTextSize(4);
tft.print("MPH");
int MapWidth =map(i, Min_Speed, Max_Speed, 0, LCDWidth);
// Serial.print("MapWidth=");
// Serial.println(MapWidth);
int Heightmap = (200 - MapWidth/8);
// Serial.print("Heightmap=");
//Serial.println(Heightmap);
tft.fillTriangle(0, 200, MapWidth, 200, MapWidth, Heightmap, TFT_GREEN);
delay(GaugeDelay);
tft.fillTriangle(0, 200, MapWidth, 200, MapWidth, Heightmap, TFT_BLACK);

// delay(GaugeDelay);
}
}

void speedometernumber2(){
drawOdometer2(odometer);
drawtripOdometer2(Tripodometer);
drawDirection2(); // Update direction
drawCoordinates();
drawSatellites();
drawSetupIcon();
drawTime();
createNeedle();
for (int rot = 0; rot <= 270; rot += 5) {
plotGauge(rot, "MPH");
delay(25);
yield();
}
}

void plotGauge(int16_t angle, String label) {
drawBackground(angle);
// Draw the needle
needle.pushRotated(&dial, angle, TFT_TRANSPARENT);
dial.pushSprite(200,100, TFT_TRANSPARENT);
//dial.deleteSprite(); // Free memory
}

void drawBackground(int16_t speed) {
dial.setColorDepth(8);
dial.createSprite(LCDWidth2, LCDHeight2); // Create sprite to draw background on
dial.setPivot(xLoc2, yLoc2); // Set pivot point for drawing
tft.setPivot(xLoc2, yLoc2); // Set pivot point for display

// Fill the background with black
dial.fillScreen(TFT_BLACK);
// Draw rounded rectangles for the background frame
dial.drawRoundRect(0, 0, LCDWidth2, LCDHeight2 - 2, 10, TFT_WHITE);
dial.drawRoundRect(2, 2, LCDWidth2 - 4, LCDHeight2 - 6, 8, TFT_WHITE);
dial.drawRoundRect(4, 4, LCDWidth2 - 8, LCDHeight2 - 10, 6, TFT_WHITE);
// Draw the circular gauge background
dial.fillCircle(LCDWidth2 / 2, LCDHeight2 / 2, GaugeRad2, TFT_WHITE);
dial.fillCircle(LCDWidth2 / 2, LCDHeight2 / 2, GaugeRad2 + 2, TFT_RED);
dial.fillRoundRect(LCDWidth2 / 2, LCDHeight2 / 2, LCDWidth2 / 2 - 11, LCDHeight2 / 2 - 12, 5, TFT_RED);
dial.fillRoundRect(LCDWidth2 / 2, LCDHeight2 / 2, LCDWidth2 / 2 - 13, LCDHeight2 / 2 - 14, 5, TFT_RED);
dial.fillCircle(LCDWidth2 / 2, LCDHeight2 / 2, GaugeRad2 - 3, TFT_WHITE);
dial.fillCircle(LCDWidth2 / 2, LCDHeight2 / 2, 5, TFT_RED);
dial.fillRoundRect(LCDWidth2/2 + 4, LCDHeight2/2 + 4,LCDWidth2/2 - 21, LCDHeight2/2 -21, 4, TFT_BLACK);
// Draw ticks for the gauge
for (int i = 0; i < 271; i += 20) {
    float sx = cos((i - 270) * reg2rad);
    float sy = sin((i - 270) * reg2rad);
    uint16_t x0 = sx * (GaugeRad2 - GaugeWidth3) + xLoc2;
    uint16_t y0 = sy * (GaugeRad2 - GaugeWidth3) + yLoc2;
    uint16_t x1 = sx * (GaugeRad2) + xLoc2;
    uint16_t y1 = sy * (GaugeRad2) + yLoc2;
    dial.drawLine(x0, y0, x1, y1, TFT_BLUE);
}
 for (int j = 0; j < 270; j+=2){
  float sx = cos((j - 270) * reg2rad);
  float sy = sin((j - 270) * reg2rad);
  uint16_t x0 = sx * (GaugeRad2 - GaugeWidth4) + xLoc2;
  uint16_t y0 = sy * (GaugeRad2 - GaugeWidth4) + yLoc2;
  uint16_t x1 = sx * (GaugeRad2) + xLoc2;
  uint16_t y1 = sy * (GaugeRad2) + yLoc2;    
  dial.drawLine(x0, y0, x1, y1, TFT_BLACK);
}

// Map speed to display it on the gauge
int speedU = map(speed, 0, 270, 0, 120);
dial.drawNumber(speedU, 250, 240, 7);
dial.drawString("MPH", 250, 300, 4);

}

void createNeedle() {
needle.setColorDepth(8);
needle.createSprite(20, 175);
needle.fillSprite(TFT_TRANSPARENT);
needle.fillCircle(10, 10, 5, TFT_RED);
needle.fillRect(8, 2, 4, 175, TFT_RED);
needle.fillTriangle(6, 4, 14, 4, 10, 94, TFT_RED);
needle.fillCircle(10, 10, 8, TFT_BLACK);
needle.setPivot(10, 10);
}

void drawOdometer2(float odometerValue) {
// Clear previous odometer display (optional)
// tft.fillRect(xLoc - 25, 300, 162, 25, TFT_BLACK);
// Display the odometer reading (8 significant digits)
tft.setCursor(xLoc-25, 20);
tft.setTextColor(TFT_WHITE, TFT_BLACK);
tft.setTextSize(3);
tft.print(odometerValue, 1); // Display 8 significant digits
}

void drawtripOdometer2(float odometertripValue) {
// Clear previous odometer display (optional)
//tft.fillRect(xLoc - 25, 330, 162, 25, TFT_BLACK);
// Display the odometer reading (8 significant digits)
tft.setCursor(xLoc-125, 60);
tft.setTextColor(TFT_WHITE, TFT_BLACK);
tft.setTextSize(3);
tft.print("Trip: ");
tft.setCursor(375, 60); // Place the odometer below MPH
tft.print(odometertripValue, 1); // Display 8 significant digits
}

void drawDirection2() {
// Get heading from GNSS (assuming getHeading() returns heading in degrees)
float heading = gnss.getCog();

// Determine the direction based on the heading
const char* direction;
if ((heading >= 337.5 && heading <= 360) || (heading >= 0 && heading < 22.5)) {
direction = "N";
} else if (heading >= 22.5 && heading < 67.5) {
direction = "NE";
} else if (heading >= 67.5 && heading < 112.5) {
direction = "E";
} else if (heading >= 112.5 && heading < 157.5) {
direction = "SE";
} else if (heading >= 157.5 && heading < 202.5) {
direction = "S";
} else if (heading >= 202.5 && heading < 247.5) {
direction = "SW";
} else if (heading >= 247.5 && heading < 292.5) {
direction = "W";
} else if (heading >= 292.5 && heading < 337.5) {
direction = "NW";
} else {
direction = "--"; // Default if heading is invalid
}

// Clear previous direction display
//tft.fillRect(380, 100, 50, 50, TFT_BLACK);

// Display the direction on the TFT
tft.setCursor(650, 250);
tft.setTextColor(TFT_WHITE, TFT_BLACK); // White text with black background
tft.setTextSize(6);
tft.print(direction);
}

void setup2(){
tft.fillScreen(TFT_BLUE);
tft.setTextColor(TFT_WHITE);
tft.setCursor(10, 10);
tft.setTextSize(5);
tft.println("Settings Menu");

tft.fillRect(51, 36, 212, 36, TFT_GREEN); // Button 1
tft.fillRect(51, 93, 212, 36, TFT_GREEN); // Button 2
tft.fillRect(51, 167, 212, 36, TFT_GREEN); // Button 3
tft.fillRect(51, 248, 212, 36, TFT_GREEN); // Button 4
tft.fillRect(51, 340, 212, 36, TFT_GREEN); // Button 5
tft.fillRect(370, 36, 212, 78, TFT_GREEN); // Button 6
tft.fillRect(370, 143, 212, 78, TFT_GREEN); // Button 7
tft.fillRect(370, 241, 212, 78, TFT_GREEN); // Button 8
tft.fillRect(370, 351, 212, 36, TFT_GREEN); // Button 9
tft.fillRect(370, 423, 118, 36, TFT_GREEN); // Button 10

tft.setCursor(51, 36);
tft.setTextColor(TFT_RED, TFT_GREEN);
tft.setTextSize(5);
tft.print("Speedometer #1");

tft.setCursor(51, 93);
tft.setTextColor(TFT_RED, TFT_GREEN);
tft.setTextSize(5);
tft.print("Speedometer #2");

tft.setCursor(51, 167);
tft.setTextColor(TFT_RED, TFT_GREEN);
tft.setTextSize(5);
tft.print("Speedometer #3");

tft.setCursor(51, 248);
tft.setTextColor(TFT_RED, TFT_GREEN);
tft.setTextSize(5);
tft.print("Speedometer #4");

tft.setCursor(51, 340);
tft.setTextColor(TFT_RED, TFT_GREEN);
tft.setTextSize(5);
tft.print("Oil has been changed");

tft.setCursor(370, 36);
tft.setTextColor(TFT_RED, TFT_GREEN);
tft.setTextSize(5);
tft.print("Miles since last oil");
tft.setCursor(370, 40);
tft.setTextColor(TFT_RED, TFT_GREEN);
tft.setTextSize(5);
tft.print("change");
tft.setCursor(370, 45);
tft.setTextColor(TFT_RED, TFT_GREEN);
tft.setTextSize(5);
tft.print(milesSinceLastChange);

tft.setCursor(370, 141);
tft.setTextColor(TFT_RED, TFT_GREEN);
tft.setTextSize(5);
tft.print("Months since last oil");
tft.setCursor(370, 146);
tft.setTextColor(TFT_RED, TFT_GREEN);
tft.setTextSize(5);
tft.print("change");
tft.setCursor(375, 146);
tft.setTextColor(TFT_RED, TFT_GREEN);
tft.setTextSize(5);
tft.print(monthsSinceLastChange);

tft.setCursor(370, 239);
tft.setTextColor(TFT_RED, TFT_GREEN);
tft.setTextSize(5);
tft.print("Input miles between oil");
tft.setCursor(370, 242);
tft.setTextColor(TFT_RED, TFT_GREEN);
tft.setTextSize(5);
tft.print("changes");

tft.setCursor(370, 351);
tft.setTextColor(TFT_RED, TFT_GREEN);
tft.setTextSize(5);
tft.print("Input months between oil");
tft.setCursor(370, 356);
tft.setTextColor(TFT_RED, TFT_GREEN);
tft.setTextSize(5);
tft.print("changes");

tft.setCursor(370, 425);
tft.setTextColor(TFT_RED, TFT_GREEN);
tft.setTextSize(5);
tft.print("exit");

}

/*void onTimer() {
//Serial.println("Interrupt occured");
bool touched = tft.getTouch(&touchX, &touchY);
//Serial.println(touchX);
delay(500);
if (touched) {
Serial.println("made a touch");
if (currentState == STATE_SPEEDOMETER) {
// Check if touch is in upper-right corner
if (touchX > 700 && touchY < 100) { // 100px x 100px in upper-right corner
currentState = STATE_SETTINGS;
}
} else if (currentState == STATE_SETTINGS) {
setup2();
// Check if one of the speedometer buttons is touched
if (touchX > 50 && touchX < 266 && touchY < 74 && touchY >32) currentSpeedometer = 1; // Button 1
else if (touchX > 50 && touchX < 266 && touchY < 129 && touchY >94) currentSpeedometer = 2; // Button 2
else if (touchX > 50 && touchX < 266 && touchY < 203 && touchY >166) currentSpeedometer = 3; // Button 3
else if (touchX > 50 && touchX < 266 && touchY < 286 && touchY >251) currentSpeedometer = 4; // Button 4
else if (touchX > 50 && touchX < 321 && touchY < 388 && touchY >338){ // Button 5
LAST_OIL_ODOMETER = odometer;
writeToSDCard("/LAST_OIL_ODOMETER.txt", LAST_OIL_ODOMETER);
LAST_OIL_YEAR = currentYear;
writeToSDCard("/LAST_OIL_YEAR.txt", LAST_OIL_YEAR);
LAST_OIL_MONTH = currentMonth;
writeToSDCard("/LAST_OIL_MONTH.txt", LAST_OIL_MONTH);}
else if (touchX > 373 && touchX < 668 && touchY < 307 && touchY >371){
currentState = Oil_Change_Miles_SETTINGS;
}
else if (touchX > 373 && touchX < 694 && touchY < 395 && touchY >351){
currentState = Oil_Change_Months_SETTINGS;
setMonthsBetweenOilChanges();}
}
else if(currentState = Oil_Change_Miles_SETTINGS){

      if (touchX > 304 && touchX < 504 && touchY >36 && touchY<72){
          OIL_CHANGE_MILES=1000;
          currentState=STATE_SETTINGS;}
      else if (touchX > 304 && touchX<504 && touchY>93 && touchY<129){
        OIL_CHANGE_MILES=1500;
        currentState=STATE_SETTINGS;
      }


        //    if()
       //else  if (touchX > 373 && touchX < 487 && touchY < 392 && touchY >353) speedometer();  // Button 10
    //else return;

    //currentState = STATE_SPEEDOMETER;
    //showSpeedometer(currentSpeedometer);
  }

}
touchX=0;
touchY=0;
//Serial.println("Made it to end of interrupt");
}*/

void setMilesBetweenOilChanges(){
tft.fillScreen(TFT_BLUE);
tft.setTextColor(TFT_WHITE);
tft.setCursor(50, 200);
tft.setTextSize(5);
tft.println("Miles between");
tft.setCursor(10, 20);
tft.println("oil changes");

tft.fillRect(304, 36, 200, 36, TFT_GREEN); // Button 1
tft.fillRect(304, 93, 200, 36, TFT_GREEN); // Button 2
tft.fillRect(304, 167, 200, 36, TFT_GREEN); // Button 3
tft.fillRect(304, 248, 200, 36, TFT_GREEN); // Button 4
tft.fillRect(304, 340, 200, 36, TFT_GREEN); // Button 5
tft.fillRect(70, 373, 100, 36, TFT_GREEN); // Button 5

tft.setCursor(350, 48);
tft.setTextColor(TFT_RED, TFT_GREEN);
tft.setTextSize(5);
tft.print("1000 miles");

tft.setCursor(350, 105);
tft.setTextColor(TFT_RED, TFT_GREEN);
tft.setTextSize(5);
tft.print("1500 miles");

tft.setCursor(350, 179);
tft.setTextColor(TFT_RED, TFT_GREEN);
tft.setTextSize(5);
tft.print("2000 miles");

tft.setCursor(350, 260);
tft.setTextColor(TFT_RED, TFT_GREEN);
tft.setTextSize(5);
tft.print("2500 miles");

tft.setCursor(350, 352);
tft.setTextColor(TFT_RED, TFT_GREEN);
tft.setTextSize(5);
tft.print("3000 miles");

tft.setCursor(82, 385);
tft.setTextColor(TFT_RED, TFT_GREEN);
tft.setTextSize(5);
tft.print("EXIT");
}

void setMonthsBetweenOilChanges(){
tft.fillScreen(TFT_BLUE);
tft.setTextColor(TFT_WHITE);
tft.setCursor(50, 200);
tft.setTextSize(5);
tft.println("Months between");
tft.setCursor(50, 220);
tft.println("oil changes");

tft.fillRect(317, 68, 212, 36, TFT_GREEN); // Button 1
tft.fillRect(317, 325, 212, 36, TFT_GREEN); // Button 2
tft.fillRect(86, 350, 140, 36, TFT_GREEN); // Button 1

tft.setCursor(337, 80);
tft.setTextColor(TFT_RED, TFT_GREEN);
tft.setTextSize(5);
tft.print("3 Months");

tft.setCursor(337, 358);
tft.setTextColor(TFT_RED, TFT_GREEN);
tft.setTextSize(5);
tft.print("6 Months");

tft.setCursor(108, 378);
tft.setTextColor(TFT_RED, TFT_GREEN);
tft.setTextSize(5);
tft.print("EXIT");

}

void handleTouch() {
if (tft.getTouch(&touchX, &touchY)) {
Serial.println("Touch detected");
// Handle the touch logic based on touchX and touchY
processTouch(touchX, touchY);
}
}

void processTouch(int x, int y) {
// Implement touch processing logic here
if (currentState == STATE_SPEEDOMETER && x > 700 && y < 100) {
currentState = STATE_SETTINGS;
// Additional logic
}
// Add other touch-handling cases
}

Sign In or Register to comment.