I have created a virtual key pad on 2.8" TFT Touch Screen by Adafruit using an Arduino Uno.
This is a copy of the code.
int x , y;
void loop()
{
boolean pressed = false;
//==> check for press
while(pressed == false) {
//==> retrieve the point
TS_Point point = screen.getPoint();
//==> map the point
x = map(point.x, screenminx, screenmaxx, 0, touch.width());
y = map(point.y, screenminy, screenmaxy, 0, touch.height());
pressed = true;
}
if((x > one_fx) && (x < (one_fx + one_fw))) { // x > 0 && x < 80
if((y > one_fy) && (y <= (one_fy + one_fh))) { // y > 83 && y < 143
Serial.print("1: x= ");
Serial.print(x);
Serial.print(" y= ");
Serial.print(y);
Serial.print("\n");
pressed = false; x = 0; y = 0;
}else { pressed = false; x = 0; y = 0; }
}
} //==> void loop end
The idea here was that whenever I press on the screen, the X and Y coordinates would be recorded to variables x and y ... and boolean pressed will be set to TRUE.
Thats what this chunk should do...
//==> check for press
while(pressed == false) {
//==> retrieve the point
TS_Point point = screen.getPoint();
//==> map the point
x = map(point.x, screenminx, screenmaxx, 0, touch.width());
y = map(point.y, screenminy, screenmaxy, 0, touch.height());
pressed = true;
}
Next in line is the if statement.. I plan on having one of these for each button. This is just one for the button number 1. Once I get this one working I plan on just doing same thing for other buttons. But I can not get it to run the way I want to.
if((x > one_fx) && (x < (one_fx + one_fw))) { // x > 0 && x < 80
if((y > one_fy) && (y <= (one_fy + one_fh))) { // y > 83 && y < 143
Serial.print("1: x= ");
Serial.print(x);
Serial.print(" y= ");
Serial.print(y);
Serial.print("\n");
pressed = false; x = 0; y = 0;
}else { pressed = false; x = 0; y = 0; }
}
} //==> void loop end
In order for the above code to execute those print statements 3 conditions must all be true.
1.) The X coordinate must be between 0 and 80.
2.) The Y coordinate must be between 83 and 143.
Both of the values above indicate that the position pressed on the screen is within button number 1.
3.) Boolean pressed must equal true.
If these 3 conditions are not true then the x and y coordinates are set to 0 and pressed is set to false. Which should put us back to the top of the loop waiting for the next X and Y coordinates to be recorded.
Unfortunately the program is not running like that. When I click number 1, it gets the x and y coordinates and sets pressed to true. It should only run once and reset x,y, and pressed to default values. However, it spams the Serial.print's until you click somewhere else on the screen outside of the number 1 range.
This lead me to believe that x, y, and pressed are not being set back to default value or that it is getting stuck in that if statement somehow? But how? It's an if statement and should only be run once if the conditions are all true.
What am I missing here? It's driving me nuts because i've been stuck on it for couple days now. I have tried moving
boolean pressed = false; int x = 0; int y = 0;
to the beginning of the void loop to ensure it is getting reset to default value before doing anything else in the loop again. I have also tried using do-while and while loops in different places but it always puts me back to the same spot no matter what I do.