OK, I copied the code from a website, and I found a couple of potential problems with the code, so I rewrote it to this:
int Lo = 3;
int Bo = 4;
int Ro = 5;
int To = 2;
int Bi = 4;
int Ri = 5;
int ledPin = 13;
int touchX = 0;
int touchY = 0;
void setup()
{
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
}
void loop ()
{
if (touched())
{
Serial.print ("X = ");
Serial.print (touchX);
Serial.print (" Y = ");
Serial.println (touchY);
digitalWrite (ledPin, HIGH);
delay (50);
digitalWrite (ledPin, LOW);
}
}
boolean touched()
{
boolean touch = false;
// HORIZONTAL ROUTINE! SET L TO GND AND R TO 5V
// Set L to GND:
pinMode(Lo, OUTPUT);
digitalWrite(Lo, LOW);
// Set R to 5V:
pinMode(Ro, OUTPUT);
digitalWrite(Ro, HIGH);
// Read the Bottom and Make the Top floating. To set the top floating, make it an input
pinMode(To, INPUT);
pinMode(Bo, INPUT);
delay(10);
touchX = analogRead(Bi);
// VERTICAL ROUTINE! SET T TO GND AND B TO 5v
//Set Bottom to GND:
pinMode(Bo, OUTPUT);
pinMode(Bo, LOW);
//Set Top to 5V:
pinMode(To, OUTPUT);
pinMode(To, HIGH);
//Read the Right Side and leave the Left Side floating. To set the Left floatng, make it an input
pinMode(Lo, INPUT);
pinMode(Ro, INPUT);
delay(10);
touchY = analogRead(Ri);
if(touchX < 1000 && touchY < 1000)
{
touch = true;
}
return touch;
}
And I changed the pinout accordingly:
Mega Pin: Breakout Pin:
========================
Analog 2 Y1
Analog 3 X2
Analog 4 Y2
Analog 5 X1
Still behaves the same. Here are some experimental results:
1. I decreased the threshold of touchX and touchY to various values, and the Serial Monitor will only display values below that threshold, but it still displays numbers without touching the screen and even when the touchscreen is disconnected from the Mega.
2. When I increase the delay functions, the Serial Monitor still outputs values as described above, but at a slower pace.
3. I decreased the threshold of touchX and touchY to 50, and the Serial Monitor does not output anything, even when I am touching the touchscreen with any amount of force, including more force than I feel comfortable with. I moved to various locations around the TS with no results.
Finally, a little piece of background info about the breakout board. When I received it, I inspected it and found that X2 and Y2 were shorted together with a bad solder job. I have learned over the years to inspect solder work before installing anything. As a result, I desoldered the pins and removed the short-circuit. I am confident that they are no longer short-circuited at te SMT solder point, but I did not take the connector apart and check it out.
Incidentally, Pins 25 and 26 on the FTDI chip of my Mega have a similar solder bridge across them, but they are common to GND anyway...
