April 7, 200917 yr Commercial Member 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, B. York FS2Crew Web Site / FS2Crew Facebook Page / FS2Crew Discord
April 7, 200917 yr 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 Gerry Howard
April 10, 200917 yr 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. Gerry Howard
April 24, 200917 yr Author Commercial Member Don't worry, I fixed it :( It was just a typo on my part (like usual :( ) B. York FS2Crew Web Site / FS2Crew Facebook Page / FS2Crew Discord
Create an account or sign in to comment