Go Down

Topic: time elapsed funtion (Read 869 times) previous topic - next topic

harshvardhan

i want to write a function that will check if the time(value passed to the funtion ) has elapsed or not.

for eg.

time_elapsed(3000); 

it should return true(1) after every 3000 milliseconds.

p.s.

this function will be called by many parts in the code, each checking for a different value of time. 

thnx in advance/  :)

majenko

For each target time you will need a separate variable to store the elapsed time in.  That gets complex.

You could either have it as a class, where each instance of the class is a time you want to test for, or pass a variable by reference to the function to use.

The latter is "lighter", and could look something like (untested):

Code: [Select]

boolean timeElapsed(unsigned long dt, unsigned long *time)
{
  unsigned long now = millis();

  if (time == 0) {
    time = now;
    return false;
  }

  if (now - time >= dt) {
    time = now;
    return true;
  }

  return false;
}

void something()
{
  unsigned long myTime = 0;

  while (!timeElapsed(3000, &myTime))
    continue;
  digitalWrite(13,HIGH);
}
Get 10% off all 4D Systems TFT screens this month: use discount code MAJENKO10

harshvardhan

uhh.   
so you are telling me to make an object (or an array).

but that is not what i want .
is there absolutely NO workaround?

PaulS

Quote
is there absolutely NO workaround?

Sure there is. It involves a lot more code, though. Unless you simply want the same reference time used in every call to the function.

harshvardhan

no, im sure i will be needing many reference times .

Go Up