Reputation: 1497
I am trying to catch how fast a number is being rotated.
I have a program where the counter starts at 0.
Every time the user clicks the button "add 1". It increments the counter by 1.
The counter maxes at 255. after 255 it goes back to 0.
Now I want to display how many seconds it took from 0 to increment to max and back to 0. Time for the complete revolution. Its gonna differ on how fast the user has clicked the button.
What I need to know is how to use a timer for this? The program is complete just the timer between the complete revolution of the numbers I need to code.
I'm using
#include <time.h> //* clock_t, clock, CLOCKS_PER_SEC
Please advice. Thank you.
int counter;
counter = pkt[0];
cout << endl;
cout << counter << endl;
if(counter == 1)
{
cout << "revolution" << endl;
}
Upvotes: 1
Views: 715
Reputation: 3402
#include <ctime>
time_t start = time(NULL);
will give you the number of seconds that have passed since 00:00 hours, Jan 1, 1970 UTC. If you put that at the start of the code you want to keep track of, you simply need to get the time again when you want to calculate how long it took to perform a complete revolution. Then get the difference. difftime
can help you get the difference. Don't think of it as a timer that's running really. You're just getting the time before and after the revolution and finding the difference.
Upvotes: 0
Reputation: 3416
If you can use C++11, <chrono>
could be a good choice.
It contains:
Durations
They measure time spans, like: one minute, two hours, or ten milliseconds. In this library, they are represented with objects of the duration class template, that couples a count representation and a period precision (e.g., ten milliseconds has ten as count representation and milliseconds as period precision).
Time points
A reference to a specific point in time, like one's birthday, today's dawn, or when the next train passes. In this library, objects of the time_point class template express this by using a duration relative to an epoch (which is a fixed point in time common to all time_point objects using the same clock).
Clocks
A framework that relates a time point to real physical time. The library provides at least three clocks that provide means to express the current time as a time_point: system_clock, steady_clock and high_resolution_clock.
Upvotes: 3