Jump to content
Sign in to follow this  
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
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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

  • Tom Allensworth,
    Founder of AVSIM Online


  • Flight Simulation's Premier Resource!

    AVSIM is a free service to the flight simulation community. AVSIM is staffed completely by volunteers and all funds donated to AVSIM go directly back to supporting the community. Your donation here helps to pay our bandwidth costs, emergency funding, and other general costs that crop up from time to time. Thank you for your support!

    Click here for more information and to see all donations year to date.
×
×
  • Create New...