time elapsed funtion

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/ :slight_smile:

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):

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);
}

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?

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.

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

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

Elapsed from when ?

PaulS:

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.

The code in my first reply uses neither an object, nor an array.

You just define a variable to store the time within the function you want to do the elapsed time checking. Pass that to the checking function, as I have done in my first reply.

harshvardhan:
is there absolutely NO workaround?

There are loads of potential workarounds, but I have no idea which ones may be relevant to your problem because you haven't described it.

If the different values passed to timeElapsed() represent different conceptual things that needs to be done at different intervals, then you could write a function specific to each thing that encapsulates the previous time, and the interval, and the code to compare the elapsed time against the interval. It's not a brilliant approach, but it's one possible approach.

You could make a simple function like this:

int hasTicked (unsigned long time)
{
  if (millis() % time == 0)
    return 1;
  else
    return 0;
}

Though the drawback over using the Blink Without Delay concepts or timer interrupts is that you could potentially miss some of the ticks depending on how often you make the call, or get multiple 1 returns per tick. You would need to use signal edge detection and avoid anything that blocks for extended periods.

ok. i am telling you what i need.

i need to trigger stuff(almost the same thing as a blink without delay) at certain intervals of time only.
the thing is, there are many trigger time value, so i want to make a funtion.

i cant afford to delay the code .

p.s.
i have no clue about interrupts; would that be of any help in this situation ?

If I interpret what you say correctly you have a number of things that may start timers, each of which is independant. Let's assume for the moment that the timers are started by 3 buttons. You could put the delay time for each button in an array then each time through loop() use the same technique as BlinkWithoutDelay to check whether the time for each button had elapsed. All the variables used to check the eplased time would also be in arrays. Do this using a for loop and set a flag in an array indicating that the time had elapsed for that timer.

Then using another for loop go through the flag array and take the appropriate action for each finished timer. The second for loop could be combined with the first one but it seems better to me to separate reading the timers and taking actions. None of the above is blocking code but you would need to ensure that your action routines do not block code execution either.

You could put some or all of this in functions if you wanted to.