Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DarkBASIC Discussion / How To reset a timer

Author
Message
Mikada
21
Years of Service
User Offline
Joined: 28th Jan 2004
Location: Kentucky
Posted: 15th Feb 2004 00:09
Does anybody know how to reset a timer to 0 again after it reaches a certian value say like 150.

I have tried every variation I know, and nothing has worked for me.

The script I am using is this.
--------------------------------------
o_tome=timer() :
ink rgb(255,255,255),0 :

lowmode=0
do

tome=(timer()-o_tome)/1000 :
inc c
rem to display the timer this command
print "Master Time: "+str$(tome)
--------------------------------------------

It counts up ok and drives the commands I have made but it will not reset to zero. I deleted the reset info from the script because it didn't work.

Mike Cohoon
Mikada
21
Years of Service
User Offline
Joined: 28th Jan 2004
Location: Kentucky
Posted: 15th Feb 2004 01:52
OK I solved it myself the coding was very close to javascript

forget everything in my first post.

rem timer---------------------------

timer=1

rem this DO is in your regular program not another one------------

do

inc timer

if timer>150 then timer=1

rem end timer---------------------------------------

rem to display timer use this---------------------
set cursor 10,10
print "TIMER: "+str$(timer)

rem to control something---------------------------------

if timer=20 then position object WHATEVERNUMBER, landsize-100,10,landsize+90 :

REM I have a sync, loop, and return command at the bottom of the sub program to close it. You may not need any or may need some of them-----------------------------

Now I am working on dual inpedeant timers at the same time.

Mike Cohoon
Crappy Coder
21
Years of Service
User Offline
Joined: 16th Sep 2003
Location: Uk, Bath
Posted: 15th Feb 2004 02:06
hmm the problem with your code is... on faster machines the code will run quicker, e.g. the timer will reach 150 faster than on a slower machine... try this:




right theres the code, i havn't tested this, just typed it into this window... hope it works and helps
Mikada
21
Years of Service
User Offline
Joined: 28th Jan 2004
Location: Kentucky
Posted: 15th Feb 2004 02:46
Hey thanks C. Coder,

I over looked that in my rewrite, you have build in a more solid time regulator than I had at my first attempt. Do you have any knowledge about making 2 of those timers run in the same script,
I intend to have one regulate static characters, and the other regulate the matrix, like night and day and weather. It would be cool if one was running at a much longer value of time, maybe a third for other things, I have discovered naming them timer1 and then timer2 doesn't work in this language.

Thanks again for the great help,

Mike

Mike Cohoon
Crappy Coder
21
Years of Service
User Offline
Joined: 16th Sep 2003
Location: Uk, Bath
Posted: 15th Feb 2004 16:12
well what you could do is trun the variables into arrays...

Maxtimers = 2
Dim StartTime( MaxTimers )
Dim CurrentTime( MaxTimers )
Dim SecondsEllapsed( MaxTimers )
Dim WaitPeroid( MaxTimers )


then in the loop you could just loop through each on in a for next loop


for t = 1 to MaxTimers
CurrentTime( t ) = timer()
WaitPeriod( t ) = rnd(5) + 5
next t


rem start of loop
do
cls


rem loops through each timer
for t = 1 to MaxTimers

rem current timer
CurrentTime( t ) = timer()

rem converts millisec to secs and checks time ellapsed
SecondsEllapsed( t ) = (CurrentTime( t ) - Startime( t )) / 1000
if SecondsEllapsed( t ) > WaitPeriod( t )
`resets timer
StartTime( t ) = timer()
endif

rem prints time
text 10,t * 25,str$(SecondsEllapsed( t )) + " / " + str$(WaitPeriod( t ))


rem end of timer looping
next t


rem end of loop
sync
loop


right, i hope that works becuase yet again i have just typed it into here, if it doesn't work you should still be able to get the general idea..

hope that helps
Crappy Coder
21
Years of Service
User Offline
Joined: 16th Sep 2003
Location: Uk, Bath
Posted: 15th Feb 2004 16:14
oh hang on, you dont need a current time array... just a variable that you can put before the for loop..
Crappy Coder
21
Years of Service
User Offline
Joined: 16th Sep 2003
Location: Uk, Bath
Posted: 15th Feb 2004 16:15
and in actual fact you dont really need to make secondsellapsed an array either..
Mentor
22
Years of Service
User Offline
Joined: 27th Aug 2002
Location: United Kingdom
Posted: 15th Feb 2004 16:40
you can make several timers and allow them to count at different rates, just create a different index for each counter, eg:

four_seconds=timer()+4000
one_minute=timer()+60000
two_hours=timer()+7200000
one_minute_fifteen_seconds=timer()+75000

then in the main loop you just keep an eye on the time, eg:

rem these timers fire just once
do
if timer()>=one_minute_fifteen_seconds then print "one minute and fifteen seconds have passed.....times up!!!":one_minute_fifteen_seconds=0
if timer()>=two_hours then play_sunset_sequence():two_hours=0
loop

if you want the timer to start counting again after it has been triggered then just reset the value after you used it, rather than in the last case where we just set them to zero to prevent em being used again, eg:

rem these timers fire repeatedly
do
if timer()>=one_minute_fifteen_seconds then print "one minute and fifteen seconds have passed.....times up!!!":one_minute_and_fifteen_seconds=timer()+75000
if timer()>=two_hours then play_sunset_sequence():two_hours=timer()+7200000
loop

in this case you just set the variables back to the correct offset, and they will start working again and go off after the correct delay, you could also make some thing happen after a random amount of time, eg

startle=timer()+rnd(6000)
do
if timer()>=startle then print "BOO!":startle=timer()+rnd(6000)
loop

or use arrays to store larger amounts of variables, basicaly, you just calculate what value the timer will be at after the desired delay (1000 timer units = 1 second), then check the timer every loop to see if it has passed the desired value, if it has then you do whatever it was you planned and then either discard the timer or reset it to some new value, you don`t get something like the VB.net timer, where it runs in the background and fires when it reaches a value without you having to maintain it, with db you have to handle the timer directly, cheers.

Mentor.

System spec : Pentium 3.0Ghz, 512MB DDR, 2x160Gb HD (using icewave hd coolers ), DVD RW/CD RW (all modes), multimedia front panel, 6 way surround sound, ATI radeon 9800Pro 128mb.

Login to post a reply

Server time is: 2025-05-23 03:13:37
Your offset time is: 2025-05-23 03:13:37