Hallo Leute,
kann mir jemand sagen wie ich GETTIMER/GETTIMERALL mal korrekt anwende? Ich möcht z.B. eine "LED" alle 10 sek. zum aufblinken bringen, oder einen Text alle 5 sek. aufleuchten lassen. Ich habe Beispiele gefunden ... aber nix einfaches was mir den Befehl erschliesst. Bei VB geht das ziemlich einfach, TIMER einfügen, intervall einsetzen, fertig. Aber wie ich das bei GLB gesehen habe muss ich ja immer rumrechnen. Vorallem weiss ich nicht wie, damit ich alle 10sek. eine FUNCTION/SUB anspringen kann, oder dergleichen. Hat jemand ein einfaches Beispiel, plz?
thx, Worebu
A simple timer example
LOCAL time%=GETTIMERALL()
LOCAL sec%
LOCAL mins%
WHILE TRUE
// If 1000 ticks passed, then increase seconds
IF GETTIMERALL()>time+1000
time=GETTIMERALL()
INC sec
ENDIF
// Increase minutes
IF sec>59
INC mins
sec=0
ENDIF
PRINT "MIN "+mins+" : SEC "+sec,20,20
// Show message every 10 seconds
IF MOD(sec,10)=0 THEN PRINT "10 SECONDS",20,40
SHOWSCREEN
WEND
OK, danke , das leuchtet ein, aber wenn ich 10 oder mehr TIMER Events habe muss ich das also 10x machen? Ist das nicht umständlich? oder gibt es für mehrere Timer Events eine einfachere möglichkeit?
thx, Worebu
You ould use an array of time events eg
GLOBAL timer%[]
DIM timer[10]
Then use the "time" value to inc/dec values held in timer[0], timer[1] etc. etc.
I found this library code I used for my timing:
// Countdown Timer
// Pass this function an integer that holds the current timer value.
// Timer variable is updated byref
// Returns TRUE if the timer is still running
// Returns FALSE if the timer has expired
// EXAMPLE:
// LOCAL wait% = 1000
// LOCAL delay% = 2000
// IF Timer_Down(wait) = FALSE
// . . . 'wait' timer has expired
// wait = 1000 // reset timer
// ENDIF
// IF Timer_Down(delay) = FALSE
// . . . 'delay' timer has expired
// delay = 1000 // reset timer
// ENDIF
FUNCTION Timer_Down%: BYREF time%
DEC time, GETTIMER()
IF time <= 0
time = 0
RETURN FALSE
ELSE
RETURN TRUE
ENDIF
ENDFUNCTION
Or this one may be more useful for your needs:
It auto resets the timer for you also.
// Same as 'Timer_Down()' except you pass the 'timer_length' as it automatically resets the timer when expired
// EXAMPLE:
// LOCAL wait% = 1000
// LOCAL delay% = 2000
// IF Timer_Down(wait, 1000) = FALSE
// . . . 'wait' timer has expired
// ENDIF
// IF Timer_Down(delay, 2000) = FALSE
// . . . 'delay' timer has expired
// ENDIF
FUNCTION Timer_Repeat%: BYREF time%, timer_length%
DEC time, GETTIMER()
IF time <= 0
time = timer_length // Reset timer
//time = timer_length-time // Reset timer (Optional to above to prevent timer drifting)
RETURN TRUE // Timer has ended
ELSE
RETURN FALSE // Timer still executing
ENDIF
ENDFUNCTION
You could also just do a simple
IF MOD(gettimerall(),1000)=0 THEN INC sec
OK, danke. Das erklärt einiges.