Help with TFT LCD adafruit

I am trying to code a TFT LCD and I've created a UI, but the code for the UI isn't working. The UI is meant to work so that it checks if the first button is pressed before checking if the next set of buttons can be pressed, but it is checking for the second set of buttons even the the first hasn't been pressed

// create numbers for the vari
#include <Adafruit_GFX.h>
#include <Adafruit_TFTLCD.h>
#include <TouchScreen.h>




#define LCD_CS A3
#define LCD_CD A2
#define LCD_WR A1
#define LCD_RD A0
#define LCD_RESET A4
// these pins define the pins being used by the Arduino^^
#define TS_MINX 118
#define TS_MINY 106
#define TS_MAXX 950
#define TS_MAXY 933
#define YP A3
#define XM A2
#define YM 9
#define XP 8
// this code calibrates the screen
Adafruit_TFTLCD tft(LCD_CS, LCD_CD, LCD_WR, LCD_RD, LCD_RESET);



TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);
// these define the pins used to activate the screen
#define BLACK 0x0000
#define BLUE 0x001F
#define RED 0xF800
#define GREEN 0x07E0
#define CYAN 0x07FF
#define MAGENTA 0xf81F
#define YELLOW 0xFFE0
#define WHITE 0xFFFF




int currentscreen;
bool Camera = false;
bool tracker = false;
bool Homevar = false;
bool backbutton = true;




void Home() {
  tft.fillScreen(RED);
  tft.setCursor(60, 100);
  tft.setTextSize(2);
  tft.print("CHOOSE APPLICATION");
  tft.fillRect(10, 10, 75, 75, BLUE);
  tft.fillRect(125, 10, 75, 75, BLUE);
  tft.fillRect(240, 10, 75, 75, BLUE);
  // creating buttons for applications, first test out using LED's
}
void Welcome() {
  tft.fillScreen(WHITE);                //WHat to fill the screen colour with- colours stated above
  tft.drawRect(0, 0, 319, 240, WHITE);  //the rectangularshape that the screen fills
  tft.setCursor(5, 5);                  //set cursor is where text will begin on the screen, top right corner of the text
  tft.setTextColor(BLACK);
  tft.setTextSize(2);
  tft.print("WELCOME LKAT");
  //add parameters
  tft.fillRect(50, 180, 210, 40, BLACK);
  tft.drawRect(50, 180, 210, 40, GREEN);
  tft.setCursor(60, 190);
  tft.setTextColor(WHITE);
  tft.setTextSize(2);
}


void LED() {
  tft.fillScreen(BLUE);
  tft.print("HELLO");
}




void setup() {


  tft.reset();
  uint16_t identifier = tft.readID();
  tft.begin(identifier);
  tft.setRotation(1);  //changing this value between 1 and 0 will change the layout of the text on screen. landscape portarait
  currentscreen = 0;
  Welcome();
  Serial.println(currentscreen);
    Serial.println(Homevar);
}


void loop() {
  TSPoint p = ts.getPoint();
  if (p.z > ts.pressureThreshhold) {
    p.x = map(p.x, TS_MAXX, TS_MINX, 0, 320);
    p.y = map(p.y, TS_MAXY, TS_MINY, 0, 480);
    pinMode(XM, OUTPUT);
    pinMode(YP, OUTPUT);
    //set screen resolution
  }

 // tft.fillRect(p.y, p.x, 5, 5, BLACK);
    

  if (currentscreen == 0) { 
    if ((p.x >= 50 && p.x <= 260) && (p.y >= 180 && p.y <= 250)) {
      
      tft.print("hi ");
      currentscreen = 1;
      Homevar = !Homevar;
      Home();
    }
    }
    
    
//     } 
//     // else {
//     //   tft.print();
//     // }
//   }
  
    
  if (currentscreen == 1 &&  Homevar == true){){
      if( p.x >10 && p.x < 85 && p.y > 10 && p.y < 85 &&  Homevar == true){
 LED();
     }
  
   }

   }

Your topic has been moved to the Programming category of the forum because it does not relate to the operation of the IDE itself

Your sketch has editing mistakes. Show the original sketch. I corrected what I could find... I have not tested this (no equipment). This compiles...

// create numbers for the vari
#include <Adafruit_GFX.h>
#include <Adafruit_TFTLCD.h>
#include <TouchScreen.h>




#define LCD_CS A3
#define LCD_CD A2
#define LCD_WR A1
#define LCD_RD A0
#define LCD_RESET A4
// these pins define the pins being used by the Arduino^^
#define TS_MINX 118
#define TS_MINY 106
#define TS_MAXX 950
#define TS_MAXY 933
#define YP A3
#define XM A2
#define YM 9
#define XP 8
// this code calibrates the screen
Adafruit_TFTLCD tft(LCD_CS, LCD_CD, LCD_WR, LCD_RD, LCD_RESET);



TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);
// these define the pins used to activate the screen
#define BLACK 0x0000
#define BLUE 0x001F
#define RED 0xF800
#define GREEN 0x07E0
#define CYAN 0x07FF
#define MAGENTA 0xf81F
#define YELLOW 0xFFE0
#define WHITE 0xFFFF




int currentscreen;
bool Camera = false;
bool tracker = false;
bool Homevar = false;
bool backbutton = true;




void Home() {
  tft.fillScreen(RED);
  tft.setCursor(60, 100);
  tft.setTextSize(2);
  tft.print("CHOOSE APPLICATION");
  tft.fillRect(10, 10, 75, 75, BLUE);
  tft.fillRect(125, 10, 75, 75, BLUE);
  tft.fillRect(240, 10, 75, 75, BLUE);
  // creating buttons for applications, first test out using LED's
}
void Welcome() {
  tft.fillScreen(WHITE);                //WHat to fill the screen colour with- colours stated above
  tft.drawRect(0, 0, 319, 240, WHITE);  //the rectangularshape that the screen fills
  tft.setCursor(5, 5);                  //set cursor is where text will begin on the screen, top right corner of the text
  tft.setTextColor(BLACK);
  tft.setTextSize(2);
  tft.print("WELCOME LKAT");
  //add parameters
  tft.fillRect(50, 180, 210, 40, BLACK);
  tft.drawRect(50, 180, 210, 40, GREEN);
  tft.setCursor(60, 190);
  tft.setTextColor(WHITE);
  tft.setTextSize(2);
}


void LED() {
  tft.fillScreen(BLUE);
  tft.print("HELLO");
}




void setup() {
  Serial.begin(115200);

  tft.reset();
  uint16_t identifier = tft.readID();
  tft.begin(identifier);
  tft.setRotation(1);  //changing this value between 1 and 0 will change the layout of the text on screen. landscape portarait
  currentscreen = 0;
  Welcome();
  Serial.println(currentscreen);
  Serial.println(Homevar);
}


void loop() {
  TSPoint p = ts.getPoint();
  if (p.z > ts.pressureThreshhold) {
    p.x = map(p.x, TS_MAXX, TS_MINX, 0, 320);
    p.y = map(p.y, TS_MAXY, TS_MINY, 0, 480);
    pinMode(XM, OUTPUT);
    pinMode(YP, OUTPUT);
    //set screen resolution
  }

  // tft.fillRect(p.y, p.x, 5, 5, BLACK);


  if (currentscreen == 0) {
    if ((p.x >= 50 && p.x <= 260) && (p.y >= 180 && p.y <= 250)) {

      tft.print("hi ");
      currentscreen = 1;
      Homevar = !Homevar;
      Home();
    }
  }


  //     }
  //     // else {
  //     //   tft.print();
  //     // }
  //   }


  if (currentscreen == 1 &&  Homevar == true) {
    if ( p.x > 10 && p.x < 85 && p.y > 10 && p.y < 85 &&  Homevar == true) {
      LED();
    }
  }
}

I can't see what you've changed but the code still doesn't work. Can you suggest anything i can change with the if statements. Because a variable is meant to change when the button is pressed, only when the button has been pressed can it check for the next button. I tried to do that in the if statements but its not working

What does work, let’s start there.

Does the screen itself work?
Do you see your UI on the screen?
Did you run the touch screen calibration sketch and update the X and Y min/max values? (If there is one for that screen and library)
Does the screen return anything when you touch it?
If you draw a pixel where you are supposedly touching the screen, do you see that pixel?

Your posted code does not compile. How did you run it?

Here is the part of your code that does not compile...

  if (currentscreen == 1 &&  Homevar == true){){
      if( p.x >10 && p.x < 85 && p.y > 10 && p.y < 85 &&  Homevar == true){
 LED();
     }
  
   }

   }

Here is the part of your code that is fixed and compiles...

  if (currentscreen == 1 &&  Homevar == true) {
    if ( p.x > 10 && p.x < 85 && p.y > 10 && p.y < 85 &&  Homevar == true) {
      LED();
    }
  }
}

Even though your code can now compile, you are using duplicate pins...

#define LCD_CS A3
#define LCD_CD A2

and

#define YP A3
#define XM A2

Your sketch is a copy/pasted TFT sketch and Touchscreen sketch. You must ensure definitions (as well as the editing errors) do not create problems.

Here is a touchscreen example. Use the code to connect your touchscreen, so you can use your touchscreen.

I fixed it. the screen itself works and I can see my UI and I receive values back
however after the first button has been pressed the coordinates have been mixed up. it now reads from the bottom right corner going up and left instead of the top left

i could just work out the coordinates with the switch but that would create problems

most of the tutorials i've followed have done this, also if i change that pin then nothing appears on the screen

You used the same pins for two different purposes. One set configures SPI, the other set configures the touch pressure x/y.

can you suggest which pins to change to, because if i change them it doesn't seem to work

Decide what needs to be done with these two sets of pins.

Your "combined" code has the pins doing different things.

Look through your code, locate where each defined pin is used. After finding all the places, look for two available pins to replace one "combined" set pins. You can use the "find" function, or you can make a text file to make a note of what you find. When you edit, be sure to "comment-out" lines rather than deleting them.

Its odd that both the LCD and the Touch screen work at the same time. Perhaps that is just how the display works.

I am assuming there is a jumper you can set on the display to change the pin configuration, but without knowing the exact model of display, I can't be certain.

Can you please tell us the model of the display and/or take a picture of the top and bottom of it, close up.

// You need to just currentscreen vairaible,
//if statements

// create numbers for the vari
#include <Adafruit_GFX.h>
#include <Adafruit_TFTLCD.h>
#include <TouchScreen.h>
#include <stdint.h>

#define LCD_CS A3
#define LCD_CD A2
#define LCD_WR A1
#define LCD_RD A0
#define LCD_RESET A4
// these pins define the pins being used by the Arduino^^
#define TS_MINX 118
#define TS_MINY 106
#define TS_MAXX 950
#define TS_MAXY 933
#define YP A3
#define XM A2
#define YM 9
#define XP 8
// this code calibrates the screen
Adafruit_TFTLCD tft(LCD_CS, LCD_CD, LCD_WR, LCD_RD, LCD_RESET);



TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);
// these define the pins used to activate the screen
#define BLACK 0x0000
#define BLUE 0x001F
#define RED 0xF800
#define GREEN 0x07E0
#define CYAN 0x07FF
#define MAGENTA 0xf81F
#define YELLOW 0xFFE0
#define WHITE 0xFFFF


int currentscreen = 0;
bool Camera = 0;
bool tracker = false;
bool Homevar = false;
bool backbutton = 1;
bool welcome = true;

void Home() {
  tft.fillScreen(RED);
  tft.setCursor(60, 100);
  tft.setTextSize(2);
  tft.print("CHOOSE APPLICATION");
  tft.fillRect(10, 10, 75, 75, BLUE);
  tft.fillRect(125, 10, 75, 75, BLUE);
  tft.fillRect(240, 10, 75, 75, BLUE);
  // creating buttons for applications, first test out using LED's
}
void Welcome() {
  tft.fillScreen(WHITE);                //WHat to fill the screen colour with- colours stated above
  tft.drawRect(0, 0, 319, 240, WHITE);  //the rectangularshape that the screen fills
  tft.setCursor(5, 5);                  //set cursor is where text will begin on the screen, top right corner of the text
  tft.setTextColor(BLACK);
  tft.setTextSize(2);
  tft.print("WELCOME LKAT");
  //add parameters
  tft.fillRect(50, 180, 210, 40, BLACK);
  tft.drawRect(50, 180, 210, 40, GREEN);
  tft.setCursor(60, 190);
  tft.setTextColor(WHITE);
  tft.setTextSize(2);
}

void LED() {
  tft.setCursor(5, 5);
  tft.fillScreen(BLUE);
  tft.print("HELLO");
}


void setup() {
  Serial.begin(9600);
  tft.reset();
  uint16_t identifier = tft.readID();
  tft.begin(identifier);
  tft.setRotation(1);  //changing this value between 1 and 0 will change the layout of the text on screen. landscape portarait
  currentscreen = 0;

  Welcome();
}

void loop() {

  TSPoint p = ts.getPoint();
  if (p.z > ts.pressureThreshhold) {
    p.y = map(p.y, TS_MINY, TS_MAXY, 0, 480);
    p.x = map(p.x, TS_MINX, TS_MAXX, 0, 320);
    pinMode(XM, OUTPUT);
    pinMode(YP, OUTPUT);

    Serial.println(p.x);
    Serial.println(p.y);
    //set screen resolution


    if (currentscreen == 0) {
      if (p.x > 50 && p.x < 260 && p.y > 180 && p.y < 260) {

        currentscreen = 1;

        Homevar = true;
        Home();
      }
    }



    if (currentscreen == 1 && Homevar == true) {

      //check coordinates, screen has rotated
      if (p.y > 10 && p.y < 85 && p.x > 10 && p.x < 85) {

        LED();
        currentscreen = 2;
      }
      //other if
    }
  }
}

Apparently this type of screen is known for its weird pin configuration.

The link below is a discussion from 2018 about this TP28017 display. One suggestion was to switch between reading the touch screen and switching back to updating the display.

do you now how'd id go about doing that

Its in the discussion. One guy gives an example.

I tried adding the suggested codes from what I could see and it didn't fix it. From what I've read the conversation ended with them not providing their new findings. Do you think it is a coding error or pin configuration like you said

all the analogue pins are being used, there are none available

Using one pin for two devices is not right.