TFT-LCD Display cannot draw while using touch function [solved]

Hi all,

I have a TFT-LCD Display with driver chip HX8347D. I am making a touch interface with four buttons (A), (B), (C) & (D) as shown below. My intention was to print a string of text on the LCD screen when a button is pressed.

The problem is that my TFT-LCD display cannot seem to draw anything while the touch function is being used. By comparing the code below with the output image above, it is clear that nothing prints after getting the point coordinates.

#include <Adafruit_GFX.h> // Hardware-specific library
#include <MCUFRIEND_kbv.h>
#include "TouchScreen.h"

#define MINPRESSURE 0
#define MAXPRESSURE 1023
#define YP A3  // must be an analog pin, use "An" notation!
#define XM A2  // must be an analog pin, use "An" notation!
#define YM 9   // can be a digital pin
#define XP 8   // can be a digital pin

MCUFRIEND_kbv tft;
TouchScreen ts = TouchScreen(XP, YP, XM, YM, MINPRESSURE);
void setup() {
  Serial.begin(115200);
  int id = tft.readID();
  tft.begin(id);
  tft.setRotation(1);
  tft.fillScreen(0);
  tft.setTextSize(3);
  tft.fillRoundRect(5,125,150,50,10,0x1F);
  tft.fillRoundRect(5,185,150,50,10,0x3720);
  tft.fillRoundRect(165,124,150,50,10,0xE800);
  tft.fillRoundRect(165,185,150,50,10,0xF7C0);
  tft.setTextColor(0xFFFF);
  tft.setCursor(55,138);
  tft.print("(A)");
  tft.setCursor(215,138);
  tft.print("(B)");
  tft.setCursor(55,198);
  tft.print("(C)");  
  tft.setCursor(215,198);
  tft.print("(D)");
  tft.setCursor(0,120);
}
int x_point = 0;
int y_point = 0;
void print_location() {
  if (x_point > 169 && x_point < 220 && y_point > 25 && y_point < 115) {
    Serial.println("You pressed (A)");
  } else if (x_point > 169 && x_point < 220 && y_point > 120 && y_point < 215) {
    Serial.println("You pressed (B)");
  } else if (x_point > 240 && x_point < 285 && y_point > 25 && y_point < 115) {
    Serial.println("You pressed (C)");
  } else if (x_point > 240 && x_point < 285 && y_point > 120 && y_point < 215) {
    Serial.println("You pressed (D)");
  }
}

void loop() {
  tft.setCursor(0,0);
  tft.print("String 1");    // For testing only
  TSPoint p = ts.getPoint();
  tft.fillScreen(0x1F);     // For testing only
  if (p.z > MINPRESSURE) {
    x_point = map(p.x, MINPRESSURE, MAXPRESSURE,0,320);
    y_point = map(p.y, MINPRESSURE, MAXPRESSURE,0,240);
    Serial.print(x_point);
    Serial.print('\t');
    Serial.println(y_point);
  }
  tft.setCursor(0,120);
  tft.print("String 2");  // For testing only
}

If I change the minimum pressure and maximum pressure, the result stays the same. The touch function seems to work as expected since I was able to read the coordinates of the point touched from the serial monitor.

For additional info, my TFT-LCD display only works with the MCUFRIEND_kbv library. I have seen a similar example of a button interface working successfully from YouTube. I will appreciate any help provided. Thanks in advance.

If the touch co-ordinates are shown in the serial monitor , then it would seem to be working .
The sketch, as written ,would give the screen you have. You fail to call print_location() which would allow you see you "messages" in the serial monitor.

:thinking:

I have tested the function print_location(). It works as expected when using the serial monitor. The problem is that I cannot print or draw anything on the LCD screen after calling the function ts.getPoint(). If you check the image attached, you will notice that 'String 2' does not appear on the screen.

Not sure if XM and YP will be the correct pins for your display, but the examples for the MCUFRIEND_kbv library that use the touchscreen require that your code restore the pinMode settings for any pins that are shared by the display and the touchscreen, immediately after any call to getPoint().

    tp = ts.getPoint();   //tp.x, tp.y are ADC values

    // if sharing pins, you'll need to fix the directions of the touchscreen pins
    pinMode(XM, OUTPUT);
    pinMode(YP, OUTPUT);

Thanks for the reply. According to the example 'diagnose_touch_pins.ino', the pins specified are correct. My program is now working as desired.