Pages: [1]   Go Down
Author Topic: time elapsed funtion  (Read 634 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Jr. Member
**
Karma: 0
Posts: 69
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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/  smiley
Logged

UK
Offline Offline
Faraday Member
**
Karma: 92
Posts: 3969
Where is your SSCCE?!?!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Why not visit my eBay shop? http://stores.ebay.co.uk/Majenko-Technologies
Replacement for the Arduino IDE: UECIDE - Proper serial terminal, graphing facilities, plugins, overhauled internals.
Java isn't bad in itself, but it has enabled morons to write programs.

Offline Offline
Jr. Member
**
Karma: 0
Posts: 69
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 551
Posts: 46230
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 69
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

East Anglia (UK)
Offline Offline
Faraday Member
**
Karma: 90
Posts: 3516
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
i want to write a function that will check if the time(value passed to the funtion ) has elapsed or not.
Elapsed from when ?
Logged

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

UK
Offline Offline
Faraday Member
**
Karma: 92
Posts: 3969
Where is your SSCCE?!?!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
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.
Logged

Why not visit my eBay shop? http://stores.ebay.co.uk/Majenko-Technologies
Replacement for the Arduino IDE: UECIDE - Proper serial terminal, graphing facilities, plugins, overhauled internals.
Java isn't bad in itself, but it has enabled morons to write programs.

UK
Offline Offline
Shannon Member
****
Karma: 184
Posts: 11197
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

I only provide help via the forum - please do not contact me for private consultancy.

California
Offline Offline
Faraday Member
**
Karma: 82
Posts: 3123
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You could make a simple function like this:

Code:
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.
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 69
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 ?
Logged

East Anglia (UK)
Offline Offline
Faraday Member
**
Karma: 90
Posts: 3516
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Pages: [1]   Go Up
Jump to: