Reputation: 604
I want to execute 2 different output at a certain time in seconds, one after another. And change the swap time using keyEvent. Here's the idea.
[Every 10 seconds]
1 2 3 4 5 6 7 8 9 (10) 11 12 13 14 15 16 17 18 19 (20) (→ second-from timer lib.)
Display Output 1 = 1-10 second
Display Output 2 = 11-20 second, (then Output 1 again, etc)
[Every 5 seconds]
1 2 3 4 (5) 6 7 8 9 (10) 11 12 13 14 (15) 16 17 18 19 (20)(→ second-from timer lib.)
Display Output 1 = 1-5 second
Display Output 2 = 6-10 second
Display Output 1 = 11-15 second, (then Output 2 again, etc)
I've been looking for question, like Timed C Program Loop and How to use seconds (time) in C program as a counter? and I only find those with timer and clock_t. I don't plan to use timer and clock since I already use a utilTimer library to return amount of seconds in realtime. Here's the my keyEvent code :
if (key == '1' ) {
delay=10;
}
if (key == '2' ) {
delay=5;
}
if (key == '3' ) {
delay=3;
}
if (key == '4' ) {
delay=1;
}
How do I implement the time loop ? As of now for the looping I'm using mod, but using mod the output is only executed once when the value meets mod. Also it's difficult to set the maximum value of the loop since I'm running realtime. So I'm still a little bit confused and haven't achieved my goal.
for (int count = 0; true; count++) {
if (count % delay == 0) {
//push output 1 every defined delay - in seconds
}
else {
//push another output
}
}
Any advice ?
[EDIT]
I'm running a simple C program on Mac. Basically what I want to do is I want to push an output in this case a string output and an image, so after several seconds the string and image will change, repeatedly. And I want to be able to control the changing frequency using key.
Upvotes: 1
Views: 3592
Reputation: 41200
You need to keep track of the last time you changed the image and string. Then you subtract the last time from the current time, and if the difference is more than the delay, you output a new image and string, then update the last time variable to the current time.
If you use unsigned values for the timers, you will not have trouble with rollover (which would be overflow for signed integers, and cause undefined behavior).
unsigned now; // from your timer
unsigned last_event; // the last time you sent stuff out
unsigned delay; // the time between output events
while (1)
{
if ((now - last_event) >= delay)
{
// send out your string and image
last_event = now;
}
else
{
// keep waiting, and do whatever else you want
// update now as appropriate from timer
}
}
My use of a while (1)
above is intended to convey the idea that the code inside the while
loop is executed frequently. This needn't, and probably wouldn't be a while
loop in practice. It would probably be
Upvotes: 1
Reputation: 73
If I understand the question right, you can use sleep
in a for loop. Its in unistd.h library.
#include unistd.h
for (int count = 0; true; count++) {
sleep(1000); //sleep for 1 second
if (count % 10 == 0) {
//every 10 seconds
}
if (count % 5 == 0) {
//every 5 seconds
}
}
Upvotes: 0