GETTIMER/GETTIMERALL

Previous topic - Next topic

Worebu

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

 
Intel I5 9600 - RTX 4070, WaKü, 32GB RAM, 1x 512gb + 1x 1Gb NVRAM,  1x4TB + 1x1TB HD

Ian Price

#1
A simple timer example

Code (glbasic) Select

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

I came. I saw. I played.

Worebu

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
Intel I5 9600 - RTX 4070, WaKü, 32GB RAM, 1x 512gb + 1x 1Gb NVRAM,  1x4TB + 1x1TB HD

Ian Price

You  ould use an array of time events eg

Code (glbasic) Select

GLOBAL timer%[]

DIM timer[10]


Then use the "time" value to inc/dec values held in timer[0], timer[1] etc. etc.
I came. I saw. I played.

Slydog

I found this library code I used for my timing:

Code (glbasic) Select
// 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.
Code (glbasic) Select
// 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
My current project (WIP) :: TwistedMaze <<  [Updated: 2015-11-25]

Ian Price

You could also just do a simple

Code (glbasic) Select

IF MOD(gettimerall(),1000)=0 THEN INC sec
I came. I saw. I played.

Worebu

OK, danke. Das erklärt einiges.
Intel I5 9600 - RTX 4070, WaKü, 32GB RAM, 1x 512gb + 1x 1Gb NVRAM,  1x4TB + 1x1TB HD