touch screen problem

hello, i am using 2.4 TFT LCD with id 7783. my LCD and touch screen is working good from libraries used from smokeandwires website. but, when i try to use touch screen through the code given below it is not working. the voltage read from the analog pin is not linear. but the same device through library file work correctly and getting linear voltage(i.e Y is constant when varied in X and vice versa).

my code:

#define X1 A2
#define X2 6
#define Y1 A1
#define Y2 7

void setup()
{
Serial.begin(9600);
}

void loop()
{
int X,Y; //Touch Coordinates are stored in X,Y variable
pinMode(Y1,INPUT);
pinMode(Y2,INPUT);
digitalWrite(Y2,LOW);
pinMode(X1,OUTPUT);
digitalWrite(X1,HIGH);
pinMode(X2,OUTPUT);
digitalWrite(X2,LOW);
X = (analogRead(Y1); //Reads X axis touch position

pinMode(X1,INPUT);
pinMode(X2,INPUT);
digitalWrite(X2,LOW);
pinMode(Y1,OUTPUT);
digitalWrite(Y1,HIGH);
pinMode(Y2,OUTPUT);
digitalWrite(Y2,LOW);
Y = (analogRead(X1); //Reads Y axis touch position

//Display X and Y on Serial Monitor
Serial.print("X = ");
Serial.print(X);
Serial.print(" Y = ");
Serial.println(Y);
delay(100);
}

code from library:

int TouchScreen::readTouchX(void) {
pinMode(_yp, INPUT);
pinMode(_ym, INPUT);
digitalWrite(_yp, LOW);
digitalWrite(_ym, LOW);

pinMode(_xp, OUTPUT);
digitalWrite(_xp, HIGH);
pinMode(_xm, OUTPUT);
digitalWrite(_xm, LOW);

return (1023-analogRead(_yp));
}

int TouchScreen::readTouchY(void) {
pinMode(_xp, INPUT);
pinMode(_xm, INPUT);
digitalWrite(_xp, LOW);
digitalWrite(_xm, LOW);

pinMode(_yp, OUTPUT);
digitalWrite(_yp, HIGH);
pinMode(_ym, OUTPUT);
digitalWrite(_ym, LOW);

return (1023-analogRead(_xm));
}

what is wrong in my approach?

thanks in advance

hi sosaleykj,

what do you mean by "not linear", is it noisy?
could this line of the original sketch be important: 1023-analogRead(_yp)

regards

hi cidos, when you move your finger straightly in x direction the value of y should be constant and vice versa. subtraction of 1023 is not a issue here.

the main problem is, the touch screen is not acting as a potential driver but as variable resistor in my code. but its working perfectly with the library code.

the problem i think is the initialization of the LCD, which some how disturbing the pins of touch screen(LCD and touch screen pins shared).

problem solved. LCD initialization is the problem. since i/o pins are shared, first the TFT LCD should be initialized and then manual reading of touch screen works well. after reading the touch screen data the i/o pin has to be made as OUTPUT pin.

:grin: