Complete action in a set time

Hi,
how can I have an if statement that checks if an action e.g. gsm.getStatus() is completed in a set time? For example

if (gsm.getStatus() in 5sec){
do sth
}else{
do sth else
}

how can I have an if statement that checks if an action e.g.

If the getStatus() method is a blocking method, you would need to modify the library to make the function end in the desired time period, IF that is even possible.

anno9:
Hi,
how can I have an if statement that checks if an action e.g.

gsm.getStatus()

is completed in a set time? For example

if (gsm.getStatus() in 5sec){

do sth
}else{
do sth else
}

Using millis(). Take the start time before calling the function. When the function has finished, compare the start time to the present time. If the difference is greater than the target time, the function did not complete within the target time.

What @Henry_Best has suggested will allow you to know how long something takes, but will not allow you to stop the action when a preset time has expired.

To stop an action you first need to know if it is a blocking function, as @PaulS mentioned.

...R

Robin2:
What @Henry_Best has suggested will allow you to know how long something takes

(in the future, if it takes a fixed period of time to complete)

aarg:

What @Henry_Best has suggested will allow you to know how long something takes

(in the future, if it takes a fixed period of time to complete)

If I understand this correctly it is the opposite of what I had in mind. IMHO it would give you historic information.

Perhaps I should have said "What @Henry_Best has suggested will allow you to know how long something took"

...R

anno9:
Hi,
how can I have an if statement that checks if an action is completed in a set time? For example

if (gsm.getStatus() in 5sec) {

do sth
} else {
  do sth else
}

As pseudo-code you've got it; just need to Arduinize it.

unsigned long tmax = 2^32-1UL;
unsigned long t = millis() + 5000UL;

if (t > (tmax - 5000UL)) t = 5000UL - (tmax - t); // crude clock rollover check-fix.

if (gsm.getStatus() && millis() <= t) {
  do sth
} else {
  do sth else
}

This entirely assumes your intention is to branch based on the 'gsm.getStatus()' execution (including interrupts) time. It does NOT ensure a maximum execution time of 5 seconds though. A well written driver would maintain application-critical hardware state(s) using interrupt(s) such that it's transparent and readily available to application coders in volatile struct/class variables.

Another possibility, depending on your objective, might be to set an external hardware timer (RTC alarm, 555, etc.) to 'interrupt' the 'getStatus()' call if it's hanging or crippling the application.

hth ~ js

Robin2:
What @Henry_Best has suggested will allow you to know how long something takes, but will not allow you to stop the action when a preset time has expired.

To stop an action you first need to know if it is a blocking function, as @PaulS mentioned.

The OP didn't ask about stopping the action, only to take different actions depending on whether or not it completed in a set time.

Henry_Best:
The OP didn't ask about stopping the action, only to take different actions depending on whether or not it completed in a set time.

As the OP is nowhere to be seen we may as well continue the discussion among ourselves.

What he said was "an if statement that checks if an action e.g. ... is completed in a set time?"

While your interpretation is perfectly reasonable I don't think it is any better than my supposition that he had in mind doing something before it completed if it was taking too long.

...R

Delta_G:
If you're smart and you do the math right, you don't need this silly rollover fix.

Indeed, neither solution is correct but the extra code provides space to insert an unmistakable comment noting the possibility of the count rollover. The post was 'on topic' and I stand by it.

Indeed, neither solution is correct but the extra code provides space to insert an unmistakable comment noting the possibility of the count rollover. The post was 'on topic' and I stand by it.

Provides space? Really, that is grasping at straws. Suck it up and admit that your "fix" is an unnecessary kludge that you needed because you didn't know the right way to do it.