Supplying the right x and y coordinates for button using in 2.8" TFT module

#include <Adafruit_TFTLCD.h>
#include <TouchScreen.h>
#include <Adafruit_GFX.h>

// define the pins used to communicate with the display
#define LCD_CS   A3
#define LCD_CD   A2
#define LCD_WR   A1
#define LCD_RD   A0
#define LCD_RESET   A4

#define TS_MINX 122
#define TS_MINY 111
#define TS_MAXX 942
#define TS_MAXY 890

#define YP A3
#define XM A2
#define YM 9
#define XP 8

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

// create an instance of the TFT display object
Adafruit_TFTLCD tft(LCD_CS, LCD_CD, LCD_WR, LCD_RD, LCD_RESET);

TouchScreen ts = TouchScreen(XP, YP, XM, YM, 364);

boolean buttonEnabled = true;

void setup() {
  Serial.begin(9600);
  tft.reset();
  uint16_t identifier = tft.readID();
  tft.begin(identifier);
  tft.setRotation(1);
  tft.fillScreen(BLUE);

  tft.setCursor(205,30);
  tft.setTextColor(YELLOW);
  tft.setTextSize(3);
  tft.print("MENU");

  tft.setCursor(20,40);
  tft.setTextColor(WHITE);
  tft.setTextSize(1);
  tft.print("PHP 500.00");

  tft.setCursor(20,30);
  tft.setTextColor(WHITE);
  tft.setTextSize(1);
  tft.print("YOUR BALANCE: ");

  tft.fillRect(20,80, 80, 40, MAGENTA);
  tft.drawRect(20,80,80,40,BLACK);
  tft.setCursor(30,90);
  tft.setTextColor(WHITE);
  tft.setTextSize(1);
  tft.print("ADOBO");
  tft.setCursor(30,100);
  tft.setTextColor(WHITE);
  tft.setTextSize(1);
  tft.print("PHP 50.00");

  tft.fillRect(110,80, 80, 40, MAGENTA);
  tft.drawRect(110,80,80,40,BLACK);
  tft.setCursor(120,90);
  tft.setTextColor(WHITE);
  tft.setTextSize(1);
  tft.print("SINIGANG");
  tft.setCursor(120,100);
  tft.setTextColor(WHITE);
  tft.setTextSize(1);
  tft.print("PHP 45.00");

  tft.fillRect(200,80, 80, 40, MAGENTA);
  tft.drawRect(200,80,80,40,BLACK);
  tft.setCursor(210,90);
  tft.setTextColor(WHITE);
  tft.setTextSize(1);
  tft.print("TULINGAN");
  tft.setCursor(210,100);
  tft.setTextColor(WHITE);
  tft.setTextSize(1);
  tft.print("PHP 55.00");

  tft.fillRect(20,180, 80, 40, MAGENTA);
  tft.drawRect(20,180,80,40,BLACK);
  tft.setCursor(30,190);
  tft.setTextColor(WHITE);
  tft.setTextSize(1);
  tft.print("SIOMAI");
  tft.setCursor(30,200);
  tft.setTextColor(WHITE);
  tft.setTextSize(1);
  tft.print("PHP 20.00");

  tft.fillRect(110,180, 80, 40, MAGENTA);
  tft.drawRect(110,180,80,40,BLACK);
  tft.setCursor(120,190);
  tft.setTextColor(WHITE);
  tft.setTextSize(1);
  tft.print("MENUDO");
  tft.setCursor(120,200);
  tft.setTextColor(WHITE);
  tft.setTextSize(1);
  tft.print("PHP 50.00");

  tft.fillRect(200,180, 80, 40, MAGENTA);
  tft.drawRect(200,180,80,40,BLACK);
  tft.setCursor(210,190);
  tft.setTextColor(WHITE);
  tft.setTextSize(1);
  tft.print("TINOLA");
  tft.setCursor(210,200);
  tft.setTextColor(WHITE);
  tft.setTextSize(1);
  tft.print("PHP 50.00");

  tft.fillRect(20,130, 80, 40, MAGENTA);
  tft.drawRect(20,130,80,40,BLACK);
  tft.setCursor(30,140);
  tft.setTextColor(WHITE);
  tft.setTextSize(1);
  tft.print("CHICKEN");
  tft.setCursor(30,150);
  tft.setTextColor(WHITE);
  tft.setTextSize(1);
  tft.print("PHP 45.00");

  tft.fillRect(110,130, 80, 40, MAGENTA);
  tft.drawRect(110,130,80,40,BLACK);
  tft.setCursor(120,140);
  tft.setTextColor(WHITE);
  tft.setTextSize(1);
  tft.print("AFRITADA");
  tft.setCursor(120,150);
  tft.setTextColor(WHITE);
  tft.setTextSize(1);
  tft.print("PHP 55.00");

  tft.fillRect(200,130, 80, 40, MAGENTA);
  tft.drawRect(200,130,80,40,BLACK);
  tft.setCursor(210,140);
  tft.setTextColor(WHITE);
  tft.setTextSize(1);
  tft.print("KARE-KARE");
  tft.setCursor(210,150);
  tft.setTextColor(WHITE);
  tft.setTextSize(1);
  tft.print("PHP 55.00");
}

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);
       
   if(p.x > 20 && p.x < 60 && p.y > 80 && p.y < 160 && buttonEnabled)
   {    
    buttonEnabled = false;
    
    pinMode(XM, OUTPUT);
    pinMode(YP, OUTPUT);

    dishNine(); // draw the ninth dish 
   }

   if(p.x > 110 && p.x < 150 && p.y > 80 && p.y < 160 && buttonEnabled)
   {    
    buttonEnabled = false;
    
    pinMode(XM, OUTPUT);
    pinMode(YP, OUTPUT);

    dishSix(); // draw the sixth dish 
   }

    if(p.x > 200 && p.x < 240 && p.y > 80 && p.y < 160 && buttonEnabled)
   {    
    buttonEnabled = false;
    
    pinMode(XM, OUTPUT);
    pinMode(YP, OUTPUT);

    dishThree(); // draw the third dish 
   }

   if(p.x > 200 && p.x < 240 && p.y > 180 && p.y < 260 && buttonEnabled)
   {    
    buttonEnabled = false;
    
    pinMode(XM, OUTPUT);
    pinMode(YP, OUTPUT);

    dishOne(); // draw the third dish 
   }
   
  }
}

void dishNine() {
  Serial.print("TINOLA");
  Serial.print("\t");
  delay(1000);
  Serial.println("Php 50.00");
  
  tft.fillScreen(BLUE);
    
  tft.setCursor(40,30);
  tft.setTextColor(YELLOW);
  tft.setTextSize(2);
  tft.print("Your order has been");
    
  tft.setCursor(100,50);
  tft.setTextColor(YELLOW);
  tft.setTextSize(2);
  tft.print("recieved.");    

  tft.setCursor(20,100);
  tft.setTextColor(WHITE);
  tft.setTextSize(2);
  tft.print("If you made a mistake,");
    
  tft.setCursor(50,120);
  tft.setTextColor(WHITE);
  tft.setTextSize(2);
  tft.print("please inform us");    
    
  tft.setCursor(80,140);
  tft.setTextColor(WHITE);
  tft.setTextSize(2);
  tft.print("immediately.");

  tft.setCursor(60,200);
  tft.setTextColor(YELLOW);
  tft.setTextSize(1);
  tft.print("(C) 2023 CALAMBA CITY SCIENCE");  

  tft.setCursor(90,210);
  tft.setTextColor(YELLOW);
  tft.setTextSize(1);
  tft.print("INTEGRATED SCHOOL"); 
}

void dishSix() {
  Serial.print("KARE-KARE");
  Serial.print("\t");
  delay(1000);
  Serial.println("Php 55.00");
  
  tft.fillScreen(BLUE);
    
  tft.setCursor(40,30);
  tft.setTextColor(YELLOW);
  tft.setTextSize(2);
  tft.print("Your order has been");
    
  tft.setCursor(100,50);
  tft.setTextColor(YELLOW);
  tft.setTextSize(2);
  tft.print("recieved.");    

  tft.setCursor(20,100);
  tft.setTextColor(WHITE);
  tft.setTextSize(2);
  tft.print("If you made a mistake,");
    
  tft.setCursor(50,120);
  tft.setTextColor(WHITE);
  tft.setTextSize(2);
  tft.print("please inform us");    
    
  tft.setCursor(80,140);
  tft.setTextColor(WHITE);
  tft.setTextSize(2);
  tft.print("immediately.");

  tft.setCursor(60,200);
  tft.setTextColor(YELLOW);
  tft.setTextSize(1);
  tft.print("(C) 2023 CALAMBA CITY SCIENCE");  

  tft.setCursor(90,210);
  tft.setTextColor(YELLOW);
  tft.setTextSize(1);
  tft.print("INTEGRATED SCHOOL"); 
}

void dishThree() {
  Serial.print("TULINGAN");
  Serial.print("\t");
  delay(1000);
  Serial.println("Php 55.00");
  
  tft.fillScreen(BLUE);
    
  tft.setCursor(40,30);
  tft.setTextColor(YELLOW);
  tft.setTextSize(2);
  tft.print("Your order has been");
    
  tft.setCursor(100,50);
  tft.setTextColor(YELLOW);
  tft.setTextSize(2);
  tft.print("recieved.");    

  tft.setCursor(20,100);
  tft.setTextColor(WHITE);
  tft.setTextSize(2);
  tft.print("If you made a mistake,");
    
  tft.setCursor(50,120);
  tft.setTextColor(WHITE);
  tft.setTextSize(2);
  tft.print("please inform us");    
    
  tft.setCursor(80,140);
  tft.setTextColor(WHITE);
  tft.setTextSize(2);
  tft.print("immediately.");

  tft.setCursor(60,200);
  tft.setTextColor(YELLOW);
  tft.setTextSize(1);
  tft.print("(C) 2023 CALAMBA CITY SCIENCE");  

  tft.setCursor(90,210);
  tft.setTextColor(YELLOW);
  tft.setTextSize(1);
  tft.print("INTEGRATED SCHOOL"); 
}

void dishOne() {
  Serial.print("ADOBO");
  Serial.print("\t");
  delay(1000);
  Serial.println("Php 55.00");
  
  tft.fillScreen(BLUE);
    
  tft.setCursor(40,30);
  tft.setTextColor(YELLOW);
  tft.setTextSize(2);
  tft.print("Your order has been");
    
  tft.setCursor(100,50);
  tft.setTextColor(YELLOW);
  tft.setTextSize(2);
  tft.print("recieved.");    

  tft.setCursor(20,100);
  tft.setTextColor(WHITE);
  tft.setTextSize(2);
  tft.print("If you made a mistake,");
    
  tft.setCursor(50,120);
  tft.setTextColor(WHITE);
  tft.setTextSize(2);
  tft.print("please inform us");    
    
  tft.setCursor(80,140);
  tft.setTextColor(WHITE);
  tft.setTextSize(2);
  tft.print("immediately.");

  tft.setCursor(60,200);
  tft.setTextColor(YELLOW);
  tft.setTextSize(1);
  tft.print("(C) 2023 CALAMBA CITY SCIENCE");  

  tft.setCursor(90,210);
  tft.setTextColor(YELLOW);
  tft.setTextSize(1);
  tft.print("INTEGRATED SCHOOL"); 
}

How can I supply the right x and y coordinates for the buttons? So far 3 out of my 9 buttons are working. The coordinates given to those are only random numbers that seemed to work. I did it again for other buttons, but it didn't work. Is there a working way to find the x and y coordinates for the said buttons?

Can you make a picture of your screen with the buttons?
Can you make a small program to print x and y when you press a button?
Do you know xmax xmin ymax and ymin?
Did your screen library come with a 'make a drawing' example?
You could make one generic function to make a button at a certain place with a certain text...

make_button(int x, int y, char* text) {
   Code here
}

And then call in a nested for loop.
That would save tons of code.

Can you apply the code with a example?

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