I want to use this chess game but have trouble with LCD, touch coordinate wrong

I want to copy this: Arduino Project Hub but I don't have Adafruit LCD. I have a 3.2" (ID below) If I try the code as-is, I get blank white screen. With a minor change I was able to get it to work:

#include <Adafruit_TFTLCD.h>
#include <stdint.h>
#include "TouchScreen.h"
#include <avr/pgmspace.h>
#include <EEPROM.h>
#include <MCUFRIEND_kbv.h>
MCUFRIEND_kbv tft;

//Adafruit_TFTLCD tft; (removed this since it'd conflict with MCUfriend library)
#define YP A3  // changed these 4 pins to match my LCD
#define XM A2 
#define YM 9 
#define XP 8

I used MCUfriend library for my LCD and I got the display to work but touch feature does not work. It seems backward on the X axis. If I touched something on the right side, it registers something on the left instead. Y axis seems correct.

ID from LCD #1:

Read Registers on MCUFRIEND UNO shield
controllers either read as single 16-bit
e.g. the ID is at readReg(0)
or as a sequence of 8-bit values
in special locations (first is dummy)

reg(0x0000) 00 00	ID: ILI9320, ILI9325, ILI9335, ...
reg(0x0004) 00 00 00 00	Manufacturer ID
reg(0x0009) 00 00 61 00 00	Status Register
reg(0x000A) 00 08	Get Power Mode
reg(0x000C) 00 06	Get Pixel Format
reg(0x0061) 00 00	RDID1 HX8347-G
reg(0x0062) 00 00	RDID2 HX8347-G
reg(0x0063) 00 00	RDID3 HX8347-G
reg(0x0064) 00 00	RDID1 HX8347-A
reg(0x0065) 00 00	RDID2 HX8347-A
reg(0x0066) 00 00	RDID3 HX8347-A
reg(0x0067) 00 00	RDID Himax HX8347-A
reg(0x0070) 00 00	Panel Himax HX8347-A
reg(0x00A1) 00 00 00 00 00	RD_DDB SSD1963
reg(0x00B0) 00 00	RGB Interface Signal Control
reg(0x00B4) 00 02	Inversion Control
reg(0x00B6) 00 0A 82 27 04	Display Control
reg(0x00B7) 00 06	Entry Mode Set
reg(0x00BF) 00 00 00 00 00 00	ILI9481, HX8357-B
reg(0x00C0) 00 21 00 00 00 00 00 00 00	Panel Control
reg(0x00C8) 00 00 00 00 00 00 00 00 00 00 00 00 00	GAMMA
reg(0x00CC) 00 71	Panel Control
reg(0x00D0) 00 00 00	Power Control
reg(0x00D2) 00 00 00 01 01	NVM Read
reg(0x00D3) 00 00 93 41	ILI9341, ILI9488
reg(0x00D4) 00 00 00 00	Novatek ID
reg(0x00DA) 00 00	RDID1
reg(0x00DB) 00 00	RDID2
reg(0x00DC) 00 00	RDID3
reg(0x00E0) 00 0F 16 14 0A 0D 06 43 75 33 06 0E 00 0C 09 08	GAMMA-P
reg(0x00E1) 00 08 2B 2D 04 10 04 3E 24 4E 04 0F 0E 35 38 0F	GAMMA-N
reg(0x00EF) 00 03 80 02 02 02	ILI9327
reg(0x00F2) 00 02 02 02 02 02 02 02 02 02 02 02	Adjust Control 2
reg(0x00F6) 00 01 00 00	Interface Control

I have another LCD but uses different pinout and seller that sold it is completely clueless. The second LCD I have needs Mega LCD board and the pinout seller provided is for Uno version that doesn't work with Mega version of LCD so I don't know which pins goes to where yet. The LCD itself is a separate 40-pin board. I don't think MCUFriend works with Mega only pinout, the D22-D53 end. I have a 40-pin LCD to UNO shield coming but it's about 3 or 4 weeks via China snail post.

Any help please with the touch coordinate on X axis?

Touch Panels are different. Some are wired with XM on different pins. The X, Y values will be different for LEFT, RT, TOP, BOT

Run the Calibration program attached to this message

This version will be in the next Release from the Library Manager. It is only in the GitHub Beta at the moment.

When you read the TouchScreen you should always map the LEFT, RT values from the Calibration to get the actual pixel coordinate.

David.

TouchScreen.h GFX Calibration
Making all control and bus pins INPUT_PULLUP
Typical 30k Analog pullup with corresponding pin
would read low when digital is written LOW
e.g. reads ~25 for 300R X direction
e.g. reads ~30 for 500R Y direction

Testing : (A2, D8) = 24
Testing : (A3, D9) = 31
Diagnosing as:-
XM,XP:  (A2, D8) = 24
YP,YM:  (A3, D9) = 31
ID = 0x9341

cx=891 cy=96 cz=648
cx=889 cy=500 cz=540
cx=890 cy=873 cz=458
cx=520 cy=102 cz=589
cx=519 cy=849 cz=359
cx=149 cy=98 cz=581
cx=159 cy=476 cz=427
cx=145 cy=844 cz=253
MCUFRIEND_kbv ID=0x9341  240 x 320
const int XP=8,XM=A2,YP=A3,YM=9; //ID=0x9341
const int TS_LEFT=923,TS_RT=117,TS_TOP=72,TS_BOT=880;
PORTRAIT CALIBRATION     240 x 320
x = map(p.x, LEFT=923, RT=117, 0, 240)
y = map(p.y, TOP=72, BOT=880, 0, 320)
Touch Pin Wiring XP=8 XM=A2 YP=A3 YM=9
LANDSCAPE CALIBRATION    320 x 240
x = map(p.y, LEFT=72, RT=880, 0, 320)
y = map(p.x, TOP=117, BOT=923, 0, 240)

Landscape is what I need for the chess game. I can't quite figure where that is in the chess code.

wilykat:

TouchScreen.h GFX Calibration

Making all control and bus pins INPUT_PULLUP
Typical 30k Analog pullup with corresponding pin
would read low when digital is written LOW
e.g. reads ~25 for 300R X direction
e.g. reads ~30 for 500R Y direction

Testing : (A2, D8) = 24
Testing : (A3, D9) = 31
Diagnosing as:-
XM,XP:  (A2, D8) = 24
YP,YM:  (A3, D9) = 31
ID = 0x9341

cx=891 cy=96 cz=648
cx=889 cy=500 cz=540
cx=890 cy=873 cz=458
cx=520 cy=102 cz=589
cx=519 cy=849 cz=359
cx=149 cy=98 cz=581
cx=159 cy=476 cz=427
cx=145 cy=844 cz=253
MCUFRIEND_kbv ID=0x9341  240 x 320
const int XP=8,XM=A2,YP=A3,YM=9; //ID=0x9341
const int TS_LEFT=923,TS_RT=117,TS_TOP=72,TS_BOT=880;
PORTRAIT CALIBRATION    240 x 320
x = map(p.x, LEFT=923, RT=117, 0, 240)
y = map(p.y, TOP=72, BOT=880, 0, 320)
Touch Pin Wiring XP=8 XM=A2 YP=A3 YM=9
LANDSCAPE CALIBRATION    320 x 240
x = map(p.y, LEFT=72, RT=880, 0, 320)
y = map(p.x, TOP=117, BOT=923, 0, 240)




Landscape is what I need for the chess game. I can't quite figure where that is in the chess code.

If you are using a Landscape mode, you would use the Landscape Calibration.
The Touch Panel never varies. p.y refers to the longer side i.e. Y in Portrait.
Note that you map p.y to x when in Landscape.

Personally, I find it easier to always refer to the Touch Panel in Portrait mode. But a human finds it easier to see what value appears from the LEFT.

Depending on how your brain works, you might be happier with:

PORTRAIT CALIBRATION     240 x 320
x = map(p.x, LEFT=923, RT=117, 0, 240)
y = map(p.y, TOP=72, BOT=880, 0, 320)
Touch Pin Wiring XP=8 XM=A2 YP=A3 YM=9
LANDSCAPE CALIBRATION    320 x 240
x = map(p.y, P_TOP=72, P_BOT=880, 0, 320);  //P_xxx Portrait Calibration
y = map(p.x, P_RT=117, P_LEFT=923, 0, 240);

Most apps set the orientation once in setup() and never change. Just map() the Touch ADC values to pixel coordinates in one place.

Are you serious about Landscape for chess? I would expect Portrait for the Player. Landscape would be like riding a horse in sidesaddle.

David.

The chess game in the link in my first post is already set to landscape mode. My issue is the touch screen isn't quite right. If I move the stylus up and down, the object in the game moves correctly. But if I move left and right, it's mirrored. It's like the X-axis is backward on my LCD.

20180116 101331 - YouTube might help show my issue.

I made several edits to your Chess program:

//#include <Adafruit_TFTLCD.h>
#include <MCUFRIEND_kbv.h>
...
/*
Adafruit_TFTLCD tft;
    #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
*/
MCUFRIEND_kbv tft;
const int XP = 7, XM = A1, YP = A2, YM = 6; //ID=0x7783
const int TS_LEFT = 176, TS_RT = 896, TS_TOP = 94, TS_BOT = 905;
...
        //        int x = 388 - p.y / 2.4;
        //        int y = p.x / 3.2 - 50;
        int x = map(p.y, TS_TOP, TS_BOT, 0, 320);  //.kbv
        int y = map(p.x, TS_RT, TS_LEFT, 0, 240);  //Portrait calibration
...
    //        int x = 388 - p.y / 2.4;
    //        int y = p.x / 3.2 - 50;
    int x = map(p.y, TS_TOP, TS_BOT, 0, 320);  //.kbv
    int y = map(p.x, TS_RT, TS_LEFT, 0, 240);  //Portrait calibration
...

Yes, in an ideal world I would just map x, y in one place. But your program has two places.

I simply ran the Calibration sketch on a Shield. And copy-pasted those two lines.
You need to replace my Calibration with your one:

const int XP=8,XM=A2,YP=A3,YM=9; //ID=0x9341
const int TS_LEFT=923,TS_RT=117,TS_TOP=72,TS_BOT=880;

Note that I have replaced the magic x,y mapping to use a logical mapping i.e. using the Arduino map() function.

David.

I applied the changes you made and now the X-axis is working but the Y-axis got flipped! A quick fix thankfully:

const int TS_LEFT = 896, TS_RT = 176, TS_TOP = 94, TS_BOT = 905;

I can finally play the chess and with the change to map I'd be able to adjust when I get new LCD.

You have not understood. I posted the Calibration for my ST7781 Shield.
Your Calibration is different. You need to use your Calibration values for your ILI9341 Shield.

You must have used your XM, YP pins because my Touch is on different pins.
As you have noticed my LEFT and RT are opposite to yours.

David.

Final version I used that works using MCUFRIEND library and change to use map so it'd be easy to adjust for different LCD

arduinomega_chess_1_1.ino (73.6 KB)

Your Calibration:

const int XP=8,XM=A2,YP=A3,YM=9; //ID=0x9341
const int TS_LEFT=923,TS_RT=117,TS_TOP=72,TS_BOT=880;

My Calibration:

const int XP = 7, XM = A1, YP = A2, YM = 6; //ID=0x7783
const int TS_LEFT = 176, TS_RT = 896, TS_TOP = 94, TS_BOT = 905;

Seriously, you should use your Calibration and not my Calibration.

Anyway, I hope you enjoy your game of Chess.

David.

wilykat:
Any help please with the touch coordinate on X axis?

Thanks for interest to my project!
There are 2 places where touchscreen coordinates translates to screen coordinaters, with the same formula

  int x=388-p.y/2.4; 
  int y=p.x/3.2-50;

If you need to flip X-coordinate, just add after that "x=320-x;"

How i visual check the exactlity of transformation - by adding "tft.drawPixel(x,y,CYAN);" in proper place

//********************************** 
void gui() {   
  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(XM, OUTPUT);
  pinMode(YP, OUTPUT);    
  int x=388-p.y/2.4; 
  int y=p.x/3.2-50;         
  if (((abs(x-touchx)>5||abs(y-touchy)>5)||drag)&&p.z>MINPRESSURE&&p.z<MAXPRESSURE) {    
    tft.drawPixel(x,y,CYAN);
    touchx=x;
    touchy=y;

About your question of formatting chess figure you are right and i upload Excel table, where all graphics was created and transformed to code

What about button for start new game - there is one hidden in submenu for arrange position (second button from right side).

@Sergey,

The Mcufriend shields in your Instructable come with all different Touch Panels.
As wilykat and I have shown, we have different XM, YP pins and different X, Y directions.

Your magic pixel-map statements work with your TOP=931, BOT=164, LEFT=160, RT=925 Touch Panel (portrait values).
My suggestions make it easier to adapt for different Touch screens.

Oh, you have made a very nice program.

David.

Thank you for help, David!
Later i will remake sketch according to your suggestion.

did anyone tried this with 2.4"tft lcd screen!

i'm trying but getting only a blank white screen

need help......

Go on. Most 2.4 inch screens are 240x320.

Post a link to the actual screen you are using e.g. Ebay sale.

Are you using the sketch from #8 ?
Post a link to the actual sketch you are using if different.

David.