Execute function if boolean is false for a certain time

Hello, I've searched this topic and all I've found is pages on the sleep() function. Basically I want my program to execute a function after a Boolean expression has been false for a certain amount of time. What I'm doing is a object mouse with a small ball that will click if I close the ball in my hand. I can get it to click if the object is not detected, but it clicks even if the object isn't detected for 1ms. Basically I want to have it so if the objected is found is false for a certain amount of time it will click. Is there anyway I can do this in C++?

Thanks
There are a couple of ways, either with multiple threads or by checking the amount of time that has elapsed since the last iteration of your running loop.
Would something like this work. Bare in mind I've only been coding for 2 weeks in any language, so please don't bash me if this is really bad.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
  
clock_t   t;
  if(ObjectFound == false) {
             t = clock();
if (t > 1 ) {

   LeftMouseClick();

}
}



I wouldn't use clock() for this. If you want to stick with the standard libraries for now then something more like this would be better:
1
2
3
4
5
6
7
8
9
10
11
12
13
clock_t Mark = time();

do
{
    if(difftime(time(), Mark) >= Time_Limit)
    {
          LeftMouseClick();
    }

//Code Code Code


}while(ObjectFound == false);

Or something to that effect. I didn't test this code.

- difftime(): http://www.cplusplus.com/reference/ctime/difftime/

- time(): http://www.cplusplus.com/reference/ctime/time/
Last edited on
Thank you so much!! I can see how this works better.
Topic archived. No new replies allowed.