Touchscreen giving 1000 as max X and Y coords for touch causing duplicate coords

I am using a 3.5" 320*480 touchscreen, with the MCUFRIEND and TouchScreen libraries which when getting the touch result, the max X and Y coordinate that it will generate is 1000 resulting in duplicate coordinates as when it goes over 1000, it goes back to 0, therefore there are multiple points with, for example: the coordinates X:300, Y:450, making it so when I have a button and set coordinates to check if the touch was inside the bounds of the button, i can activate the button not only by pressing the button, but also at the duplicate point elsewhere.

My code gives me the X and Y points when I touch a point so I am using a stylus to touch the corners of the rectangle which I want to be my button, and setting the resulting X and Y values to variables, to check if the touch result is between, so I know when the button has been pressed.

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

//    #include <Fonts/FreeSerif9pt7b.h>
//    tft.setFont(&FreeSerif9pt7b);


char *name = "Please Calibrate.";  //edit name of shield
const int XP=8,XM=A2,YP=A3,YM=9; //320x480 ID=0x9486
const int TS_LEFT=919,TS_RT=134,TS_TOP=94,TS_BOT=942;
const int TS_MINX=130,TS_MAXX=905,TS_MINY=70,TS_MAXY=920;
//const int TS_LEFT=135,TS_RT=907,TS_TOP=941,TS_BOT=94;
//const int TS_MINX=130,TS_MAXX=905,TS_MINY=70,TS_MAXY=920;

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

#define MINPRESSURE 200
#define MAXPRESSURE 1000

const int Mode1_X1=162,Mode1_X2=321,Mode1_Y1=370,Mode1_Y2=466; // Setting x1-x2 y1-y2 to the outer bounds for button to switch modes
uint16_t ID, oldcolor, currentcolor;
uint8_t Orientation = 2;    //PORTRAIT INVERTED

// Assign human-readable names to some common 16-bit color values:
#define BLACK   0x0000
#define BLUE    0x001F
#define RED     0xF800
#define GREEN   0x07E0
#define CYAN    0x07FF
#define MAGENTA 0xF81F
#define YELLOW  0xFFE0
#define WHITE   0xFFFF


void colorselector() {
    tft.fillScreen(BLACK);
    int x = (tft.width() / 2);
    int y = (tft.height() / 2);
    tft.fillCircle(x, y, 25, RED);
    Serial.println("colorselector ran");
}

void setup(void)
{
    uint16_t tmp;

    tft.reset();
    ID = tft.readID();
    tft.begin(ID);
    Serial.begin(9600);
    tft.setRotation(Orientation);
    tft.fillScreen(BLACK);
    tft.setCursor(0, 0);
    tft.setTextSize(2);
    tft.print("Hello World!");

    // Draw buttons
    tft.drawRect(tft.width() / 2, 0, tft.width() / 2, 100, BLUE);

}

/*
void drawGreenRect() {
  int rectWidth = 50;
  int rectHeight = 50;
  int x = (tft.width() / 2) - (rectWidth / 2);
  int y = (tft.height() / 2) - (rectHeight / 2);
  tft.fillRect(x, y, rectWidth, rectHeight, GREEN);
  tft.fillCircle(x, y, rectWidth, RED);
} */

void loop()
{
    
  TSPoint touch = ts.getPoint();
  pinMode(XM, OUTPUT);
  pinMode(YP, OUTPUT);
  if (touch.z > MINPRESSURE && touch.z < MAXPRESSURE)
  {
    if (0) //(touch.x > 450 && touch.x < 570  && touch.y > 450 && touch.y < 570) //(touch.x > Mode1_X1 && touch.x < Mode1_X2 && touch.y > Mode1_Y1 && touch.y < Mode1_Y2)
    {
    colorselector();
    }
  
      else
      { ///*
        int16_t x = map(touch.x, TS_MINX, TS_MAXX, tft.width(), 0);
        int16_t y = map(touch.y, TS_MINY, TS_MAXY, tft.height(), 0);

        tft.setTextSize(3);
        tft.setTextColor(RED, BLACK);
        tft.setCursor(10, 25);
        tft.print("X: ");
        tft.println(x);
        tft.setCursor(10, 55);
        tft.print("Y: ");
        tft.println(y);

        Serial.print("X: ");
        Serial.print(x);
        Serial.print(", Y: ");
        Serial.println(y);
        
        Serial.println("loop ran");

        //colorselector();
      }
  }
  delay(100);
}

Does each offset of the false location of touch map exactly with the true location of touch?

I'm not sure I understand the question, but say I have the bounds for my box as 100-200 on the X and 400-500 on the Y, it will activate if I press between those coordinates on the first point that shares the coordinates, or the second duplicate point after the coordinates go back to 0.

position_diff

What are the values for touch.x and touch.y for each corner before map function?

Below are screenshots of the 4 corners values with the map function removed
The X value no longer has duplicates, because it goes to 900 max as the screen is too small to give a value over 1000, however at the very top of the screen on the Y axis, it goes over 1000 and starts to give duplicate values.

Also not 100% sure what the map function does as I have modified an example to create this code, so not sure if I need it or not?

  else
      { ///*
       // int16_t x = map(touch.x, TS_MINX, TS_MAXX, tft.width(), 0);
       // int16_t y = map(touch.y, TS_MINY, TS_MAXY, tft.height(), 0);

        tft.setTextSize(3);
        tft.setTextColor(RED, BLACK);
        tft.setCursor(10, 25);
       /* tft.print("X: ");
        tft.println(x);
        tft.setCursor(10, 55);
        tft.print("Y: ");
        tft.println(y);
/*
        Serial.print("X: ");
        Serial.print(x);
        Serial.print(", Y: ");
        Serial.println(y);
        */
        Serial.println("loop ran");
        tft.setCursor(10, 25);
        tft.print("X: ");
        tft.println(touch.x);
        tft.setCursor(10, 55);
        tft.print("Y: ");
        tft.println(touch.y);

        //colorselector();
      }

Removal of the map function also seemed to invert the way the numbers are measured, as in the X value decreases from left to right and the Y value decreases from bottom to top, the opposite of before.

Please embed the images here using forum tool. Link to external source doesn't help when using smartphone.

Edit previous post too.

And those imgur adverts... niiice.

Sorry, first time posting on this forum and posting from my computer so assumed it was fine, I have updated both posts now thanks for the heads up.

Hi!

Try this:

 const int TS_MINX = 905, TS_MAXX = 140, TS_MINY = 120, TS_MAXY = 945;

  int16_t x = map(touch.x, TS_MINX, TS_MAXX, 0, 320);
  int16_t y = map(touch.y, TS_MINY, TS_MAXY, 0,  480);

Using the value from your pics I can get this coordinates:

  (9, 2)            (321, 0)

(22, 470)         (316, 478)

Which makes sense to me.

You can check it here:

Best regards.

Thanks heaps for the help, that fixed the X value but not the Y, however when I compared the value it printed in the serial monitor to the one on screen, the serial monitor value was going all the way down until it reached the edge of the screen, whereas on the touch screen it would reset to 1000, I realised that this problem was caused by the fact I wasn't clearing the last digit on the end when I printed the value, so it was printing for example "97" but was displaying "970" as it never cleared the 0.

        tft.setTextSize(3);
        tft.setTextColor(RED, BLACK);
        Serial.println(touch.x);
        Serial.println(touch.y);
        Serial.println("loop ran");
        tft.setCursor(10, 200);
        tft.print("X: ");
        tft.print(touch.x);
        tft.setCursor(10, 230);
        tft.print("      ");
        tft.setCursor(10, 230);
        tft.print("Y: ");
        tft.print(touch.y);

Glad to hear that the problem was solved.

Take a look in my posts in this topic to learn how to build and manage menus.

Thank you, will do.

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