Mcufriend touch screen output

Thought I'd share this someone might find it useful. I don't know whether it's just my touch screen or they are all like this, but there seems to be a problem with getting a consistent output from this device. Under normal usage it may not be a problem but if I wanted to say measure the time that the screen is pressed I got some very odd readings. If the screen was pressed the output frequently dropped to 0 which gave a premature "finger off" (0) reading, also I was getting a very high reading every couple of hundred cycles. I tried adjusting the last parameter in the TouchScreen ( 6, A1, A2, 7, 300) function but met with only limited success. So with my limited abilities I wrote the following code which converts the output value to either a LOW or HIGH state then takes three readings and if there is less than three lows it still returns a HIGH state ( I noticed it was rare for the sensor to return more than 2 consecutive 0's whilst the screen was touched, by increasing this value this would decrease its sensitivity, however this will increase it's response and release times but probably not that I think that it would be a problem

#include <MCUFRIEND_kbv.h>
MCUFRIEND_kbv tft;       // hard-wired for UNO shields anyway.
#include <TouchScreen.h>

TouchScreen ts = TouchScreen  ( 6, A1, A2, 7, 300);
TSPoint tp;

int state=LOW;
int nv;
int lc;

void setup() {

 uint16_t id;
 tft.reset();
 id = tft.readID();
 tft.begin(id);
 tft.setRotation(1);
 
}

void loop() {
 
 int rt=0;
  
  tp = ts.getPoint();
  pinMode(A1, OUTPUT);
  pinMode(A2, OUTPUT);

  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;}rt=0;nv=0;lc=0;}
  lc++;

  

}

all that's left is to attach some code to whether state is LOW or HIGH, I bet there's a much better way of doing this...