I need a bit of help with touchscreen coordinates on mcufriend display

Hi everyone

So I have a mcufriend display that has a 4-wire resistive touchscreen attached to the panel. It's a 3.5" display with a resolution of 480x320. The pcb behind it is red and it says "3.5" TFT LCD for arduino uno".

I am using it with a mega and works fine.

Everything is ok except I cannot for the life of me get the touchscreen coordinates to match the coordinates of the actual icons.

Let's say I have this code:

tft.drawRect(16, 55, 322, 220, RED);

The X and the Y are 16 and 55.

So in the touchscreen logic I programmed it to look for:

p.x = map(p.x, TS_MAXX, TS_MINX, 0, tft.width());
p.y = map(p.y, TS_MINY, TS_MAXY, 0, tft.height());

if (p.x>16 && p.x<338 && p.y>55 && p.y<275){
//do stuff
}

I obtained the x and y bigger coordinates by making x of the drawRect + width and y of the drawrect + height.

The problem is that when I use serial to register the touches it clearly shows that if for example I touch where I believe it should be X 2 and Y 2, it's actually not. The drawRect would draw there with no problem but the touchscreen would think it is 400 or 900 or -2 or whatever, just not the actual X and y.

I have tried to copy the calibration data given by mcufriend_kbv calibration sketch and I swapped these values around a lot but I still cannot get it to match the actual positions on the screen.

Here are my touch-related variables:

#define YP A2
#define XM A1
#define YM 6
#define XP 7

#define MINPRESSURE 6
#define MAXPRESSURE 1000

#define TS_MINX 147
#define TS_MINY 154
#define TS_MAXX 942
#define TS_MAXY 911

I use it in landscape (1)

Any help would be greatly appreciated

Sometimes X & Y are mirrored, depends on "orientation" - display could manipulate an image, but touch sensor is not, AFAIK.
To troubleshoot an issue just print x & y coordinate registered by arduino over serial, than see if it's reversed or offset touching in different location.