How to set a timed if statement (IF screen is touched for more than 3 seconds)

Hello, I am using the Adafruti 2.8" TFT capacitive version connected to Leonardo. I am wondering if it is possible to set a condition where:

if (screen is touched for 3 seconds)

{
action a
}
else
{
action b
}

You could start with this for an idea.

It checks for the button to have changed and be high, every time through loop(). You would check for that condition and start a timer (the inner if in that example). Then on subsequent passes through loop() check for it to have changed to low (the else of that example): then note how long it was pressed for.

Ignore the part where it counts the presses. I don't know the logic of those touch screens: if a touch is low then reverse the logic in the example.

That's very off the top of my head: it's 0500 here and I haven't finished my first cup of coffee. 8)

How about time stamps?
I am new to the Arduino thing but on other Hardware i would use the events "screen touched" and "Finger liftet" to store a timestamp each and calculate the difference in e.g. milliseconds.
You can then define your condition as mentioned above to do whatever you want.
Polling the touchscreen with an inner loop to measure the time might freeze the whole system while measuring.
Again, i am new here so please dont kill me if i say something stupid.
Happy making

My bit messy way. but i dont know how to measure if the screen is pressed :smiley:

int rep, timeStart, timepassed;

void loop() {
  while(/*touchpressed*/ ) {
    rep++;
    if(rep == 1) timeStart = millis();
    timepassed = millis() - timeStart;
    if(timepassed >= 3000) {
     // action a
    }
    }
  
  if(timepassed < 3000) {
    //action b
  }
  rep = 0;
}

this is how i would do this. but i can be that this is wrong xD

Ardubambi:
Polling the touchscreen with an inner loop to measure the time might freeze the whole system while measuring.

Don't forget though, that loop() is running anyway, all the time.

It is a sort of timestamp idea actually: millis() runs always, and is the number of milliseconds since power on or reset. So you poll the "switch" (I'm mentally visualising a touch on the screen as the press of a button), log the time at which you first see it pressed, and every time through loop() you look to see if it's still pressed. If it's not, log the new millis() and see how long it was pressed. (Yes it's not impossible you'll miss a release and new press, but at loops in the order of 10s of 1000s of times a second, not really likely.)

Also see the good old standby for things to do with timing, Blink Without Delay.