Having trouble getting touchscreen to work

Hello, I have a tft display with touchscreen, but I can't seem to get the buttons to work.
When pressed in the right coordinates it should print in serial monitor Button Pressed! , but it just wont do it. It will show me the button being pressed, (this is from serial monitor)
X = 894 Y = 178 Pressure = 787
But it won't give the ''Button pressed!'' line
What could be the issue?
The code for this is at the bottom

#include <TouchScreen.h>

#include <Adafruit_GFX.h>    // Core graphics library
//#include <Adafruit_TFTLCD.h> // Hardware-specific library
#include <MCUFRIEND_kbv.h>
MCUFRIEND_kbv tft;

#define	BLACK   0x0000
#define	BLUE    0x001F
#define	RED     0xF800
#define	GREEN   0x07E0
#define CYAN    0x07FF
#define MAGENTA 0xF81F
#define YELLOW  0xFFE0
#define WHITE   0xFFFF
#define PURPLE  0x801A
//touchscreen
uint8_t YP = A1;  // must be an analog pin, use "An" notation!
uint8_t XM = A2;  // must be an analog pin, use "An" notation!
uint8_t YM = 7;   // can be a digital pin
uint8_t XP = 6;   // can be a digital pin
uint8_t SwapXY = 0;

uint16_t TS_LEFT = 920;
uint16_t TS_RT  = 90;
uint16_t TS_TOP = 940;
uint16_t TS_BOT = 140;
char *name = "Unknown controller";

TouchScreen ts = TouchScreen(XP, YP, XM, YM, 500);
TSPoint tp;

#define MINPRESSURE 100
#define MAXPRESSURE 6000

#define SWAP(a, b) {uint16_t tmp = a; a = b; b = tmp;}

int16_t BOXSIZE;
int16_t PENRADIUS = 3;
uint16_t identifier, oldcolor, currentcolor;
uint8_t Orientation = 3;    //PORTRAIT
//Touchscreen

//Adafruit_TFTLCD tft(LCD_CS, LCD_CD, LCD_WR, LCD_RD, LCD_RESET);
// If using the shield, all control and data lines are fixed, and
// a simpler declaration can optionally be used:
// Adafruit_TFTLCD tft;
uint16_t g_identifier;
    const int analogWindow = 50; // analog window width in mS (50 mS = 20Hz)
    unsigned int analog;
    unsigned int base;
    
void setup(void) {
  pinMode(53, OUTPUT);    // sets the digital pin 13 as output
  Serial.begin(9600);
  Serial.println(F("TFT LCD test"));

#ifdef USE_OPENSMART_SHIELD_PINOUT
  Serial.println(F("Using OPENSMART 3.2\" TFT Arduino Shield Pinout"));
#else
  Serial.println(F("Using OPENSMART 3.2\" TFT Breakout Board Pinout"));
#endif

  Serial.print("TFT size is "); Serial.print(tft.width()); Serial.print("x"); Serial.println(tft.height());


    uint32_t when = millis();
    //    while (!Serial) ;   //hangs a Leonardo until you connect a Serial
    if (!Serial) delay(5000);           //allow some time for Leonardo
    Serial.println("Serial took " + String((millis() - when)) + "ms to start");
    static uint16_t identifier;
    //    tft.reset();                 //we can't read ID on 9341 until begin()
    g_identifier = tft.readID(); //
    Serial.print("ID = 0x");
    Serial.println(g_identifier, HEX);
    if (g_identifier == 0x00D3 || g_identifier == 0xD3D3) g_identifier = 0x9481; // write-only shield
    if (g_identifier == 0xFFFF) g_identifier = 0x9341; // serial
//    g_identifier = 0x9329;                             // force ID
	

    tft.begin(g_identifier);//to enable ILI9327 driver code
  tft.setRotation(3);
  
  Serial.println(F("Benchmark                Time (microseconds)"));

  Serial.println(F("Done!"));
  tft.fillScreen(BLACK);
  tft.setCursor(0, 50);
  tft.setTextColor(GREEN);
  tft.setTextSize(2);
   digitalWrite(53, HIGH);
   delay(4000);
  base = analogRead(15);
Serial.println (base);
Serial.println ("base");
tft.fillRect(0, 0, 30, 30, RED);
tft.setTextSize(4);
    tft.setTextColor(RED);
    tft.setCursor(34, 0);
    tft.print("SIZE +");
tft.fillRect(0, 210, 30, 30, BLUE);
tft.setTextSize(4);
    tft.setTextColor(BLUE);
    tft.setCursor(34, 210);
    tft.print("SIZE -");
    //touchscreen
    BOXSIZE = tft.width() / 6;
    
    tft.reset();
    identifier = tft.readID();
  
    }
    //touchscreen
    


void loop(void) {


  int barheight = 30;
    analog = analogRead(15);
    Serial.println(analog);
  for(uint8_t rotation=0; rotation<4; rotation++){
  digitalWrite(53, HIGH);
    analog = analogRead(15);
    Serial.println(analog);
  for(uint8_t rotation=0; rotation<4; rotation++){
   tft.fillRect(110,120,200,barheight,BLACK);
 if((analog>=base-2)&&(analog<=base+2))
  {
tft.fillRect(190,120,20,barheight,RED);
  }
  else if((analog>=base-3)&&(analog<=base+3))
  {
    tft.fillRect(190,120,20,30,RED);
  tft.fillRect(170,120,20,30,MAGENTA);
  tft.fillRect(210,120,20,30,MAGENTA);
  }
    else if((analog>=base-4)&&(analog<=base+4))
  {
     tft.fillRect(190,120,20,30,RED);
  tft.fillRect(170,120,20,30,MAGENTA);
  tft.fillRect(210,120,20,30,MAGENTA);
  tft.fillRect(150,120,20,30,PURPLE);
  tft.fillRect(230,120,20,30,PURPLE);
  }
    else if((analog>=base-5)&&(analog<=base+5))
  {
     tft.fillRect(190,120,20,30,RED);
  tft.fillRect(170,120,20,30,MAGENTA);
  tft.fillRect(210,120,20,30,MAGENTA);
  tft.fillRect(150,120,20,30,PURPLE);
  tft.fillRect(230,120,20,30,PURPLE);
  tft.fillRect(130,120,20,30,BLUE);
  tft.fillRect(250,120,20,30,BLUE);
  }
    else if((analog>=base-6)&&(analog<=base+6))
  {
     tft.fillRect(190,120,20,30,RED);
  tft.fillRect(170,120,20,30,MAGENTA);
  tft.fillRect(210,120,20,30,MAGENTA);
  tft.fillRect(150,120,20,30,PURPLE);
  tft.fillRect(230,120,20,30,PURPLE);
  tft.fillRect(130,120,20,30,BLUE);
  tft.fillRect(250,120,20,30,BLUE);
  tft.fillRect(110,120,20,30,BLUE);
  tft.fillRect(270,120,20,30,BLUE);
  }
  //Touchscreen
 uint16_t xpos, ypos;  //screen coordinates
    digitalWrite(A3,HIGH);
    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);
    pinMode(XP, OUTPUT);
    pinMode(YM, OUTPUT);
     if (tp.z > MINPRESSURE && tp.z < MAXPRESSURE) {
       Serial.print("X = "); Serial.print(tp.x);
     Serial.print("\tY = "); Serial.print(tp.y);
     Serial.print("\tPressure = "); Serial.println(tp.z);
        // is controller wired for Landscape ? or are we oriented in Landscape?
        if (SwapXY != (Orientation & 1)) SWAP(tp.x, tp.y);
        // scale from 0->1023 to tft.width  i.e. left = 0, rt = width
        // most mcufriend have touch (with icons) that extends below the TFT
        // screens without icons need to reserve a space for "erase"
        // scale the ADC values from ts.getPoint() to screen values e.g. 0-239
       
        xpos = map(tp.x, TS_LEFT, TS_RT, 0, tft.width());
        ypos = map(tp.y, TS_TOP, TS_BOT, 0, tft.height());
         if ((tp.x > 850 && tp.x < 900) && (tp.y > 160 && tp.y < 200)) {
            Serial.print("Button Pressed!");
            tft.fillCircle(0, 40, 3, GREEN);

        // are we in top color box area ?
//Touchscreen
 }
     }}}}

First step as always is to press ctrl-T.
Then your code will be formatted nicely and you can see how the logic flows.

                if (SwapXY != (Orientation & 1)) SWAP(tp.x, tp.y);

Since SwapXY = 0 and Orientation = 3 the condition is true.

Seriously, look at the current library examples. The Calibration sketch detects the correct XM, XP, ... pins and gives you the Portrait values.
The Touchshield example shows how to use the Calibration for different rotations.

I advise you to map the Portrait ADC values that you get from ts.getPoint() to the actual xpos, ypos pixel coordinates.
Your head will stop hurting !!

David.

1 Like

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