Author Topic: GETTIMER/GETTIMERALL  (Read 1384 times)

Offline Worebu

  • Mr. Polyvector
  • ***
  • Posts: 132
  • Moin, Moin
    • View Profile
GETTIMER/GETTIMERALL
« on: 2016-Feb-22 »
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

 
PC: Intel I7 3770K, 32GB DDR3, GTX1060 6GB,  480GB SSD, 1x2TB, 1x1TB, 2x ext. 1TB USB3.0, 1x ext. 2TB USB3.0, W10 x64 Pro, WaKü

Offline Ian Price

  • Administrator
  • Prof. Inline
  • *******
  • Posts: 4094
  • On the shoulders of giants.
    • View Profile
    • My Apps
Re: GETTIMER/GETTIMERALL
« Reply #1 on: 2016-Feb-23 »
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
 
« Last Edit: 2016-Feb-23 by Ian Price »
I came. I saw. I played.

Offline Worebu

  • Mr. Polyvector
  • ***
  • Posts: 132
  • Moin, Moin
    • View Profile
Re: GETTIMER/GETTIMERALL
« Reply #2 on: 2016-Feb-23 »
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
PC: Intel I7 3770K, 32GB DDR3, GTX1060 6GB,  480GB SSD, 1x2TB, 1x1TB, 2x ext. 1TB USB3.0, 1x ext. 2TB USB3.0, W10 x64 Pro, WaKü

Offline Ian Price

  • Administrator
  • Prof. Inline
  • *******
  • Posts: 4094
  • On the shoulders of giants.
    • View Profile
    • My Apps
Re: GETTIMER/GETTIMERALL
« Reply #3 on: 2016-Feb-23 »
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.

Offline Slydog

  • Prof. Inline
  • *****
  • Posts: 924
  • KodeSource
    • View Profile
    • KodeSource
Re: GETTIMER/GETTIMERALL
« Reply #4 on: 2016-Feb-23 »
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]

Offline Ian Price

  • Administrator
  • Prof. Inline
  • *******
  • Posts: 4094
  • On the shoulders of giants.
    • View Profile
    • My Apps
Re: GETTIMER/GETTIMERALL
« Reply #5 on: 2016-Feb-23 »
You could also just do a simple

Code: GLBasic [Select]
IF MOD(gettimerall(),1000)=0 THEN INC sec
 
I came. I saw. I played.

Offline Worebu

  • Mr. Polyvector
  • ***
  • Posts: 132
  • Moin, Moin
    • View Profile
Re: GETTIMER/GETTIMERALL
« Reply #6 on: 2016-Feb-24 »
OK, danke. Das erklärt einiges.
PC: Intel I7 3770K, 32GB DDR3, GTX1060 6GB,  480GB SSD, 1x2TB, 1x1TB, 2x ext. 1TB USB3.0, 1x ext. 2TB USB3.0, W10 x64 Pro, WaKü