TFT touchscreen edge detection not working as expected.

Hi,
I have a small 2.4" TFT touch display shield from china connected to my Arduino Uno. I can read the touch screen and output to the TFT fine but I'm having some issues doing state change detection in order to be able to create a button. I've used the same idea as in the state change example from the Arduino IDE.
Here is my code

int state = 0;
int laststate = 0;

void loop() {
  // put your main code here, to run repeatedly:
TSPoint p = ts.getPoint();
if(p.z>minPressure && p.z<maxPressure){
  state = 1;
}
else{
  state = 0;
}
if (laststate != state){
  if (state ==1){
    Serial.print("On");
  }
  else{
    Serial.print("Off");
  }
  
}
laststate = state;
}

I would expect it to print On in the serial monitor while I am touching the display and then off when I release it. Instead what is happening is it is printing on and then off in rapid succession to the serial monitor.

Display I am using

The library I am using for display

sketch_mar30a.ino (1.02 KB)

Welcome,

I don't have this display so I can't test that library, but it should work IF p.z is correct. Try print its value to see if it's within the expected range when you touch the display.

There is also a pressure() method which may give a different value than getPoint(), so maybe try that

uint16_t pressure = ts.pressure();
if(pressure>minPressure && pressure<maxPressure){

Also try to use getPoint()/pressure() less often, like once every 100ms, might help

Thank you so much.
It's working now using the pressure() method. I tried printing the values from p.z to the serial monitor and found it was occasionally registering a zero while I was pressing the screen- hence triggering the off behavior. Doesn't seem to be a problem with the pressure() method for some reason.
Either way, it's fine now thank you.

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