Arduino TFT LCD, X and Y inverted?

Hello everyone! I was working with a LCD touchscreen shield ( Link given below ) and encountered an issue with the touchscreen. The text display is fine but the issue is that the X and Y axis of the touch control is flipped, so in order to press a button in one corner I have to press the opposite corner etc. This also applies to drawing on the touchscreen. If I draw a line horizontally, it appears vertically from the top.

Can I invert the X and Y axis from the given code below?
I am using an Aduino UNO and arduino IDE version 1.8.13.
I am not getting any errors during compilation or during uploading.

TFT LCD touchscreen - https://www.amazon.com/HiLetgo-Display-ILI9341-240X320-Arduino/dp/B0722DPHN6/ref=sr_1_8?crid=1KF1F59VP3J98&dchild=1&keywords=arduino+touchscreen+display&qid=1599058930&sprefix=arduino+touch%2Caps%2C409&sr=8-8
Adafruit TFT LCD text / graphics display library - GitHub - adafruit/TFTLCD-Library: Arduino library for 8-bit TFT LCDs such as ILI9325, ILI9328, etc
Adafruit TFT LCD touchscreen display library - https://github.com/adafruit/Adafruit_TouchScreen

Code I am using ( for the painting on screen ):

// Paint example specifically for the TFTLCD breakout board.
// If using the Arduino shield, use the tftpaint_shield.pde sketch instead!
// DOES NOT CURRENTLY WORK ON ARDUINO LEONARDO

#include <Adafruit_GFX.h>    // Core graphics library
#include <Adafruit_TFTLCD.h> // Hardware-specific library
#include <TouchScreen.h>

#if defined(__SAM3X8E__)
    #undef __FlashStringHelper::F(string_literal)
    #define F(string_literal) string_literal
#endif

// When using the BREAKOUT BOARD only, use these 8 data lines to the LCD:
// For the Arduino Uno, Duemilanove, Diecimila, etc.:
//   D0 connects to digital pin 8  (Notice these are
//   D1 connects to digital pin 9   NOT in order!)
//   D2 connects to digital pin 2
//   D3 connects to digital pin 3
//   D4 connects to digital pin 4
//   D5 connects to digital pin 5
//   D6 connects to digital pin 6
//   D7 connects to digital pin 7

// For the Arduino Mega, use digital pins 22 through 29
// (on the 2-row header at the end of the board).
//   D0 connects to digital pin 22
//   D1 connects to digital pin 23
//   D2 connects to digital pin 24
//   D3 connects to digital pin 25
//   D4 connects to digital pin 26
//   D5 connects to digital pin 27
//   D6 connects to digital pin 28
//   D7 connects to digital pin 29

// For the Arduino Due, use digital pins 33 through 40
// (on the 2-row header at the end of the board).
//   D0 connects to digital pin 33
//   D1 connects to digital pin 34
//   D2 connects to digital pin 35
//   D3 connects to digital pin 36
//   D4 connects to digital pin 37
//   D5 connects to digital pin 38
//   D6 connects to digital pin 39
//   D7 connects to digital pin 40

#define YP A1 // must be an analog pin, use "An" notation!
#define XM A2  // must be an analog pin, use "An" notation!
#define YM 7   // can be a digital pin
#define XP 6   // can be a digital pin

#define TS_MINX 920
#define TS_MINY 120
#define TS_MAXX 150
#define TS_MAXY 940

// For better pressure precision, we need to know the resistance
// between X+ and X- Use any multimeter to read it
// For the one we're using, its 300 ohms across the X plate
TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);

#define LCD_CS A3
#define LCD_CD A2
#define LCD_WR A1
#define LCD_RD A0
// optional
#define LCD_RESET A4

// 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


Adafruit_TFTLCD tft(LCD_CS, LCD_CD, LCD_WR, LCD_RD, LCD_RESET);

#define BOXSIZE 40
#define PENRADIUS 3
int oldcolor, currentcolor;

void setup(void) {
  Serial.begin(9600);
  Serial.println(F("Paint!"));
  
  tft.reset();
  
  uint16_t identifier = tft.readID();

  if(identifier == 0x9325) {
    Serial.println(F("Found ILI9325 LCD driver"));
  } else if(identifier == 0x9328) {
    Serial.println(F("Found ILI9328 LCD driver"));
  } else if(identifier == 0x7575) {
    Serial.println(F("Found HX8347G LCD driver"));
  } else if(identifier == 0x9341) {
    Serial.println(F("Found ILI9341 LCD driver"));
  } else if(identifier == 0x8357) {
    Serial.println(F("Found HX8357D LCD driver"));
  } else {
    Serial.print(F("Unknown LCD driver chip: "));
    Serial.println(identifier, HEX);
    Serial.println(F("If using the Adafruit 2.8\" TFT Arduino shield, the line:"));
    Serial.println(F("  #define USE_ADAFRUIT_SHIELD_PINOUT"));
    Serial.println(F("should appear in the library header (Adafruit_TFT.h)."));
    Serial.println(F("If using the breakout board, it should NOT be #defined!"));
    Serial.println(F("Also if using the breakout, double-check that all wiring"));
    Serial.println(F("matches the tutorial."));
    return;
  }

  tft.begin(identifier);

  tft.fillScreen(BLACK);

  tft.fillRect(0, 0, BOXSIZE, BOXSIZE, RED);
  tft.fillRect(BOXSIZE, 0, BOXSIZE, BOXSIZE, YELLOW);
  tft.fillRect(BOXSIZE*2, 0, BOXSIZE, BOXSIZE, GREEN);
  tft.fillRect(BOXSIZE*3, 0, BOXSIZE, BOXSIZE, CYAN);
  tft.fillRect(BOXSIZE*4, 0, BOXSIZE, BOXSIZE, BLUE);
  tft.fillRect(BOXSIZE*5, 0, BOXSIZE, BOXSIZE, MAGENTA);
  // tft.fillRect(BOXSIZE*6, 0, BOXSIZE, BOXSIZE, WHITE);
 
  tft.drawRect(0, 0, BOXSIZE, BOXSIZE, WHITE);
  currentcolor = RED;
 
  pinMode(13, OUTPUT);
}

#define MINPRESSURE 10
#define MAXPRESSURE 1000

void loop()
{
  digitalWrite(13, HIGH);
  TSPoint p = ts.getPoint();
  digitalWrite(13, LOW);

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

  // we have some minimum pressure we consider 'valid'
  // pressure of 0 means no pressing!

  if (p.z > MINPRESSURE && p.z < MAXPRESSURE) {
    /*
    Serial.print("X = "); Serial.print(p.x);
    Serial.print("\tY = "); Serial.print(p.y);
    Serial.print("\tPressure = "); Serial.println(p.z);
    */
    
    if (p.y < (TS_MINY-5)) {
      Serial.println("erase");
      // press the bottom of the screen to erase 
      tft.fillRect(0, BOXSIZE, tft.width(), tft.height()-BOXSIZE, BLACK);
    }
    // scale from 0->1023 to tft.width
    p.x = map(p.x, TS_MINX, TS_MAXX, tft.width(), 0);
    p.y = map(p.y, TS_MINY, TS_MAXY, tft.height(), 0);
    /*
    Serial.print("("); Serial.print(p.x);
    Serial.print(", "); Serial.print(p.y);
    Serial.println(")");
    */
    if (p.y < BOXSIZE) {
       oldcolor = currentcolor;

       if (p.x < BOXSIZE) { 
         currentcolor = RED; 
         tft.drawRect(0, 0, BOXSIZE, BOXSIZE, WHITE);
       } else if (p.x < BOXSIZE*2) {
         currentcolor = YELLOW;
         tft.drawRect(BOXSIZE, 0, BOXSIZE, BOXSIZE, WHITE);
       } else if (p.x < BOXSIZE*3) {
         currentcolor = GREEN;
         tft.drawRect(BOXSIZE*2, 0, BOXSIZE, BOXSIZE, WHITE);
       } else if (p.x < BOXSIZE*4) {
         currentcolor = CYAN;
         tft.drawRect(BOXSIZE*3, 0, BOXSIZE, BOXSIZE, WHITE);
       } else if (p.x < BOXSIZE*5) {
         currentcolor = BLUE;
         tft.drawRect(BOXSIZE*4, 0, BOXSIZE, BOXSIZE, WHITE);
       } else if (p.x < BOXSIZE*6) {
         currentcolor = MAGENTA;
         tft.drawRect(BOXSIZE*5, 0, BOXSIZE, BOXSIZE, WHITE);
       }

       if (oldcolor != currentcolor) {
          if (oldcolor == RED) tft.fillRect(0, 0, BOXSIZE, BOXSIZE, RED);
          if (oldcolor == YELLOW) tft.fillRect(BOXSIZE, 0, BOXSIZE, BOXSIZE, YELLOW);
          if (oldcolor == GREEN) tft.fillRect(BOXSIZE*2, 0, BOXSIZE, BOXSIZE, GREEN);
          if (oldcolor == CYAN) tft.fillRect(BOXSIZE*3, 0, BOXSIZE, BOXSIZE, CYAN);
          if (oldcolor == BLUE) tft.fillRect(BOXSIZE*4, 0, BOXSIZE, BOXSIZE, BLUE);
          if (oldcolor == MAGENTA) tft.fillRect(BOXSIZE*5, 0, BOXSIZE, BOXSIZE, MAGENTA);
       }
    }
    if (((p.y-PENRADIUS) > BOXSIZE) && ((p.y+PENRADIUS) < tft.height())) {
      tft.fillCircle(p.x, p.y, PENRADIUS, currentcolor);
    }
  }
}

The paint interface on the TFT LCD will look something like this:

Please do help me! Thanks in advance.

I suggest that you install and run the MCUFRIEND_kbv examples.
Report back with any problems.

If all goes well, report back with the output from the Touch Calibration sketch. Copy-paste from the Serial Terminal.

Then we can help you with your chosen "Adafruit_TFTLCD" projects.

David.

All the MCUFRIEND_kbv examples functioned perfectly just except the paint section in one of the touch examples.

OUTPUT from UTouch_calibr_kbv.h ( Run 3 times ):

Please install MCUFRIEND_kbv via the IDE Library Manager.
Run the MCUFRIEND_kbv calibration example.

I have no idea where you got UTouch_calibr_kbv.h
But the output looks complete rubbish.

David.

Image of output:


For some reason I could not copy paste from the serial monitor so I took a screenshot..

Your link says ILI9341. The example output says ILI9325.

The example output is complete rubbish too. The Touch pins and ID look ok but the cx, cy values are silly.

Please verify whether your shield looks like the photos in the Amazon link.
Your image in #0 shows a Blue pcb !!!

Please show a real life photo of your Shield plugged into your Uno (as Nature intended)

You should always be able to copy-paste from the Arduino Serial Terminal e.g. ctl-A, ctl-C with ctl-V to your message.

David.

david_prentice:
Your link says ILI9341. The example output says ILI9325.

Please verify whether your shield looks like the photos in the Amazon link.
Your image in #0 shows a Blue pcb !!!

I have a The example output says ILI9325. I just wanted to show the interface of the paint sketch so I didnt care about the TFT LCD ( I will from now on, sorry ).

Real life photo of the MY TFT LCD:


I figured it out by watching this video! By the way David is MCUFRIEND_kbv your library? Cause it is real good!

Yes, I am the author.

The video shows the basic steps well.

Some of them too swiftly:
e.g. Install library from ZIP (unwise)
e.g. selecting a specific Library Example
e.g. running Touch_Shield example
e.g. copy-paste new calibration values

Some of them too slowly.
e.g. graphics tests
e.g. pressing crosshairs in Calibration example

I strongly recommend using IDE: Tools->Library Manager

Only install from ZIP if library is not available through the Library Manager.
Be aware that your "ZIP install" may need manually deleting if you subsequently want to Update via the Library Manager.

What was your original problem?
Did you press the "wrong" cross-hairs?

David.

david_prentice:
What was your original problem?
Did you press the "wrong" cross-hairs?

You got it. That's all I needed from the video. Great library though!

Please install the current v2.9.9-Release via the Library Manager

The cross-hairs change colour "better".

David.

Just continuing because I encountered yet another small problem. Using the code given below I made a filled box in the left hand corner with the number 5 printed inside it. I want ‘Test’ to display on 100, 100 ( x, y ) when the box containing 5 is pressed ( touchscreen ). It did not display on the tft but it does display in serial. I really don’t know what the problem is.

#include <MCUFRIEND_kbv.h> // MCUFRIEND_kbv library
#include <TouchScreen.h> // Adafruit Touchscreen library
#include <Adafruit_GFX.h> // Adafruit graphics library

MCUFRIEND_kbv tft;
const int XP = 7, XM = A1,YP = A2,YM = 6; 
const int TS_LEFT = 185, TS_RT = 897, TS_TOP = 153, TS_BOT = 887; // Calibrated 

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

int xpos, ypos;

#define white 0xFFFFFF
#define black 0x0000

void setup()
{
  Serial.begin ( 9600 );
  tft.begin ( 0x9325 );

  tft.fillRect ( 0, 0, 50, 50, white );
  tft.setCursor ( 10, 10 );
  tft.setTextSize ( 5 );
  tft.setTextColor ( black );
  tft.print ( "5" ); // Rectangle in left hand corner with 5
}
void loop()
{
  uint16_t xpos, ypos;
  tp = ts.getPoint(); // Getting x, y ( touched )

  xpos = map (tp.x, TS_LEFT, TS_RT, 0, tft.width());
  ypos = map (tp.y, TS_TOP, TS_BOT, 0, tft.height()); // mapping the values

  if ( xpos <= 25 && xpos >= 0 && ypos <= 25 && ypos >= 0 && tp.z >= 100 )
  {
    tft.setCursor ( 100, 100 ); 
    tft.setTextColor ( white );
    tft.setTextSize ( 5 );
    tft.print ( "5" ); // This is the line where it does not work
    Serial.println ( "5" ); // This line works
  }
}

You should always restore pinMode() after a getPoint() call

  tp = ts.getPoint(); // Getting x, y ( touched )
  pinMode(XM, OUTPUT);
  pinMode(YP, OUTPUT);

David.

Really really good. You helped me so much. Couldn't have done it without you! :slight_smile: thanks for all your help!

david_prentice:
You should always restore pinMode() after a getPoint() call

  tp = ts.getPoint(); // Getting x, y ( touched )

pinMode(XM, OUTPUT);
  pinMode(YP, OUTPUT);




David.

Worked by the way!