Hello,
I am fairly new to programming the arduino and have tried to look on the forum, watch youtube videos, and change lines of code one by one to try to fix the issue. I have spent hours trying to figure this out and I can't.
I am using a Elegoo 2560 board
touchscreen is a Elegoo 2.8" TFT with 0x9341 LCD driver
I am having a problem mapping touch (x,y) coordinates when the touchscreen is in landscape mode (setrotation (1)).
I want to create a button in landscape mode which I can get the button displayed but because I rotated the screen my button (x,y) coordinates do not match with the LCD button coordinates. I have tried swapping p.x and p.y in the mapping code and a bunch of other things, all which have not changed the problem.
My code has the LCD screen displaying (0,0) at the top left of the screen for
(setrotation(1))
Second, what is the mapping code to have x left to right and y top to bottom while in landscape mode?
currently:
top left (x,y) = (0,240)
top right (x,y) = (0,0)
bottom left (x,y) = (320,240)
bottom right (x,y) = (320,0)
What I want:
top left (x,y) = (0,0)
top right (x,y) = (320,0)
bottom left (x,y) = (0,240)
bottom right (x,y) = (320,240)
summary: I am trying to get display coordinates to equal touch coordinates even though the LCD screen has been rotated while touch part does not rotate.
This code I posted below is to see if my x and y LCD and my x and y touchpoint are equal which they are not. By placing the green box I know what point I set that at and therefore know what the point should be.
sorry for the rambling, just trying to give as much information as possible.
#include <Elegoo_GFX.h> // Core graphics library
#include <Elegoo_TFTLCD.h> // Hardware-specific library
#include <TouchScreen.h>
#if defined(__SAM3X8E__)
#undef __FlashStringHelper::F(string_literal)
#define F(string_literal) string_literal
#endif
#define YP A3 // must be an analog pin, use "An" notation!
#define XM A2 // must be an analog pin, use "An" notation!
#define YM 9 // can be a digital pin
#define XP 8 // can be a digital pin
#define TS_MINX 112
#define TS_MAXX 912
#define TS_MINY 78
#define TS_MAXY 903
// pressure resisistance @300ohms
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
//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
Elegoo_TFTLCD tft(LCD_CS, LCD_CD, LCD_WR, LCD_RD, LCD_RESET);
void setup() {
Serial.begin(9600) ;
Serial.println("Program run") ;
pinMode(13,OUTPUT) ;
pinMode(XM, OUTPUT);
pinMode(YP, OUTPUT);
tft.reset() ;
tft.begin(0x9341);
tft.setRotation(1) ;
tft.fillScreen(BLUE) ;
tft.fillRect (50,180, 220,40,GREEN) ;
tft.setCursor(60,190) ;
tft.setTextColor(RED) ;
tft.setTextSize(2) ;
tft.print("I hope this works") ;
#define MINPRESSURE 10
#define MAXPRESSURE 1000
}
void loop () {
digitalWrite (13,HIGH) ;
TSPoint p = ts.getPoint();
digitalWrite (13,LOW) ;
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);
//example code uses lines 1 & 3
p.x = map(p.x, TS_MINX, TS_MAXX, tft.width(), 0);
//p.x = tft.width()-map(p.x, TS_MINX, TS_MAXX, tft.width(), 0);
p.y = (tft.height()-map(p.y, TS_MINY, TS_MAXY, tft.height(), 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(")");
}
}