MCUFriend tft touch location

im trying to get the buttons touch location right so far 1-6 sort of respond properly. please forgive me for my limited knowledge in these lcd screens. i simplified some code that runs well and show the issue im having. so basically im using the button test example from the utouch library and trying to make it work with the MCUFRIEND_kbv and TouchSCREEN libraries. so far i have drawn the button the the correct position but defining exactly where the button is im having a hard time. right now i have button 2 draw a rectangle and gave it the coordinates for the touch location but it draws the rectangle in the wrong place. does it appear i have the touch location ni the wrong place? i calibrated the screen and map the touch values and it seems to be close. but i would really be thankful for some help right about now.

#include <Adafruit_GFX.h>
#include <MCUFRIEND_kbv.h>
MCUFRIEND_kbv tft;
#include <TouchScreen.h>
#define MINPRESSURE 200
#define MAXPRESSURE 1000
#include <UTFTGLUE.h>

const int XP = 7, XM = A1, YP = A2, YM = 6; //320x480 ID=0x6814
const int TS_LEFT=119,TS_RT=919,TS_TOP=72,TS_BOT=926;
int x, y;

bool readTouch = true;
TSPoint tp;
UTFTGLUE myGLCD(0, A2, A1, A3, A4, A0); //all dummy args
TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);

char stCurrent[20]="";
int stCurrentLen=0;
char stLast[20]="";



void setup() {
  // put your setup code here, to run once:
 myGLCD.clrScr();
   uint16_t ID = tft.readID();
  myGLCD.InitLCD(PORTRAIT);
  myGLCD.clrScr();
  drawButtons();
}



void updateStr(int val)
{
  if (stCurrentLen<20)
  {
    stCurrent[stCurrentLen]=val;
    stCurrent[stCurrentLen+1]='\0';
    stCurrentLen++;
    myGLCD.setColor(0, 255, 0);
    myGLCD.print(stCurrent, LEFT, 224);
  }
  else
  {
    myGLCD.setColor(255, 0, 0);
    myGLCD.print("BUFFER FULL!", CENTER, 192);
    delay(500);
    myGLCD.print("            ", CENTER, 192);
    delay(500);
    myGLCD.print("BUFFER FULL!", CENTER, 192);
    delay(500);
    myGLCD.print("            ", CENTER, 192);
    myGLCD.setColor(0, 255, 0);
  }
}
void waitForIt(int x1, int y1, int x2, int y2)
{
  myGLCD.setColor(255, 0, 0);
  myGLCD.drawRoundRect (x1, y1, x2, y2);
//  while (TouchScreen.dataAvailable())
//    myTouch.read();
  myGLCD.setColor(255, 255, 255);
  myGLCD.drawRoundRect (x1, y1, x2, y2);
}
void drawButtons()
{
  // Draw the upper row of buttons
  for (x = 0; x < 5; x++)
  {
    myGLCD.setColor(0, 0, 255);
    myGLCD.fillRoundRect (15 + (x * 61), 310, 60 + (x * 61), 360);
    myGLCD.setColor(255, 255, 255);
    myGLCD.drawRoundRect (15 + (x * 61), 310, 60 + (x * 61), 360);
    myGLCD.printNumI(x + 1, 31 + (x * 61), 330);
  }
  // Draw the center row of buttons
  for (x = 0; x < 5; x++)
  {
    myGLCD.setColor(0, 0, 255);
    myGLCD.fillRoundRect (15 + (x * 61), 370, 60 + (x * 61), 420);
    myGLCD.setColor(255, 255, 255);
    myGLCD.drawRoundRect (15 + (x * 61), 370, 60 + (x * 61), 420);
    if (x < 4)
      myGLCD.printNumI(x + 6, 35 + (x * 60), 390);
  }
  myGLCD.print("0", 275, 390);
  // Draw the lower row of buttons
  myGLCD.setColor(0, 0, 255);
  myGLCD.fillRoundRect (15, 430, 150, 470); //color
  myGLCD.setColor(255, 255, 255);
  myGLCD.drawRoundRect (15, 430, 150, 470);
  myGLCD.print("Clear", 48, 440);
  myGLCD.setColor(0, 0, 255);
  myGLCD.fillRoundRect (170, 430, 305, 470);
  myGLCD.setColor(255, 255, 255);
  myGLCD.drawRoundRect (170, 430, 305, 470);
  myGLCD.print("Enter", 208, 440);
  myGLCD.setBackColor (0, 0, 0);
}
void loop() {
  //       bool down = Touch_getXY();

  x = map(tp.x, 119, 919, 0, 320);
y = map(tp.y, 72, 926, 0, 480);

  tp = ts.getPoint();

  if (readTouch) {
    pinMode(XM, OUTPUT);
    pinMode(YP, OUTPUT);
    if (tp.z < MINPRESSURE || tp.z > MAXPRESSURE) {
      if (x > 10 && x < 50  && y > 340 && y < 360) {
        Serial.println("number 1");
        updateStr('1');
        drawButtons();

      }
      if (x > 70 && x < 120  && y > 320 && y < 360) {
        Serial.println("number 2");
         waitForIt(70, 120, 320, 360 );
        updateStr('2');
        //    drawButtons();

      }
      if (x > 140 && x < 190  && y > 320 && y < 360) {
        Serial.println("number 3");
        updateStr('3');
        //  drawButtons();

      }
      if (x > 195 && x < 250  && y > 320 && y < 360) {
        Serial.println("number 4");
        updateStr('4');
        //    drawButtons();

      }
      if (x > 265 && x < 310  && y > 320 && y < 360) {
        Serial.println("number 5");
        updateStr('5');
        //     drawButtons();

      }
      if (x > 5 && x < 55  && y > 375 && y < 415) {
        Serial.println("number 6");
        updateStr('6');
        //    drawButtons();

      }
    }
  }

}

EDIT: I had the coordinates in the wrong place when drawing the rectangle.

Install MCUFRIEND_kbv, Adafruit_GFX, Adafruit_Touchscreen libraries via the IDE Library Manager.

I provide library examples for you.

Please adapt a library example. What size screen do you have? What is the controller ID?

Do NOT attempt to use UTFTGLUE.

If you make a pencil drawing of what you want the screen to look like, I will help you.
I might even write an example program for you.

David.

david_prentice:
Install MCUFRIEND_kbv, Adafruit_GFX, Adafruit_Touchscreen libraries via the IDE Library Manager.

I provide library examples for you.

Please adapt a library example. What size screen do you have? What is the controller ID?

Do NOT attempt to use UTFTGLUE.

If you make a pencil drawing of what you want the screen to look like, I will help you.
I might even write an example program for you.

David.

Hi thank you for the reply. may i ask why not use UTFTGLUE? i see in the setup they share some of the same pins. which one of the three libraries you mention should i use instead of UTFTGLUE?

EDIT: according to touch calibration wizard the pecs of the controller are //320x480 ID=0x6814

also i modified my loop so the buttons and lines are in the correct location excluding enter and clear

void loop() {
  //       bool down = Touch_getXY();

  x = map(tp.x, 119, 919, 0, 320);
y = map(tp.y, 72, 926, 0, 480);

  tp = ts.getPoint();

   if (readTouch) {
    pinMode(XM, OUTPUT);
    pinMode(YP, OUTPUT);
    if (tp.z < MINPRESSURE || tp.z > MAXPRESSURE) {

      if (x > 15 && x < 60  && y > 310 && y < 360) {
        Serial.println("number 1");
        waitForIt(15, 310, 60, 360 );
        updateStr('1');
      }
      if (x > 76 && x < 121  && y > 310 && y < 360) {
        Serial.println("number 2");
        waitForIt(76, 310, 121, 360 );
        updateStr('2');
      }
      if (x > 137 && x < 182  && y > 310 && y < 360) {
        Serial.println("number 3");
        waitForIt(137, 310, 182, 360 );
        updateStr('3');
      }
      if (x > 198 && x < 243  && y > 310 && y < 360) {
        Serial.println("number 4");
        waitForIt(198, 310, 243, 360 );
        updateStr('4');
      }
      if (x > 259 && x < 304  && y > 320 && y < 360) {
        Serial.println("number 5");
        waitForIt(259, 310, 304, 360 );
        updateStr('5');
      }
      if (x > 15 && x < 60  && y > 370 && y < 420) {
        Serial.println("number 6");
        waitForIt(15, 370, 60, 420 );
        updateStr('6');
      }
      if (x > 76 && x < 121  && y > 370 && y < 420) {
        Serial.println("number 7");
       waitForIt(76, 370, 121, 420 );
        updateStr('7');

      }
            if (x > 137 && x < 182  && y > 370 && y < 420) {
        Serial.println("number 8");
        waitForIt(137, 370, 182, 420 );
        updateStr('8');
      }
      if (x > 198 && x < 243  && y > 370 && y < 420) {
        Serial.println("number 9");
       waitForIt(198, 370, 243, 420 );
        updateStr('9');
      }
            if (x > 259 && x < 304  && y > 370 && y < 420) {
        Serial.println("number 0");
       waitForIt(259, 370, 304, 420 );
        updateStr('0');
      }
    }
  }
}

right now using UTFTGLUE i draw my lines like this

myGLCD.drawLine(6, 40, 315, 40);
myGLCD.drawLine(160, 160, 160, 200);
myGLCD.drawLine(6, 200, 315, 200);
myGLCD.drawLine(6, 465, 315, 465);
myGLCD.drawRect(0, 0, 319, 479);

what would be the equivalent for MCUFRIEND_kbv?

You can either post a link to your sketch i.e. https://www.mediafire.com/file/n2pdq1o6b9xutba/tx_simplified2.ino/file

Or you can make it a clickable link e.g. sketch code

Or you could add the file as an Attachment. Click the [Preview] button and it lets you attach files.

Why have you chosen to use UTFT-style methods?
This is a Adafruit_GFX style library.

If you are writing a new program, you should always use GFX methods.
If you are trying to port a working UTFT program, say so. Post a link to the original working UTFT code.

I can show you how to write a new program.
Or how to port UTFT.

David.

what is equivalent to myGLCD.drawLine with Adafruit_GFX ?

From Adafruit_GFX.h

void     drawLine(int16_t x0, int16_t y0, int16_t x1, int16_t y1, uint16_t color);

So it is similar. With an extra argument for color.

Seriously. You can just Google "Adafruit_GFX tutorial" link

All the methods are pretty intuitive. You just look up the signatures.

If you are writing a new program, you should always use GFX methods.
If you are trying to port a working UTFT program, say so. Post a link to the original working UTFT code.

David.