Author Topic: how top make a program run for certain amount of time?  (Read 2669 times)

0 Members and 1 Guest are viewing this topic.

Offline vipulan12Topic starter

  • Robot Overlord
  • ****
  • Posts: 181
  • Helpful? 0
how top make a program run for certain amount of time?
« on: May 04, 2014, 10:49:42 AM »
hey everyone, I have been working on a project that requires a time limit, the idea is that the program will run for a minute( from the the moment the program is run) and stops when 1 minute passes and displays a value

how can this be done? i done some research and looked at timer interrupts but i am having a hard time understanding if this is what i need to complete this task also i can't find any simple example programs that i could look at for reference

would this help me out if not how could this be done?( keep in mind that another program will be run during the 1 minute gap)

Offline jwatte

  • Supreme Robot
  • *****
  • Posts: 1,345
  • Helpful? 82
Re: how top make a program run for certain amount of time?
« Reply #1 on: May 04, 2014, 02:36:11 PM »
Assuming your program has a main loop (that runs over and over again) and some way of reading the current time, then it's super simple.

1) When starting assign the value of current time to some persistent variable (such as a global) as start time
2) Each time through the main loop of your program, read the current time, and subtract the start time
3) If the interval calculated is >= 60 seconds, and the process is running, stop the process and declare success

In Arduino-like code, it might look like:

Code: [Select]
unsigned long startTime;
bool running;

void setup() {
  // whatever
}

void loop() {
  if (running) {
    unsigned long now = millis();
    unsigned long elapsed = now - startTime;
    if (elapsed >= 60000) { // 60 seconds
      running = false;
      complete();
    }
  }
  else {
    if (should_start()) {
      running = true;
      startTime = millis();
    }
  }
}

You implement should_start() and complete().

}


Offline vipulan12Topic starter

  • Robot Overlord
  • ****
  • Posts: 181
  • Helpful? 0
Re: how top make a program run for certain amount of time?
« Reply #2 on: May 05, 2014, 05:33:52 PM »
Thanks, now my code works that way i want it to work

 


Get Your Ad Here