How to Implement a Timer in C++ for Repeating Code Blocks

  • Comp Sci
  • Thread starter jaydnul
  • Start date
  • Tags
    C++ Timer
In summary, the main issue in the conversation is that the code is getting stuck on the first chunk and looping forever, preventing the other chunks from running. The solution proposed is to use a main loop to iterate through each chunk and use a sleep function to ensure that each chunk runs for a specific amount of time before moving on. The use of the C++11 and chrono library is recommended for better accuracy.
  • #1
jaydnul
558
15
Basically within my whole code is 4 major chunks of code that each loop repeatedly. The obvious problem is when I run it, it will get stuck on the first chunk and loop forever, therefore never moving on the next chunk. What I need is to have each section run for 10 seconds and then move on. Is there a simple function that could do this?

Thanks a bunch!
 
Physics news on Phys.org
  • #2
why can't you restructure your code with a main loop so that you do one iteration for each of the four chunks?

or does the subsequent chunks depend on the first chunk to be completed?

As an example

Code:
for(int i=0; i<max; i++) {
   dochunk1(i)
   dochunk2(i)
    ...
}
 
  • #3
I need more than one iteration from each chunk at a time. They are blinking LEDs and need to be blinking for a certain amount of time before moving on to the next LEDs (simplified but you get the idea).
 
  • #4
How is your program determining how long 10 sec. is? What OS are you using? The reason I ask is that the OS might have some APIs that you can use to do this, which would probably be better than you having an empty loop run for a set amount of time that you determine by trial and error.
 
  • #5
look for a sleep function

so you set your LEDs then sleep then set them differently then sleep ...
 
  • #6
Hmm. Maybe not what people suggested. Would do the following (and maybe this is what you want):
Code:
unsigned char cur_loop = 0; // an index.
unsigned char loop_count = 4; // you have four loops.
long interval = 10000 // 10000, assuming milliseconds, stand for 10 seconds per loop.
long time = current_system_time(); // use whatever type best suits your needs (or the underlying implementation).
while (true) {
  if (current_system_time() - time > interval) {
    cur_loop = (cur_loop + 1) % loop_count;
  }
  switch (cur_loop) {
     // cases and their blocks here (the magic happens here!)
  }
}
// Be warned of the overhead of current_system_time()!
Copying and pasting this will not do much. I suggest C++11 and the chrono library. Read the documentation. Good luck.
If you need to repeat for n seconds then go ahead a sleep function won't work without multithreading (noob here, so I may be wrong).
 

1. How do I start creating a timer in C++?

The first step to creating a timer in C++ is to include the ctime library in your program. This library contains functions that allow you to work with time and date values.

2. How do I set the duration of the timer?

To set the duration of the timer, you can use the clock() function from the ctime library. This function returns the number of clock ticks since the program was launched. You can use this value to calculate the duration of the timer in seconds.

3. How do I display the timer in real-time?

The most common way to display the timer in real-time is by using a loop. Inside the loop, you can use the clock() function to get the current time and calculate the elapsed time since the timer started. This value can then be displayed using cout or any other output method.

4. How can I add a countdown feature to the timer?

To add a countdown feature to the timer, you can use the sleep() function from the ctime library. This function pauses the program for a specified number of seconds. You can use this function inside a loop to create a countdown effect.

5. How can I make the timer more accurate?

The accuracy of the timer depends on the clock resolution of your system. To increase the accuracy, you can use a higher precision clock, such as high_resolution_clock from the chrono library. This clock has a higher resolution and can provide more accurate time measurements.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
8
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
3K
Replies
6
Views
980
  • Engineering and Comp Sci Homework Help
Replies
11
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
6
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
15
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
Back
Top