The touch the touchscreen only one loop is wanted

have a 3.5" tough screen. In the main loop I wonder if the screen has been touched. Based on the coordinates I determine which "button" has been chosen. But when I press it once the loop won't stop, so when I print the status I get a large repose.
How can I ensure in the added code that the loop is only run through once for the selection of the "button".
Thanks for any help.

void loop()
{
  if (ts.touched()){
    TS_Point p = ts.getPoint();
    t_y = p.x;
    t_x = p.y;
    pressed = true;
    //Serial.printf("%03d   %03d\n",p.x,p.y);
    if (pressed) Serial.println(chckBtn(t_x, t_y, pressed));
  }
}

int chckBtn(int x_pos, int y_pos, bool pressed)
{
  if (y_pos < 96 && pressed) 
  {
    if      ( x_pos >   0 && x_pos <  96 ) return 1;
    else if ( x_pos >  96 && x_pos < 192 ) return 2;
    else if ( x_pos > 192 && x_pos < 288 ) return 3;
    else if ( x_pos > 288 && x_pos < 384 ) return 4;
    else if ( x_pos > 384 && x_pos < 480 ) return 5;  
    pressed = false;
    delay(100);
  }
}

First thing you should share whole code.

And i can't understand properly what are you trying to say.

"When you are touching only once it print multiple times" is this your problem

Or if you want to run code only once you can just put that code in void setup instead of void loop

Look for this example. Can be useful for you.

If I understand your question correctly then your problem is that you are checking to see if a button is pressed rather than checking to see if a button becomes pressed.

Here is one way to do it:

bool lastTouched;

void setup()
{
  // TBD: Initialize everything here
  lastTouched = ts.touched(); // make this the last line in setup()
}
void loop()
{
  bool touched = ts.touched();
  if (touched  != lastTouched ){
     lastTouched = touched;
     if (touched)
     {
        TS_Point p = ts.getPoint();
        t_y = p.x;
        t_x = p.y;
        pressed = true;
       //Serial.printf("%03d   %03d\n",p.x,p.y);
       if (pressed) Serial.println(chckBtn(t_x, t_y, pressed));
     }
     else
     {
         // TBD: optional processing for when there is a transition from touched to not touched
     }
  }
}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.