I am trying to make keypad on a touch screen LCD. I managed to get the visual part of it down and am a little stuck on getting the buttons to behave the way I want when clicked.
I can detect touch and look for touch in specified areas (x min - x max && y min - y max), but my problem is that when it does detect touch in specified areas, it spams it as long as it is pressed down.
When you add the "delay(1000);' to the code, it still spams it but now it just takes longer to spam because it is also spamming the delays.
I would like to Serial.writeln("1"); only to be sent once when the 1 button is clicked and or held down. I have tried using millis() but was still not getting expected output.
Does anyone know how to work around this?
void loop()
{
//==> see if there's any touch data for us
if(!screen.bufferEmpty())
{
//==> retrieve the point
TS_Point point = screen.getPoint();
//==> map the point
int x = map(point.x, screen_minx, screen_maxx, 0, touch.width());
int y = map(point.y, screen_miny, screen_maxy, 0, touch.height());
//if clicked in this area do this
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.println("1");
delay(1000);
}
}
} //==> touch check end if
} //==> void loop end
My two cents... just make a boolean variable so when the key is pressed the first time it sets key pressed to True. While key pressed is True, don't check for key presses. Reset key pressed to False after a specified number of milliseconds.
luxxtek:
stay in a "do - while" loop as long as you detect the screen is touched
when released Serial.print("whatever has been pressed")
mainlin:
My two cents... just make a boolean variable so when the key is pressed the first time it sets key pressed to True. While key pressed is True, don't check for key presses. Reset key pressed to False after a specified number of milliseconds.
I must be making some type of logic error, now when ever i press the button.. it spams it until I click an area outside of the buttons IF x/y range.
Here it is with do-loop and pressed boolean
void loop()
{
int x , y;
boolean pressed = false;
//==> check for press
do {
//==> 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;
} while(!screen.bufferEmpty() && pressed == false);
if((x > one_fx) && (x < (one_fx + one_fw)) && (pressed == true)) { // 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");
}
}
} //==> void loop end