Introduction: Touch Screen Overlay With Arduino
Recently my friend Ankush purchased some Nintendo DS replacement Touch Screens and breakout boards for an "Arduino Motorcycle RGB led" project he is working on. After having little success with the Touch Screen library and other tutorials online we decided to write the code ourselves.
The code given below is designed to work with RBG led's. Since it gives excellent coordinate readings and is very easy to set up, it can be used for much more than just led's. The code here however is designed to use the X axis to control the hue and the Y axis to control the brightness for led's eventually positioned under Ankush's motorcycle.
When working with these touch screens it became apparent quickly that they are not all the same. Each touch screen gives out different maximum and minimum readings for both the X and Y axis so some tweaking to the code is needed for calibration purposes. Some even have reversed X and Y readings which can be fixed in code, or simply by rotating your touch screen.
Whats needed to tweak the touch screens?
-Find your X and Y minimum/maximum values. (use xPos / yPos)
-Calculate your range
and that's basically it!
Nintendo DS Touch Screen:
http://www.amazon.com/eForCity-Touch-Screen-Nintendo-DS-Lite/dp/B0010LXJYC
https://www.sparkfun.com/products/8977
Touch Screen Breakout
https://www.sparkfun.com/products/9170
<<<<<<<<<<<<<<<<<<<
/*
* Created By: Ankush Verma & Cory Malantonio
* Kushy04@gmail.com
* cgmalantonio@gmail.com
*
* Y- to analog 0
* X+ to analog 1
* Y+ to analog 2
* X- to analog 3
*
*/
int xVal = 0;
int yVal = 0;
int touchX;
int touchY;
int xPos;
int yPos;
int redPin = 11;
int greenPin = 9;
int bluePin = 10;
int rL = 0;
int bL = 0;
int gL = 0;
void setup()
{
pinMode(redPin, OUTPUT);
pinMode(bluePin, OUTPUT);
pinMode(greenPin, OUTPUT);
Serial.begin(38400);
}
void loop()
{
// Set up the analog pins in preparation for reading the Y value
pinMode( A1, INPUT ); // Analog pin 1
pinMode( A3, INPUT ); // Analog pin 3
pinMode( A0, OUTPUT ); // Analog pin 0
digitalWrite( A0, LOW ); // Analog pin 0 as GND connection
pinMode( A2, OUTPUT ); // Analog pin 2
digitalWrite( A2, HIGH ); // Analog pin 2 as +5V connection
yVal = analogRead( 1 ); // Read the Y value
// Set up the analog pins in preparation for reading the Y value
// from the touchscreen
pinMode( A0, INPUT ); // Analog pin 0
pinMode( A2, INPUT ); // Analog pin 2
pinMode( A1, OUTPUT ); // Analog pin 1
digitalWrite( A1, LOW ); // Analog pin 1 as GND connection
pinMode( A3, OUTPUT ); // Analog pin 3
digitalWrite( A3, HIGH ); // Analog pin 3 as +5V connection
xVal = analogRead( 0 ); // Read the x value
//when touchscreen is not pressed it rests at certain coordinates
//this makes sure the coordinates at rest don't get calculated
touchX = xVal;
if( touchX > 20 && touchX < 900 ) {
//// Here is where you need your minimum X value and Range of X
//// (touchX, minimum x, range of x, converts to 0-1024)
xPos = map(touchX, 108.0, 899.0, 0, 1024);
xPos = constrain(xPos, 0, 1024);
}
touchY = yVal;
if( touchY > 20 && touchY < 900 ) {
//// Here is where you need your minimum Y value and Range of Y
//// (touchY, minimum y, range of y, converts to 0-1024)
yPos = map(touchY, 145.0, 892.0, 0, 1024);
yPos = constrain(yPos, 0, 1024);
}
float brightness;
brightness = int (yPos)/1024.0;
//this lovely bit converts 0-1024, into three 255 values
int value = int (xPos);
float RGBslider = (float)value/1024.0;
float redLevel = 128.0 * ( 2 * cos( 2 * PI * (RGBslider + 0.125)));
float greenLevel = 128.0 * ( 2 * cos( 2 * PI * (RGBslider + 0.375)));
float blueLevel = 128.0 * ( 2 * cos( PI * RGBslider));
//allows values of color to be affected by value of brightness
redLevel = redLevel * (brightness);
greenLevel = greenLevel * (brightness);
blueLevel = blueLevel * (brightness);
/*
if (redLevel > 255) redLevel = 255;
if (redLevel < 0) redLevel = 0;
if (greenLevel > 255) greenLevel = 255;
if (greenLevel < 0) greenLevel = 0;
if (blueLevel > 255) blueLevel = 255;
if (blueLevel < 0) blueLevel = 0;
*/
//does same thing as section commented out above
redLevel = constrain(redLevel, 0, 255);
greenLevel = constrain(greenLevel, 0, 255);
blueLevel = constrain(blueLevel, 0, 255);
//rgb led's being used work backwards
//0 = on || 255 = off
//this reverses normal values
rL = 255 - int(redLevel);
bL = 255 - int(blueLevel);
gL = 255 - int(greenLevel);
analogWrite(redPin, rL);
analogWrite(greenPin, gL);
analogWrite(bluePin, bL);
Serial.print(brightness);
Serial.print(":");
Serial.print(rL);
Serial.print(".");
Serial.print(gL);
Serial.print(".");
Serial.println(bL);
//Serial.print(xPos);
//Serial.println(yPos);
delay(10);
}
The code given below is designed to work with RBG led's. Since it gives excellent coordinate readings and is very easy to set up, it can be used for much more than just led's. The code here however is designed to use the X axis to control the hue and the Y axis to control the brightness for led's eventually positioned under Ankush's motorcycle.
When working with these touch screens it became apparent quickly that they are not all the same. Each touch screen gives out different maximum and minimum readings for both the X and Y axis so some tweaking to the code is needed for calibration purposes. Some even have reversed X and Y readings which can be fixed in code, or simply by rotating your touch screen.
Whats needed to tweak the touch screens?
-Find your X and Y minimum/maximum values. (use xPos / yPos)
-Calculate your range
and that's basically it!
Nintendo DS Touch Screen:
http://www.amazon.com/eForCity-Touch-Screen-Nintendo-DS-Lite/dp/B0010LXJYC
https://www.sparkfun.com/products/8977
Touch Screen Breakout
https://www.sparkfun.com/products/9170
<<<<<<<<<<<<<<<<<<<
>>>>>>>>>>>>>>>>>>>
/*
* Created By: Ankush Verma & Cory Malantonio
* Kushy04@gmail.com
* cgmalantonio@gmail.com
*
* Y- to analog 0
* X+ to analog 1
* Y+ to analog 2
* X- to analog 3
*
*/
int xVal = 0;
int yVal = 0;
int touchX;
int touchY;
int xPos;
int yPos;
int redPin = 11;
int greenPin = 9;
int bluePin = 10;
int rL = 0;
int bL = 0;
int gL = 0;
void setup()
{
pinMode(redPin, OUTPUT);
pinMode(bluePin, OUTPUT);
pinMode(greenPin, OUTPUT);
Serial.begin(38400);
}
void loop()
{
// Set up the analog pins in preparation for reading the Y value
pinMode( A1, INPUT ); // Analog pin 1
pinMode( A3, INPUT ); // Analog pin 3
pinMode( A0, OUTPUT ); // Analog pin 0
digitalWrite( A0, LOW ); // Analog pin 0 as GND connection
pinMode( A2, OUTPUT ); // Analog pin 2
digitalWrite( A2, HIGH ); // Analog pin 2 as +5V connection
yVal = analogRead( 1 ); // Read the Y value
// Set up the analog pins in preparation for reading the Y value
// from the touchscreen
pinMode( A0, INPUT ); // Analog pin 0
pinMode( A2, INPUT ); // Analog pin 2
pinMode( A1, OUTPUT ); // Analog pin 1
digitalWrite( A1, LOW ); // Analog pin 1 as GND connection
pinMode( A3, OUTPUT ); // Analog pin 3
digitalWrite( A3, HIGH ); // Analog pin 3 as +5V connection
xVal = analogRead( 0 ); // Read the x value
//when touchscreen is not pressed it rests at certain coordinates
//this makes sure the coordinates at rest don't get calculated
touchX = xVal;
if( touchX > 20 && touchX < 900 ) {
//// Here is where you need your minimum X value and Range of X
//// (touchX, minimum x, range of x, converts to 0-1024)
xPos = map(touchX, 108.0, 899.0, 0, 1024);
xPos = constrain(xPos, 0, 1024);
}
touchY = yVal;
if( touchY > 20 && touchY < 900 ) {
//// Here is where you need your minimum Y value and Range of Y
//// (touchY, minimum y, range of y, converts to 0-1024)
yPos = map(touchY, 145.0, 892.0, 0, 1024);
yPos = constrain(yPos, 0, 1024);
}
float brightness;
brightness = int (yPos)/1024.0;
//this lovely bit converts 0-1024, into three 255 values
int value = int (xPos);
float RGBslider = (float)value/1024.0;
float redLevel = 128.0 * ( 2 * cos( 2 * PI * (RGBslider + 0.125)));
float greenLevel = 128.0 * ( 2 * cos( 2 * PI * (RGBslider + 0.375)));
float blueLevel = 128.0 * ( 2 * cos( PI * RGBslider));
//allows values of color to be affected by value of brightness
redLevel = redLevel * (brightness);
greenLevel = greenLevel * (brightness);
blueLevel = blueLevel * (brightness);
/*
if (redLevel > 255) redLevel = 255;
if (redLevel < 0) redLevel = 0;
if (greenLevel > 255) greenLevel = 255;
if (greenLevel < 0) greenLevel = 0;
if (blueLevel > 255) blueLevel = 255;
if (blueLevel < 0) blueLevel = 0;
*/
//does same thing as section commented out above
redLevel = constrain(redLevel, 0, 255);
greenLevel = constrain(greenLevel, 0, 255);
blueLevel = constrain(blueLevel, 0, 255);
//rgb led's being used work backwards
//0 = on || 255 = off
//this reverses normal values
rL = 255 - int(redLevel);
bL = 255 - int(blueLevel);
gL = 255 - int(greenLevel);
analogWrite(redPin, rL);
analogWrite(greenPin, gL);
analogWrite(bluePin, bL);
Serial.print(brightness);
Serial.print(":");
Serial.print(rL);
Serial.print(".");
Serial.print(gL);
Serial.print(".");
Serial.println(bL);
//Serial.print(xPos);
//Serial.println(yPos);
delay(10);
}