TIMER: Difference between revisions

From QB64 Phoenix Edition Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
 
(4 intermediate revisions by the same user not shown)
Line 1: Line 1:
The '''TIMER''' function returns the number of seconds past the previous midnite down to an accuracy of 1/18th of a second.
A '''TIMER''' statement enables, turns off or stops timer event trapping. QBasic only uses the base timer, but '''QB64''' can run many.




== QB Syntax ==
{{PageSyntax}}
::: seconds! = TIMER
;QuickBASIC:TIMER {ON|STOP|OFF}
;QB64:TIMER(''number%'') {ON|STOP|OFF|FREE}


== QB64 Syntax ==
::: seconds# = TIMER[(''accuracy!'')]


{{PageParameters}}
* ''number'' denotes a specific numbered timer event in '''QB64 only'''. QB64 can run many timer events at once including the base timer.
* TIMER ON enables event trapping of an [[ON TIMER(n)]] statement. While enabled, a check is made after every code statement to see if the specified time has elapsed and the ON TIMER [[GOSUB]] (or [[SUB]] in QB64) procedure is executed.
* TIMER STOP disables timer event trapping. When an event occurs while stopped, it is remembered. If timer events are turned back on later, any remembered events are immediately executed.
* TIMER OFF turns timer event trapping completely off and no subsequent events are remembered.


* TIMER return values range from 0 at midnight to 86399! A comparison value must stay within that range!
<center>'''QB64 only'''</center>
* [[INTEGER]] or [[LONG]] second values range from 0 at midnight to 86399 seconds each day.
* Get a TIMER number from [[_FREETIMER]] ONLY except when the base timer(no number or 0) is used. Use specific variables or an array to hold each event number value for later reference.
* QBasic can return [[SINGLE]] values down to about .04 or 1/18th (one tick) of a second accurately.
* If the TIMER number is omitted or 0, the TIMER used is the base timer.
* '''QB64''' can read [[DOUBLE]] ''accuracy'' down to 1 millisecond. Example: {{text|start# <nowiki>=</nowiki> TIMER(.001)|green}}
* Specific TIMER events can be enabled, suspended, turned off or freed using [[TIMER|TIMER(n)]] ON, STOP, OFF or FREE.
* Use [[DOUBLE]] variables for millisecond accuracy as [[SINGLE]] values are only accurate to 100ths of a second later in the day!
* TIMER(n) '''FREE''' clears a specific timer event when it is no longer needed. '''The base TIMER or TIMER(0) cannot be freed!'''
* TIMER loops should use a midnight adjustment to avoid non-ending loops in QBasic.
* TIMER can also be used for timing program Events. See [[ON TIMER(n)]] or the [[TIMER (statement)]]
* '''QB64''' can use a [[_DELAY]] down to .001(one millisecond) or [[_LIMIT]] loops per second. Both help limit program CPU usage.


 
<center>'''QB64 Timing Alternatives'''</center>
''Example 1:'' Delay SUB with a midnight correction for when TIMER returns to 0. '''QB64''' can use [[_DELAY]] for delays down to .001.
* The [[TIMER (function)]] can be used to find timed intervals down to 1 millisecond(.001) accuracy.
{{CodeStart}}
* The [[_DELAY]] statement can be used to delay program execution for intervals down to milliseconds.
{{Cl|DO...LOOP|DO}}
* [[_LIMIT]] can slow down loops to a specified number of frames per second. This can also alleviate a program's CPU usage.
  {{Cl|PRINT}} "Hello";
  Delay .5  'accuracy down to .05 seconds or 1/18th of a second in Qbasic
  {{Cl|PRINT}} "World!";
{{Cl|LOOP}} {{Cl|UNTIL}} {{Cl|INKEY$}} = {{Cl|CHR$}}(27) 'escape key exit
 
{{Cl|END}}
 
{{Cl|SUB}} Delay (dlay!)
start! = {{Cl|TIMER}}
{{Cl|DO...LOOP|DO}} {{Cl|WHILE}} start! + dlay! >= {{Cl|TIMER}}
  {{Cl|IF...THEN|IF}} start! > {{Cl|TIMER}} {{Cl|THEN}} start! = start! - 86400
{{Cl|LOOP}}
{{Cl|END SUB}}
{{CodeEnd}}
:''Explanation:'' When the delay time is added to the present TIMER value, it could be over the maximum number of 86399 seconds. So when TIMER becomes less than start it has reached midnight. The delay value then must be corrected by subtracting 86400.
 
 
''Example 2:'' Looping one TIMER tick of 1/18th of a second (ticks per second can be changed)
{{CodeStart}}
{{Cl|DEF SEG}} = 0 ' set to {{Cl|PEEK}} and {{Cl|POKE}} {{Cl|TIMER}} Ticks
{{Cl|DO...LOOP|DO}} ' main program loop
  ' program code
  {{Cl|POKE}} 1132, 0 ' zero Timer ticks
  {{Cl|DO...LOOP|DO}} ' delay loop
    x% = {{Cl|PEEK}}(1132)
    {{Cl|IF...THEN|IF}} x% <> px% {{Cl|THEN}} {{Cl|PRINT}} x%;
    px% = x%
  {{Cl|LOOP}} {{Cl|UNTIL}} x% >= 18 '18 ticks in one second
  {{Cl|PRINT}} "code " ' program code
{{Cl|LOOP}} {{Cl|UNTIL}} {{Cl|INKEY$}} = {{Cl|CHR$}}(27)
{{Cl|DEF SEG}} ' reset segment to default
 
{{Cl|END}}
{{CodeEnd}}
{{OutputStart}} 0  1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17  18 code
0  1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17  18 code
0  1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17  18 code
{{OutputEnd}}
:''Explanation:'' The [[POKE]] before the delay loop sets the tick count to 0. The [[PEEK]] count increases until the tick count returns 18 ticks and ends the loop. The same thing could be approximated by using a delay loop with: {{text|second! <nowiki>=</nowiki> '''TIMER''' + 1|green}}




''Example 3:'' Using a [[DOUBLE]] variable for [[TIMER]](.001) millisecond accuracy in '''QB64''' throughout the day.
{{PageExamples}}
''Example:'' How to update the time while [[PRINT|printing]] at the same time in a program.
{{CodeStart}}
{{CodeStart}}
ts! = TIMER(.001)     'single variable
  TIMER ON ' enable timer event trapping
td# = TIMER(.001)    'double variable
  LOCATE 4, 2 ' set the starting PRINT position
  {{Cl|ON TIMER(n)|ON TIMER}}(10) GOSUB Clock ' set procedure execution repeat time
  DO WHILE INKEY$ = "": PRINT "A"; : SLEEP 6: LOOP
  TIMER OFF
  {{Cl|SYSTEM}}


  PRINT "Single ="; ts!
  Clock:
  PRINT "Double ="; td#
  row = {{Cl|CSRLIN}} ' Save current print cursor row.
  col = {{Cl|POS|POS(0)}} ' Save current print cursor column.
  LOCATE 2, 37: PRINT {{Cl|TIME$}}; ' print current time at top of screen.
  LOCATE row, col ' return to last print cursor position
  {{Cl|RETURN}}
{{CodeEnd}}
{{CodeEnd}}
{{OutputStart}}
: NOTE: SLEEP will be interrupted in QBasic.
Single = 77073.09
Double = 77073.094
{{OutputEnd}}
:''Explanation:'' [[SINGLE]] variables will cut off the millisecond accuracy returned so [[DOUBLE]] variables should be used. TIMER values will also exceed [[INTEGER]] limits. When displaying TIMER values, use [[LONG]] for seconds and [[DOUBLE]] for milliseconds.




''See also:''
{{PageSeeAlso}}
* [[_DELAY]], [[_LIMIT]], [[SLEEP]]
* [[ON TIMER(n)]], [[TIMER (function)]]
* [[RANDOMIZE]], [[Scancodes]](example)
* [[_DELAY]], [[_LIMIT]]
* [[ON TIMER(n)]], [[TIMER (statement)]]




{{PageNavigation}}
{{PageNavigation}}

Latest revision as of 17:04, 24 February 2023

A TIMER statement enables, turns off or stops timer event trapping. QBasic only uses the base timer, but QB64 can run many.


Syntax

QuickBASIC
TIMER {ON|STOP|OFF}
QB64
TIMER(number%) {ON|STOP|OFF|FREE}


Parameters

  • number denotes a specific numbered timer event in QB64 only. QB64 can run many timer events at once including the base timer.
  • TIMER ON enables event trapping of an ON TIMER(n) statement. While enabled, a check is made after every code statement to see if the specified time has elapsed and the ON TIMER GOSUB (or SUB in QB64) procedure is executed.
  • TIMER STOP disables timer event trapping. When an event occurs while stopped, it is remembered. If timer events are turned back on later, any remembered events are immediately executed.
  • TIMER OFF turns timer event trapping completely off and no subsequent events are remembered.
QB64 only
  • Get a TIMER number from _FREETIMER ONLY except when the base timer(no number or 0) is used. Use specific variables or an array to hold each event number value for later reference.
  • If the TIMER number is omitted or 0, the TIMER used is the base timer.
  • Specific TIMER events can be enabled, suspended, turned off or freed using TIMER(n) ON, STOP, OFF or FREE.
  • TIMER(n) FREE clears a specific timer event when it is no longer needed. The base TIMER or TIMER(0) cannot be freed!
QB64 Timing Alternatives
  • The TIMER (function) can be used to find timed intervals down to 1 millisecond(.001) accuracy.
  • The _DELAY statement can be used to delay program execution for intervals down to milliseconds.
  • _LIMIT can slow down loops to a specified number of frames per second. This can also alleviate a program's CPU usage.


Examples

Example: How to update the time while printing at the same time in a program.

  TIMER ON ' enable timer event trapping
  LOCATE 4, 2 ' set the starting PRINT position
  ON TIMER(10) GOSUB Clock ' set procedure execution repeat time
  DO WHILE INKEY$ = "": PRINT "A"; : SLEEP 6: LOOP
  TIMER OFF
  SYSTEM

 Clock:
  row = CSRLIN ' Save current print cursor row.
  col = POS(0) ' Save current print cursor column.
  LOCATE 2, 37: PRINT TIME$; ' print current time at top of screen.
  LOCATE row, col ' return to last print cursor position
 RETURN
NOTE: SLEEP will be interrupted in QBasic.


See also



Navigation:
Main Page with Articles and Tutorials
Keyword Reference - Alphabetical
Keyword Reference - By usage
Report a broken link