hi guys please i am having trouble understanding a part of this code, i got it off the net.
how can i relate the output to the actual dimension of my screen so that i can section the screen into 3 equal parts along Y?
i am making use of this screen.
/*=================================
4- Wire Touchscreen Connections
A0=====X+==X1
A1=====X-==X2
A2=====Y+==Y1
A3=====Y-==Y2
=================================*/
int Xresolution = 320; //62.41
int Yresolution = 240; //30.41
// Setting Touch screen connections
#define X1 A0
#define X2 A1
#define Y1 A2
#define Y2 A3
void setup()
{
Serial.begin(9600);
}
void loop()
{
int X,Y; //Touch Coordinates are stored in X,Y variable
pinMode(Y1,INPUT);
pinMode(Y2,INPUT);
if(Y1>0 && Y2>0){ //After contact
digitalWrite(Y2,LOW);
pinMode(X1,OUTPUT);
//digitalWrite(X1,HIGH);
pinMode(X2,OUTPUT);
digitalWrite(X2,LOW);
X = (analogRead(Y1))/(1024/Xresolution); //Reads X axis touch position
}
pinMode(X1,INPUT);
pinMode(X2,INPUT);
if(X1>0 && X2>0){
digitalWrite(X2,LOW);
pinMode(Y1,OUTPUT);
//digitalWrite(Y1,HIGH);
pinMode(Y2,OUTPUT);
digitalWrite(Y2,LOW);
Y = (analogRead(X1))/(1024/Yresolution); //Reads Y axis touch position
}
//Display X and Y on Serial Monitor
if(X>0 || Y>0){ //After contact
Serial.print("X = ");
Serial.print(X);
Serial.print(" Y = ");
Serial.println(Y);
delay(100);
}
}
The code I’ve seen for touchscreens has some equations that take the values you get directly and translate them to the coordinates that match your screen.
Is your code printing out anything plausible as you touch the screen variously?
OK if you are getting consistent enough readings at the four corners that’s four point of a rectangle.
The desired coordinates are also four points, with the kitty-corners probably like 0, 0 and 319, 239 or whatever the maximum X and Y are for your display.
So maths. I haven’t found it plain and simple. You can just use the map function, once for each axis.
Here's some demo code, sorry I cannot determine from where it came, but it illustrates gathering the corners and making the transformation.
The equations I speak of are hidden taken care of in the map function. It's something everyone theoretical knows how to write, if you are curious the code can be found on your machine. As can any code that goes into an Arduino sketch.
# include <stdint.h>
# include <SeeedTouchScreen.h>
# define YP A2 // must be an analog pin, use "An" notation!
# define XM A1 // must be an analog pin, use "An" notation!
# define YM 14 // can be a digital pin, this is A0
# define XP 17 // can be a digital pin, this is A3
// Measured ADC values for (0,0) and (210-1,320-1)
// TS_MINX corresponds to ADC value when X = 0
// TS_MINY corresponds to ADC value when Y = 0
// TS_MAXX corresponds to ADC value when X = 240 -1
// TS_MAXY corresponds to ADC value when Y = 320 -1
# define TS_MINX 116*2
# define TS_MAXX 890*2
# define TS_MINY 83*2
# define TS_MAXY 913*2
// For better pressure precision, we need to know the resistance
// between X+ and X- Use any multimeter to read it
// The 2.8" TFT Touch shield has 300 ohms across the X plate
TouchScreen ts = TouchScreen(XP, YP, XM, YM);
void setup(void) {
Serial.begin(9600);
}
void loop(void) {
// a point object holds x y and z coordinates
Point p = ts.getPoint();
// here we transform the to the screen rectangle:
p.x = map(p.x, TS_MINX, TS_MAXX, 0, 240);
p.y = map(p.y, TS_MINY, TS_MAXY, 0, 320);
// we have some minimum pressure we consider 'valid'
// pressure of 0 means no pressing!
if (p.z > __PRESURE) {
Serial.print("X = "); Serial.print(p.x); Serial.print("\tY = "); Serial.print(p.y);
Serial.print("\tPressure = "); Serial.println(p.z);
}
delay(50);
}
It may not work directly with your code approach but that's the kind of thing you need to compute.