SparkFun Forums 

Where electronics enthusiasts find answers.

Your source for all things Atmel.
Hi, I bought one of your DS touchscreens, and have been programming just a simple reader which will take x and y co-ordinates for the point touched using the Arduino.

The screen is connected through a breakout board to four ADC pins on the Arduino (Due) 0-4 and the code I've written is shown below.

When I try and gather either the x-coord or the y-coord separately (ie: comment out the other function call and Serial.print lines) then I get a pretty good range of values (roughly 80 < x < 940 and 90 < y < 930).

However, when I call both functions and display both via the serial, as in the code below, the ranges are drastically smaller (47 ≤ x ≤ 53 and 937 ≤ y 968). I'd like to understand why there is such a radical difference, and if there's anything that can be done to improve it.

Thanks in advance,
Code: Select all
int x1 = 3;  // analogue pins for touchscreen controllers
int x2 = 1;
int y1 = 0;
int y2 = 2;

int xPosition = 0;
int yPosition = 0;

void setup() {
  Serial.begin(9600);  //start serial communication

void loop() {
   getXposition();  //get x-coordinate
   getYposition();  //get y-coordinate
   Serial.println(xPosition);  //display x-coordinate
   Serial.println(yPosition);  //display y-coordinate

void getXposition() {
  pinMode(14 + x1, OUTPUT);
  pinMode(14 + x2, OUTPUT);
  digitalWrite(14 + x1, HIGH);
  digitalWrite(14 + x2, LOW);
  xPosition = analogRead(y2);

void getYposition() {
  pinMode(14 + y1, OUTPUT);
  pinMode(14 + y2, OUTPUT);
  digitalWrite(14 + y1, HIGH);
  digitalWrite(14 + y2, LOW);
  yPosition = analogRead(x1);
By whoismaha
Don't you have to set x1 and y2 as INPUTs before you read from them?
By trialex
Don't you have to set x1 and y2 as INPUTs before you read from them?
Technically no, the code behind analogRead() automatically does the pinMode(pin, INPUT); for you.

Maybe try increasing the delay() inside the reading functions, or add a delay between the calls tot he reading functions in the loop() section.
By whoismaha
It still doesn't look right to me. If only one function is used.. it is working fine, right? So it's the combination of the two. Which means something isn't being turned off before the other is run.

When you are reading X(via y2) y1 is still outputting HIGH when it should be "disconnected". You could either make it an input or output it low.

That's what i gathered from: ... 20WORK.pdf