Resistive touchscreen display not working with arduino

I recently bought a tft display with a touch panel built in but I cant seem to get the touchscreen part of it working. This is the link to the display www.amazon.co.uk/dp/B07PNQFSKN?ref=ppx_pop_mob_ap_share and this is my code (it does include code from other people)

#include <MCUFRIEND_kbv.h> 
#include <Adafruit_GFX.h>
MCUFRIEND_kbv tft;
#include <TouchScreen.h>
#include <FreeDefaultFonts.h>
const int XP=9,XM=A3,YP=A3,YM=8;
const int TS_LEFT=907,TS_RT=136,TS_TOP=942,TS_BOT=139;

TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);
TSPoint tp;
#define BLACK 0x0000
#define NAVY 0x000F
#define DARKGREEN 0x03E0
#define DARKCYAN 0x03EF
#define MAROON 0x7800
#define PURPLE 0x780F
#define OLIVE 0x7BE0
#define LIGHTGREY 0xFFFF
#define DARKGREY 0x7BEF
#define BLUE 0x001F
#define GREEN 0x07E0
#define CYAN 0x07FF
#define RED 0xF800
#define MAGENTA 0xF81F
#define YELLOW 0xFFE0
#define WHITE 0xFFFF
#define ORANGE 0xFD20
#define GREENYELLOW 0xAFE5

void setup(void)
{
  Serial.begin(9600);
  uint16_t ID = tft.readID();
  Serial.println(ID);
  Serial.println(tft.width());
  Serial.println(tft.height());
  if (ID == 0xD3) ID = 0x9481;
  tft.begin(ID);
  tft.setRotation(0);
  int h = 0;
  int m = 0;
  int s = 0;
  tft.fillScreen(BLACK);
  tft.setFont(&FreeSevenSegNumFont);
  tft.setTextSize(1.5);
  tft.setCursor(-15, 55);
  tft.print("1234");
  int r = 101;
  int ws = 10;
  while (ws != 0){
    tft.drawFastHLine(0 , r , 320 , WHITE);
    ws = ws - 1;
    r = r + 1;
  }
  tft.drawRect(0, 112, 320, 380, BLUE);
  tft.fillRect(0, 112, 320, 380, BLUE);

  tft.drawRoundRect(22, 137, 50, 50, 10, LIGHTGREY);
  tft.fillRoundRect(22, 137, 50, 50, 10, LIGHTGREY);

  tft.drawRoundRect(97, 137, 50, 50, 10, LIGHTGREY);
  tft.fillRoundRect(97, 137, 50, 50, 10, LIGHTGREY); 

  tft.drawRoundRect(172, 137, 50, 50, 10, LIGHTGREY);
  tft.fillRoundRect(172, 137, 50, 50, 10, LIGHTGREY);

  tft.drawRoundRect(247, 137, 50, 50, 10, LIGHTGREY);
  tft.fillRoundRect(247, 137, 50, 50, 10, LIGHTGREY);   


  tft.drawRoundRect(22, 212, 50, 50, 10, LIGHTGREY);
  tft.fillRoundRect(22, 212, 50, 50, 10, LIGHTGREY);

  tft.drawRoundRect(97, 212, 50, 50, 10, LIGHTGREY);
  tft.fillRoundRect(97, 212, 50, 50, 10, LIGHTGREY);

  tft.drawRoundRect(172, 212, 50, 50, 10, LIGHTGREY);
  tft.fillRoundRect(172, 212, 50, 50, 10, LIGHTGREY);

  tft.drawRoundRect(247, 212, 50, 50, 10, LIGHTGREY);
  tft.fillRoundRect(247, 212, 50, 50, 10, LIGHTGREY);


  tft.drawRoundRect(22, 287, 50, 50, 10, LIGHTGREY);
  tft.fillRoundRect(22, 287, 50, 50, 10, LIGHTGREY);

  tft.drawRoundRect(97, 287, 50, 50, 10, LIGHTGREY);
  tft.fillRoundRect(97, 287, 50, 50, 10, LIGHTGREY); 

  tft.drawRoundRect(172, 287, 50, 50, 10, LIGHTGREY);
  tft.fillRoundRect(172, 287, 50, 50, 10, LIGHTGREY);

  tft.drawRoundRect(247, 287, 50, 50, 10, LIGHTGREY);
  tft.fillRoundRect(247, 287, 50, 50, 10, LIGHTGREY); 

  tft.drawRoundRect(97, 362, 50, 50, 10, LIGHTGREY);
  tft.fillRoundRect(97, 362, 50, 50, 10, LIGHTGREY); 

  tft.drawRoundRect(247, 362, 50, 50, 10, LIGHTGREY);
  tft.fillRoundRect(247, 362, 50, 50, 10, LIGHTGREY);


  tft.drawRoundRect(25, 427, 272, 30, 10, LIGHTGREY);
  tft.fillRoundRect(25, 427, 272, 30, 10, LIGHTGREY);

  tft.setTextColor(0x2945);

  tft.setFont(&FreeSevenSegNumFont);
  tft.setTextSize(1.5);
  tft.setCursor(20, 187);
  tft.print("1");
  tft.setCursor(105, 187);
  tft.print("2");
  tft.setCursor(180, 187);
  tft.print("3");
  tft.setFont(NULL);
  tft.setTextSize(5);
  tft.setCursor(257, 144);
  tft.print("+");

  tft.setFont(&FreeSevenSegNumFont);
  tft.setTextSize(1.5);
  tft.setCursor(28, 263);
  tft.print("4");
  tft.setCursor(105, 263);
  tft.print("5");
  tft.setCursor(180, 263);
  tft.print("6");
  tft.setFont(NULL);
  tft.setTextSize(5);
  tft.setCursor(259, 220);
  tft.print("-");

  tft.setFont(&FreeSevenSegNumFont);
  tft.setTextSize(1.5);
  tft.setCursor(28, 338);
  tft.print("7");
  tft.setCursor(105, 338);
  tft.print("8");
  tft.setCursor(180, 338);
  tft.print("9");
  tft.setFont(NULL);
  tft.setTextSize(5);
  tft.setCursor(259, 295);
  tft.print("*");

  tft.setFont(&FreeSevenSegNumFont);
  tft.setTextSize(1.5);
  tft.setCursor(106, 412);
  tft.print("0");
  tft.setFont(NULL);
  tft.setTextSize(5);
  tft.setCursor(258, 370);
  tft.print("/");

  tft.setCursor(80, 430);
  tft.setFont(NULL);
  tft.setTextSize(3.5);
  tft.print("Calculate");
}

void loop(void)
{
  tp = ts.getPoint();
  int x = tp.x;
  int y = tp.y;
  if (tp.z > 24400){
    Serial.println("x: " + String(x) + " y: " + String(y));
    delay(10);
  }
  if (tp.z > 24400 and ((x < 72 and x > 22) and (y < 187 and y > 137))){
    tft.fillScreen(RED);
    delay(10);
  }
}

I'm trying to make a simple calculator

Have you tried running the display library example sketches? When you say it's not working, it's vague. Please describe the non-working exactly.

Yes I have and what I mean by not working is that in this section of the code

tp = ts.getPoint();
  int x = tp.x;
  int y = tp.y;
  if (tp.z > 24400){
    Serial.println("x: " + String(x) + " y: " + String(y));
    delay(10);
  }

it is supposed to print out the x and y values to the serial port however it doesn't

...and what happens instead?

it doesn't print out anything to the serial port but in the start of the code it does print out the width and height so I suspect it is a problem with the code

Did you try a serial debug print of 'tp,z'? If the value is <= 24400, nothing will print.

I will try that now

My code is not very efficient but here it is

{
  void loop(void){
  tp = ts.getPoint();
  int x = tp.x;
  int y = tp.y;
  Serial.println("z: " + tp.z);
  delay(10);
}

It just prints out z: constantly to the serial port with no value
it does have the whole entire other segment of the code

You screwed it up. println() can only print one value. You forgot the abominable String conversion.

oh okay i will change it

Do you understand this code at all? What is the significance of the magic number '24400'?

It used to work before but it gave incorrect x and y values and I noticed that if the number was below 24400 it kept printing out the x and y values of where the most pressure is applied

I'm sorry, you have not answered several of my questions directly. Someone else will have to help you. Good luck.

Yes, these Blue 3.5 inch shields often have Touch pins on 9, A3, A2, 8.
No, you can't have A3 , A3. i.e. two ADC with the same pin.

From examples.

#define MINPRESSURE 200
#define MAXPRESSURE 1000
...
    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);
    // we have some minimum pressure we consider 'valid'
    // pressure of 0 means no pressing!

    if (tp.z > MINPRESSURE && tp.z < MAXPRESSURE) {

You will never get 24400 from a valid touch. You will never get x < 72 when TS_RT=136. It is possible to get y < 187 when TS_BOT=139.

The Library comes with a whole set of examples. Run all of the examples before you ever think of writing your own sketches.

David.

Thank you for replying with this information I will try it now

I ran one of the examples labelled "diagnose_Touchpins" this gave me the XM , XP , YP , YM values for my display then I deleted this const int TS_LEFT=907,TS_RT=136,TS_TOP=942,TS_BOT=139; part of the code since I didn't think it wouldn't affect the functioning of the code (It didn't). In the void loop part of the code I put

if ((tp.z > 350 and tp.z < 1000) or tp.z == 0){
    Serial.print("X: ");
    Serial.print(String(tp.x));
    Serial.print(" Y: ");
    Serial.println(String(tp.y));
  }
  else{
    Serial.print("Z: ");
    Serial.println(String(tp.z));
  }

and I ran some tests such as changing the minimum pressure and maximum pressure and I worked out the values for my specific screen then I ran some tests again to print 1 to the serial when 1 on the screen was pressed and it worked thank you for your help @david_prentice

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