I am using an UNO with a four wire resistive touchscreen overlay similar to the type used on the Nintendo Ds but much larger, probably closer to eleven inches. Whenever I touch the screen I only get output from one LED out of four and not necessarily in the areas defined in my code. I have tried code from scratch and modifying samples I have found online as well in the Ardino books I own. The sample codes I have found all involve the brightness of LEDs and the one LED that lights up does seem affected by the area that I touch. I simply want to crate four virtual buttons on the screen that when I touch those defined areas I get an output pulse to a relay.
[code/*]
* ds_touch_screen_mood_light.PDE - Arduino sketch - approx 1552 bytes
* Read from a touch panel the touch position
* Then light up RGB LED using 3 split screen on Y and fading on X
*
* Hardware used:
* Resistive touch panel, 4 wire (DS touch screen)
* Arduino board, LEDs
*
* May 25th 2009
* by Robin Whitfield
*
* *********
* Original touch screen code:
* April 5th 2009
* by Marco Nicolato
* *********
*/
// vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
// Touch panel wiring
//
//When looking at DS screen with connector bottom left they are in order going down as follows:
//TOP
//LEFT
//BOTTOM
//RIGHT
//
// Digital connections (used to drive power)
#define Lo 8 // LEFT to digital output 2
#define Bo 9 // BOTTOM to digital output 3
#define Ro 10 // RIGHT to digital output 4
#define To 11 // TOP to Digital output 5
// Analog connections (used to read the touch position)
#define Ti 3 // TOP also to analog input 3
#define Ri 4 // RIGHT also to analog input 4
//define the LED pins:
#define LEDR 0 //set LEDR as pin 8
#define LEDG 1 //set LEDG as pin 9
#define LEDB 2 //set LEDB as pin 10
#define LEDY 4
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
// vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
// set initial touched position
int touchX = 0;
int touchY = 0;
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
void setup()
{
Serial.begin(9600);
pinMode(LEDR, OUTPUT); //set all LED pins as output pins
pinMode(LEDG, OUTPUT); //"
pinMode(LEDB, OUTPUT); //"
pinMode(LEDY, OUTPUT);
}
void loop()
{
if (touched()) //if the screen is touched then...
{
Serial.print("X=");
Serial.print(touchX); //prints X to the serial
Serial.print(" Y=");
Serial.println(touchY); //print
if((touchX > 870 && touchX < 880) && (touchY > 570 && touchY < 580))
digitalWrite(LEDB, HIGH); //write brightness to blue LED
delay(50);
}
else{
digitalWrite(LEDB, 0);
if((touchX > 885 && touchX < 895) && (touchY > 555 && touchY < 565))
{
digitalWrite(LEDR, HIGH); //write brightness to red LED
}
else{
digitalWrite(LEDR, 0);
}
if((touchX > 895 && touchX < 905) && (touchY > 610 && touchY < 620))
{
digitalWrite(LEDG, HIGH); //write brightness to green led
delay(50);
}
else{
digitalWrite(LEDG, 0);
}
if((touchX > 920 && touchX < 930) && (touchY > 606 && touchY < 620))
{
digitalWrite(LEDY, HIGH); //write brightness to green led
delay(50);
}
else{
digitalWrite(LEDY, 0);
}
}
}
// return TRUE if touched, and set coordinates to touchX and touchY
boolean touched()
{
boolean touch = false;
// Horizontal routine - set L to gnd and R to Vcc
// set L = ground
pinMode(Lo, OUTPUT);
digitalWrite(Lo, LOW);
// set R = Vcc
pinMode(Ro, OUTPUT);
digitalWrite(Ro, HIGH);
// T e B high impedance (input mode)
pinMode(To, INPUT);
pinMode(Bo, INPUT);
// wait a bit, then read from Top
delay(10);
touchX = analogRead(Ti);
// Vertical routine - set B to gnd and T to Vcc
// Set B = gnd
pinMode(Bo, OUTPUT);
digitalWrite(Bo, LOW);
// set T = Vcc
pinMode(To, OUTPUT);
digitalWrite(To, HIGH);
// R e L high impedance (input mode)
pinMode(Ro, INPUT);
pinMode(Lo, INPUT);
// wait a bit, then read from Right
delay(10);
touchY = analogRead(Ri);
// Only read touch if coords are below 1000 and above 0 (stops errors with certain screens)
if(touchX < 1000 and touchX > 0 and touchY < 1000 and touchY > 0)
touch = true;
return touch;
}
I am using an UNO with a four wire resistive touchscreen overlay similar to the type used on the Nintendo Ds but much larger, probably closer to eleven inches. Whenever I touch the screen I only get output from one LED out of four and not necessarily in the areas defined in my code. I have tried code from scratch and modifying samples I have found online as well in the Ardino books I own. The sample codes I have found all involve the brightness of LEDs and the one LED that lights up does seem affected by the area that I touch. I simply want to crate four virtual buttons on the screen that when I touch those defined areas I get an output pulse to a relay.
I am using the wiring like the attached image with the exception of changing the pins to the ones used in the code. I am pulling ground off the same block of wires used with pin # 10 in case it matters.
I used the attached code successfully with a larger 4-wire touchscreen. I can't recall where I got it from - I think it was for a NintendoDS screen. As you will see it uses 4 analog inputs. I haven't experimented with any other arrangement. More recently I got a NintendoDS touch screen but I haven't tried it yet.
void setup() {
Serial.begin(9600);
}
void loop() {
Serial.print("Ax ");
Serial.print(readXpos());
Serial.print(" Ay ");
Serial.println(readYpos());
delay(500);
}
int readXpos() {
pinMode(A0, INPUT);
pinMode(A1, OUTPUT);
pinMode(A2, INPUT);
pinMode(A3, OUTPUT);
digitalWrite(A3, HIGH);
digitalWrite(A1, LOW);
int anX = analogRead(A0);
anX = analogRead(A0);
return(anX);
}
int readYpos() {
pinMode(A0, OUTPUT);
pinMode(A1, INPUT);
pinMode(A2, OUTPUT);
pinMode(A3, INPUT);
digitalWrite(A2, HIGH);
digitalWrite(A0, LOW);
int anY = analogRead(A1);
anY = analogRead(A1);
return(anY);
}
I appreciate the reply, I was certain that after I made such a sloppy first post I would never get a response. I have been playing around with the code you provided me and although I haven't got it to work the way I need it yet I will keep trying.
Your code and mine both give me a serial reading when I open the serial monitor. What I want to do is send a pulse to an output (i.e. LED or relay) whenever I touch the screen in one of the areas marked on the paper I placed under the overlay and no where else. My code was sending an output on any touch and to only one pin.
I find it
very difficult to
read your snippets
when the code
jerks all over the place.
Put each {
on a new line
and use Tools + Auto Format
before posting code.
It just might make it obvious what is wrong with your code.
#define Lo 8 // LEFT to digital output 2
#define Bo 9 // BOTTOM to digital output 3
#define Ro 10 // RIGHT to digital output 4
#define To 11 // TOP to Digital output 5
// Analog connections (used to read the touch position)
#define Ti 3 // TOP also to analog input 3
#define Ri 4 // RIGHT also to analog input 4
//define the LED pins:
#define LEDR 0 //set LEDR as pin 8
#define LEDG 1 //set LEDG as pin 9
#define LEDB 2 //set LEDB as pin 10
#define LEDY 4
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
// vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
// set initial touched position
int touchX = 0;
int touchY = 0;
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
void setup()
{
Serial.begin(9600);
pinMode(LEDR, OUTPUT); //set all LED pins as output pins
pinMode(LEDG, OUTPUT); //"
pinMode(LEDB, OUTPUT); //"
pinMode(LEDY, OUTPUT);
}
void loop()
{
if (touched()) //if the screen is touched then...
{
Serial.print("X=");
Serial.print(touchX); //prints X to the serial
Serial.print(" Y=");
Serial.println(touchY); //print
if((touchX > 870 && touchX < 880) && (touchY > 570 && touchY < 580))
digitalWrite(LEDB, HIGH); //write brightness to blue LED
delay(50);
}
else
{
digitalWrite(LEDB, 0);
if((touchX > 885 && touchX < 895) && (touchY > 555 && touchY < 565))
{
digitalWrite(LEDR, HIGH); //write brightness to red LED
}
else
{
digitalWrite(LEDR, 0);
}
if((touchX > 895 && touchX < 905) && (touchY > 610 && touchY < 620))
{
digitalWrite(LEDG, HIGH); //write brightness to green led
delay(50);
}
else
{
digitalWrite(LEDG, 0);
}
if((touchX > 920 && touchX < 930) && (touchY > 606 && touchY < 620))
{
digitalWrite(LEDY, HIGH); //write brightness to green led
delay(50);
}
else
{
digitalWrite(LEDY, 0);
}
}
}
// return TRUE if touched, and set coordinates to touchX and touchY
boolean touched()
{
boolean touch = false;
// Horizontal routine - set L to gnd and R to Vcc
// set L = ground
pinMode(Lo, OUTPUT);
digitalWrite(Lo, LOW);
// set R = Vcc
pinMode(Ro, OUTPUT);
digitalWrite(Ro, HIGH);
// T e B high impedance (input mode)
pinMode(To, INPUT);
pinMode(Bo, INPUT);
// wait a bit, then read from Top
delay(10);
touchX = analogRead(Ti);
// Vertical routine - set B to gnd and T to Vcc
// Set B = gnd
pinMode(Bo, OUTPUT);
digitalWrite(Bo, LOW);
// set T = Vcc
pinMode(To, OUTPUT);
digitalWrite(To, HIGH);
// R e L high impedance (input mode)
pinMode(Ro, INPUT);
pinMode(Lo, INPUT);
// wait a bit, then read from Right
delay(10);
touchY = analogRead(Ri);
// Only read touch if coords are below 1000 and above 0 (stops errors with certain screens)
if(touchX < 1000 and touchX > 0 and touchY < 1000 and touchY > 0)
touch = true;
return touch;
}
This is my full code and my apologies for posting so sloppy.
I've taken a copy of your code. It seemed like I had to delete 1000 blank lines before I could see more than a couple of lines at a time. Whitespace can be very useful but you have far too much. When you are posting another version prune the whitespace first.
I can't quickly relate your code to your short description of your problem
Despite my if and else it still wanted to output on any touch and only to one pin.
I did notice that you seem to have if (touched()) ... light an LED and also (effectively) else if not touched ... light an LED. Where do the values for touchX and touchY come from in the else clause?
I think you might be better to read the touch screen and store the values separately from the IF/ELSE logic.
If my comments don't make any sense please describe your problem in a lot more detail - what should happen and what actually happens.