TFT Touch Screen Display

I have a 3.4 inch TFT display connected to arduino uno.
I am using McufriendKbv library.
My primary aim is to detect the touch points.
But when I touch a particular position every time I touch I am getting different x y z values which are not at all correlated.
And when I power off and on and do experiment again the values are completely different. Because of that I am not able to put any condition for my touch buttons.

What could be the problem?

Thanks in advance

You need to post your code, I don't think anyone can help unless you do

/* Author : Prateek Hegde
Algo : TFT Buttons creation
Based on the inputs from the button data is sent over UART based on the selection
*/
#include <Adafruit_GFX.h>
#include <MCUFRIEND_kbv.h>
#include <SPI.h>
#include "TouchScreen_kbv.h"

MCUFRIEND_kbv tft;

// Assign human-readable names to some common 16-bit color values:
#define BLACK 0x0000
#define BLUE 0x001F
#define RED 0xF800
#define GREEN 0x07E0
#define CYAN 0x07FF
#define MAGENTA 0xF81F
#define YELLOW 0xFFE0
#define WHITE 0xFFFF

#ifndef min
#define min(a, b) (((a) < (b)) ? (a) : (b))
#endif

static int buttonCount = 1;
static bool pageFlag = true;

int XP = 6, YP = A1, XM = A2, YM = 7; //most common configuration

TouchScreen_kbv ts(XP, YP, XM, YM, 300); //re-initialised after diagnose
TSPoint_kbv tp;

void readResistiveTouch(void);
bool ISPRESSED(void);

static int y1,z1,y2,z2,y3,z3,y4,z4,y5,z5,y6,z6;

void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
uint16_t ID = tft.readID(); // Read the Device Id from the SPI Interface
Serial.print("ID = 0x");
Serial.println(ID, HEX);
if (ID == 0xD3D3) ID = 0x9481; // write-only shield
// ID = 0x9329; // force ID
tft.begin(ID);
tft.setRotation(135);
/**Caliberation part/
tft.fillScreen(0xFFFF);
tft.setCursor(0,0);
tft.setTextColor(0xF120);
tft.setTextSize(3);
/*******Process Automation *************/

tft.fillScreen(0xFFFF);
tft.setCursor(0,0);
tft.setTextColor(0xF120);
tft.setTextSize(3);

tft.print(" PROCESS AUTOMATION ");
tft.setCursor(0,50);
tft.setTextSize(2);
tft.print("Process 1");

tft.fillRect(0,30,200,75,0x07E0);
tft.fillRect(0,135,200,75,0x07E0);
tft.fillRect(210,30,200,75,0x07E0);
tft.fillRect(210,135,200,75,0x07E0);
tft.fillRect(0,230,200,75,0x07E0);
tft.fillRect(210,230,200,75,0x07E0);
tft.setCursor(0,50);
tft.setTextSize(2);
tft.print("Process 1");
tft.setCursor(0,155);
tft.print("Process 3");
tft.setCursor(220,50);
tft.print("Process 2");
tft.setCursor(220,155);
tft.print("Process 4");
tft.setCursor(0,250);
tft.print("Process 5");
tft.setCursor(220,250);
tft.print("Process 6");

}

void loop() {
// put your main code here, to run repeatedly:

if(pageFlag == true)
{

pageFlag = false;
tft.fillScreen(0xFFFF);
tft.setCursor(0,0);
tft.setTextColor(0xF120);
tft.setTextSize(3);

tft.print(" PROCESS AUTOMATION");
tft.fillRect(125,80,200,65,0x07E0);
Serial.println(buttonCount);
tft.setCursor(150,110);
tft.print("Process " + String(buttonCount));

// tft.fillRect(390,280,100,50,0x07E0);
tft.setCursor(400,250);
// tft.print("--->");

//tft.fillRect(0,280,100,50,0x07E0);
tft.setCursor(0,250);
tft.print("<---");

}

if(ISPRESSED() == true)
{
readResistiveTouch();
// Serial.println("tp.x=" + String(tp.x) + ", tp.y=" + String(tp.y) + ", tp.z =" + String(tp.z));
// Serial.println("y1=" + String(y1) + ",z1=" + String(z1)+ ", y2 =" + String(y2));
// Serial.println("y1=" + String(y1) + ",z1=" + String(z1) + ", y2 =" + String(y2) + ",z2 =" + String(z2));
// Serial.println("y3=" + String(y3) + ",z3=" + String(z3) + ", y4 =" + String(y4) + ",z4 =" + String(z4));
// Serial.println("y5=" + String(y5) + ",z5=" + String(z5) + ", y6 =" + String(y6) + ",z6 =" + String(z6));

// if(tp.z > (z1-10) && tp.z < (z1+10) && tp.y > (y1-15) && (tp.y < (y1+15)))
// if( tp.z >1055 && tp.z < 1082 && tp.y > (920) && (tp.y < (960)))
if( tp.z >1055 && tp.z < 1082 && tp.y > (920))
{
// Serial.println("Pressed Process " + String(buttonCount));
Serial.println(buttonCount);
buttonCount++;
pageFlag = true;
}
else if( tp.x < 66 && tp.z >1151 && tp.z < 1192 || tp.y < 850)
{
Serial.println("Go Back");
//buttonCount = buttonCount - 1;
buttonCount++;
pageFlag = true;
}
// else if(tp.z > 1031 && tp.z < 1053 && tp.y >970 && tp.y < 990 )
// else if(tp.z > 1031 && tp.z < 1062 && tp.y > 960 )
/* else if(tp.z > 1031 && tp.z < 1052 && tp.y > 980 )
{
Serial.println("Go Next");
buttonCount++;
pageFlag = true;
}*/

if(buttonCount > 6 || buttonCount < 1)
{
buttonCount = 1;
}

}

}

void readResistiveTouch(void)
{
tp = ts.getPoint();
pinMode(YP, OUTPUT); //restore shared pins
pinMode(XM, OUTPUT);
digitalWrite(YP, HIGH); //because TFT control pins
digitalWrite(XM, HIGH);
// Serial.println("tp.x=" + String(tp.x) + ", tp.y=" + String(tp.y) + ", tp.z =" + String(tp.z));
}
bool ISPRESSED(void)
{
// .kbv this was too sensitive !!
// now touch has to be stable for 50ms
int count = 0;
bool state, oldstate;
while (count < 10) {
readResistiveTouch();

if(tp.y == 1023)
{
return false;
}

state = tp.z > 200; //ADJUST THIS VALUE TO SUIT YOUR SCREEN e.g. 20 ... 250
if (state == oldstate) count++;
else count = 0;
oldstate = state;
delay(5);
}
return oldstate;
}

This is the code I am using

Since no one else has replied I've had a look at the code, got it to run on my system(sort of),you might find the touch screen coordinates will be more accurate by using the map function

//touch screen max and min coordinates
const int TS_MINX = 86;
const int TS_MINY =148;
const int TS_MAXX =962;
const int TS_MAXY= 862;


//use this to map the values, the 380 and 240 values are screen size in pixels

tp.x = map(tp.x, TS_MAXX, TS_MINX, 380, 0);tp.y = map(tp.y, TS_MAXY, TS_MINY, 240, 0);

it's also a lot easier to work out where your presses are because the touch panel and screen use the same coordinates

also what's with the tft.rotation(135)? I thought it was either 1 or 0?

I bypassed your readResistiveTouch() function and incorporated into the main loop, I got around the touch panel sensitivity in one of my projects by using the following code

int rt=0;

tp = ts.getPoint();
pinMode(A1, OUTPUT);
pinMode(A2, OUTPUT);

abs(tp.z);


if (tp.z>>0 && tp.z<=500){nv=150;}else{nv=0;}rt=rt+nv;
  
if (lc==3){if(rt>=150){state=HIGH;}else{state=LOW;}lc=0;rt=0;}lc++;

you can then use the value of state to detect whether the screen was touched. Hope this helps.

Edit: The more I look at this piece of code the less sense it seems to make., there seems to be a lot of redundant code and large amounts that have been commented out. The code that creates buttons in setup, that screen always gets drawn over because you've set pageflag to true? That's exactly what happened when I ran it.