# LCD touch : increase or decrease as per touch

Hi All,

I’m little stuck on trying to write a code where at every touch on the screen it will display increase or decrease in number. Below is the code I’m trying to use. When every I touch the screen it increase the number from 0 to 100 or decrease the number from 100 to 0 by single touch. I want it to increase or decrease with every touch on its respective location.

This is my first time coding so please be gentle :).

``````	while (true)
{
if (myTouch.dataAvailable())
{
int x1=myTouch.getX(); // assign the x and y coordinates to variables
int y1=myTouch.getY(); //
int i;
if ((y1>=31) && (y1<=49)) {
if ((x1>=142) && (x1<=160)) {
waitForIt(142, 30, 162, 54);
//This is where I'm not able to think of.......
//Increase the number
i++;
myGLCD.printNumI(i, 110, 33);

}
if ((y1>=31) && (y1<=49)) {
if ((x1>=162) && (x1<=179)) {
waitForIt(162, 31, 179, 49);
//This is where I'm not able to think of.......
//Decrease the number
i--;
myGLCD.printNumI(i, 110, 33);

}
}
}
}
}
``````

regards
BW

Try a short delay after you press the screen so you have time to remove your finger before the next reading.

Quick way using delay();

``````while (true)
{
if (myTouch.dataAvailable())
{
int x1=myTouch.getX(); // assign the x and y coordinates to variables
int y1=myTouch.getY(); //
int i;
if ((y1>=31) && (y1<=49)) {
if ((x1>=142) && (x1<=160)) {
waitForIt(142, 30, 162, 54);
//This is where I'm not able to think of.......
//Increase the number
i++;
myGLCD.printNumI(i, 110, 33);
delay(500); // Half second delay

}
if ((y1>=31) && (y1<=49)) {
if ((x1>=162) && (x1<=179)) {
waitForIt(162, 31, 179, 49);
//This is where I'm not able to think of.......
//Decrease the number
i--;
myGLCD.printNumI(i, 110, 33);
delay(500); // Half second delay
}
}
}
}
}
``````

Proper way using millis()

``````long now; // put these before setup with other variables
long end;
while (true)
{
if (myTouch.dataAvailable())
{
int x1=myTouch.getX(); // assign the x and y coordinates to variables
int y1=myTouch.getY(); //
int i;
if ((y1>=31) && (y1<=49)) {
if ((x1>=142) && (x1<=160)) {
waitForIt(142, 30, 162, 54);
//This is where I'm not able to think of.......
//Increase the number
i++;
myGLCD.printNumI(i, 110, 33);
MyDelay(); // Calls proper delay
}
if ((y1>=31) && (y1<=49)) {
if ((x1>=162) && (x1<=179)) {
waitForIt(162, 31, 179, 49);
//This is where I'm not able to think of.......
//Decrease the number
i--;
myGLCD.printNumI(i, 110, 33);
MyDelay(); // Calls proper delay

}
}
}
}
}

void MyDelay()
{
long  now = millis();
long  end = now + 500; // Set delay for half a second
while(millis() < end)
{
// Do what you want during delay
}
}
``````

Even better way cleaning up the code abit and giving more space between buttons for more accurate button reading.
Also erasing previously printed number.

``````int i; // put these before setup with other variables
long now;
long end;
while (true)
{
if (myTouch.dataAvailable())
{
int x1=myTouch.getX(); // assign the x and y coordinates to variables
int y1=myTouch.getY(); //

if ((y1>=31) && (y1<=49)) {
if ((x1>=150) && (x1<=168)) {
waitForIt(150, 31, 168, 49);
//This is where I'm not able to think of.......
//Increase the number
i++;
MyDelay(); // Calls proper delay
}
if ((x1>=178) && (x1<=195)) {
waitForIt(178, 31, 195, 49);
//This is where I'm not able to think of.......
//Decrease the number
i--;
MyDelay(); // Calls proper delay

}
}
}
}
}

void MyDelay()
{
myGLCD.print("   ", 90, 33); // Erase previous number
long  now = millis();
long  end = now + 500; // Set delay for half a second
while(millis() < end)
{
myGLCD.printNumI(i, 90, 33); //print new number during delay
}
}
``````

You can make a proper function that looks at your on-screen button coordinates and does an action when pressed once and not when held down.
NOTE: You can use arrays for multiple button or simply write buttonOne, buttonTwo…etc
Also do the same for boolean button and lastState

``````boolean button = false; // set globally at top of code
boolean lastState;

//------------------MAIN CODE-------------------------
button = TouchButton(142, 30, 162, 54, x1, y1); //This will return true if button is touched and false if not touched.

if(button && button != lastState)// check current button state and compare with last button state
{
i++; // if the two states are different, increment " i ", else do nothing
}
lastState = button; // update lastState
//------------------END OF CODE------------------------------------

boolean TouchButton(unsigned int x1, unsigned int y1, unsigned int x2,  unsigned int y2, unsigned int xc, unsigned int yc)
{ //x1,y1,x2,y2 = button coordinates. xc,yc = touch data gotten from myTouch.getX() / getY()
if(xc >= x1 && xc <= x2 && yc >= y1 && yc <= y2) return true; // will return true if touch coordinate (xc, yc) are within the coordinates of the button.
return false;
}
``````

Thanks for the reply. Currently I'm away on vacation and will not be able to test it. Once i get back and test it I will update.

Regards
BW

Thanks elac & HazardsMind for reply. Both of the reply are working fine for me. I need 1 more help.
I have multiple buttons where I want to increase there values individually. right now what is happening is that when I increase the value from first button it last state is stored and when I click on second button it start from the value stored from first button. I want the value of each button to be independent for each buttons.

regards
BW

The function I gave you will output individual values. This works because you are using different button coordinates, and make sure the buttons don't overlap each other. That's also the same function I made for my library, and it works great. Of course I did add a way to flip the touch values with a swap function that differentiates whether its set for portrait or landscape.