Archived

This topic is now archived and is closed to further replies.

byork

Countdown Timer

Recommended Posts

This is a piece of code (I forget where I found it from) that I've been using as part of my count-down timer:if ( start == 1 ) { chrono = CLOCK_TOTAL_SECONDSvar.var_value.n ; chronoevent = CLOCK_TOTAL_SECONDSvar.var_value.n ; start = 2 ; } if ( start == 2 ) { chronotime = CLOCK_TOTAL_SECONDSvar.var_value.n - chrono ; } else { chronotime = 0 ; } if ( start == 2 ) { chronotimeevent = CLOCK_TOTAL_SECONDSvar.var_value.n - chronoevent ; } else { chronotimeevent = 0 ; } if ( chronotime < 0 ) { chronotime = chronotime + 86400 ; } if ( chronotimeevent < 0 ) { chronoevent = chronoevent + 86400 ; } if ( start == 2 ) { vr = ( chronotime - fmod(chronotime,60) ) / 60 ; } if ( start == 2 ) { eventtime = ( chronotimeevent - fmod(chronotimeevent,60) ) / 60 ; } vr = fmod(vr,60) ; eventtime = fmod(eventtime,60) ; My code for count down timer starts at 45 minutes.The trouble is that when the timer reaches 0 minutes, eventtime will sometimes bounce back up to some random number.It's hard to reproduce. Sometimes it happens. Sometimes it doesn't.Anyone have any ideas?Or does anyone have another count down timer in C that does the same job but which is simpler?Cheers,

Share this post


Link to post
Share on other sites
Help AVSIM continue to serve you!
Please donate today!

This is a piece of code (I forget where I found it from) that I've been using as part of my count-down timer:if ( start == 1 ) { chrono = CLOCK_TOTAL_SECONDSvar.var_value.n ; chronoevent = CLOCK_TOTAL_SECONDSvar.var_value.n ; start = 2 ; } if ( start == 2 ) { chronotime = CLOCK_TOTAL_SECONDSvar.var_value.n - chrono ; } else { chronotime = 0 ; } if ( start == 2 ) { chronotimeevent = CLOCK_TOTAL_SECONDSvar.var_value.n - chronoevent ; } else { chronotimeevent = 0 ; } if ( chronotime < 0 ) { chronotime = chronotime + 86400 ; } if ( chronotimeevent < 0 ) { chronoevent = chronoevent + 86400 ; } if ( start == 2 ) { vr = ( chronotime - fmod(chronotime,60) ) / 60 ; } if ( start == 2 ) { eventtime = ( chronotimeevent - fmod(chronotimeevent,60) ) / 60 ; } vr = fmod(vr,60) ; eventtime = fmod(eventtime,60) ; My code for count down timer starts at 45 minutes.The trouble is that when the timer reaches 0 minutes, eventtime will sometimes bounce back up to some random number.It's hard to reproduce. Sometimes it happens. Sometimes it doesn't.Anyone have any ideas?Or does anyone have another count down timer in C that does the same job but which is simpler?Cheers,
What's the code that (1) sets the time (45 min) and (2) checks when that time's up

Share this post


Link to post
Share on other sites

Try this snippet for a GP timer (no warranty and usual disclaimers!)

/* Definitions */ bool Running; bool TimeUp; long StartTime; long CurrentTime; long ElapsedTime; long LimitTime;  /* If timer not running start it  */ if (!Running)   {   ElapsedTime = 0;   StartTime = time(NULL);   Running = true;   TimeUp = false;   }  /* If timer running update it */ if (Running)   {   ElapsedTime = time(NULL) - StartTime;    // Check if time up   if (ElapsedTime >= LimitTime)	 { 	 TimeUp = true;	 Running = false;	 }   else	 TimeUp = false;   }

Set the required time in LimitTime in seconds, set Running = false, and then check TimeUp to see if it has elapsed. The timer is stopped when time's up so needs restarting.time(NULL) returns the elapsed time in seconds from 00:00:00 GMT, January 1, 1970 so there's no need to worry abiut day end. I imagine that FS time is derived from this.

Share this post


Link to post
Share on other sites

Don't worry, I fixed it :( It was just a typo on my part (like usual :( )

Share this post


Link to post
Share on other sites