TIMER: Difference between revisions

From QB64 Phoenix Edition Wiki
Jump to navigation Jump to search
m (QBasic capitalisation)
Tag: visualeditor
No edit summary
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.  
The '''TIMER''' function returns the number of seconds past the previous midnite down to an accuracy of 1/18th of a second.




Line 11: Line 11:
* TIMER return values range from 0 at midnight to 86399! A comparison value must stay within that range!
* TIMER return values range from 0 at midnight to 86399! A comparison value must stay within that range!
* [[INTEGER]] or [[LONG]] second values range from 0 at midnight to 86399 seconds each day.
* [[INTEGER]] or [[LONG]] second values range from 0 at midnight to 86399 seconds each day.
* QBasic can return [[SINGLE]] values down to about .04 or 1/18th (one tick) of a second accurately.  
* QBasic can return [[SINGLE]] values down to about .04 or 1/18th (one tick) of a second accurately.
* '''QB64''' can read [[DOUBLE]] ''accuracy'' down to 1 millisecond. Example: {{text|start# <nowiki>=</nowiki> TIMER(.001)|green}}  
* '''QB64''' can read [[DOUBLE]] ''accuracy'' down to 1 millisecond. Example: {{text|start# <nowiki>=</nowiki> TIMER(.001)|green}}
* Use [[DOUBLE]] variables for millisecond accuracy as [[SINGLE]] values are only accurate to 100ths of a second later in the day!
* Use [[DOUBLE]] variables for millisecond accuracy as [[SINGLE]] values are only accurate to 100ths of a second later in the day!
* TIMER loops should use a midnight adjustment to avoid non-ending loops in QBasic.
* TIMER loops should use a midnight adjustment to avoid non-ending loops in QBasic.
Line 20: Line 20:


''Example 1:'' Delay SUB with a midnight correction for when TIMER returns to 0. '''QB64''' can use [[_DELAY]] for delays down to .001.
''Example 1:'' Delay SUB with a midnight correction for when TIMER returns to 0. '''QB64''' can use [[_DELAY]] for delays down to .001.
{{CodeStart}} '' ''
{{CodeStart}}
{{Cl|DO...LOOP|DO}}
{{Cl|DO...LOOP|DO}}
   {{Cl|PRINT}} "Hello";
   {{Cl|PRINT}} "Hello";
Line 34: Line 34:
   {{Cl|IF...THEN|IF}} start! > {{Cl|TIMER}} {{Cl|THEN}} start! = start! - 86400
   {{Cl|IF...THEN|IF}} start! > {{Cl|TIMER}} {{Cl|THEN}} start! = start! - 86400
{{Cl|LOOP}}
{{Cl|LOOP}}
{{Cl|END SUB}} '' ''
{{Cl|END SUB}}
{{CodeEnd}}
{{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.
:''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.
Line 40: Line 40:


''Example 2:'' Looping one TIMER tick of 1/18th of a second (ticks per second can be changed)
''Example 2:'' Looping one TIMER tick of 1/18th of a second (ticks per second can be changed)
{{CodeStart}} '' ''
{{CodeStart}}
{{Cl|DEF SEG}} = 0 ' set to {{Cl|PEEK}} and {{Cl|POKE}} {{Cl|TIMER}} Ticks
{{Cl|DEF SEG}} = 0 ' set to {{Cl|PEEK}} and {{Cl|POKE}} {{Cl|TIMER}} Ticks
{{Cl|DO...LOOP|DO}} ' main program loop
{{Cl|DO...LOOP|DO}} ' main program loop
Line 54: Line 54:
{{Cl|DEF SEG}} ' reset segment to default
{{Cl|DEF SEG}} ' reset segment to default


{{Cl|END}} '' ''
{{Cl|END}}
{{CodeEnd}}
{{CodeEnd}}
{{OutputStart}} 0  1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17  18 code
{{OutputStart}} 0  1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17  18 code
Line 64: Line 64:


''Example 3:'' Using a [[DOUBLE]] variable for [[TIMER]](.001) millisecond accuracy in '''QB64''' throughout the day.
''Example 3:'' Using a [[DOUBLE]] variable for [[TIMER]](.001) millisecond accuracy in '''QB64''' throughout the day.
{{CodeStart}} '' ''
{{CodeStart}}
  ts! = TIMER(.001)    'single variable
  ts! = TIMER(.001)    'single variable
  td# = TIMER(.001)    'double variable
  td# = TIMER(.001)    'double variable


  PRINT "Single ="; ts!
  PRINT "Single ="; ts!
  PRINT "Double ="; td# '' ''
  PRINT "Double ="; td#
{{CodeEnd}}
{{CodeEnd}}
{{OutputStart}}
{{OutputStart}}
  Single = 77073.09
  Single = 77073.09
  Double = 77073.094  
  Double = 77073.094
{{OutputEnd}}
{{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.
:''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:''  
''See also:''
* [[_DELAY]], [[_LIMIT]], [[SLEEP]]  
* [[_DELAY]], [[_LIMIT]], [[SLEEP]]
* [[RANDOMIZE]], [[Scancodes]](example)
* [[RANDOMIZE]], [[Scancodes]](example)
* [[ON TIMER(n)]], [[TIMER (statement)]]
* [[ON TIMER(n)]], [[TIMER (statement)]]

Revision as of 02:52, 23 January 2023

The TIMER function returns the number of seconds past the previous midnite down to an accuracy of 1/18th of a second.


QB Syntax

seconds! = TIMER

QB64 Syntax

seconds# = TIMER[(accuracy!)]


  • TIMER return values range from 0 at midnight to 86399! A comparison value must stay within that range!
  • INTEGER or LONG second values range from 0 at midnight to 86399 seconds each day.
  • QBasic can return SINGLE values down to about .04 or 1/18th (one tick) of a second accurately.
  • QB64 can read DOUBLE accuracy down to 1 millisecond. Example: start# = TIMER(.001)
  • Use DOUBLE variables for millisecond accuracy as SINGLE values are only accurate to 100ths of a second later in the day!
  • 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.


Example 1: Delay SUB with a midnight correction for when TIMER returns to 0. QB64 can use _DELAY for delays down to .001.

DO
  PRINT "Hello";
  Delay .5  'accuracy down to .05 seconds or 1/18th of a second in Qbasic
  PRINT "World!";
LOOP UNTIL INKEY$ = CHR$(27) 'escape key exit

END

SUB Delay (dlay!)
start! = TIMER
DO WHILE start! + dlay! >= TIMER
  IF start! > TIMER THEN start! = start! - 86400
LOOP
END SUB
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)

DEF SEG = 0 ' set to PEEK and POKE TIMER Ticks
DO ' main program loop
  ' program code
  POKE 1132, 0 ' zero Timer ticks
  DO ' delay loop
    x% = PEEK(1132)
    IF x% <> px% THEN PRINT x%;
    px% = x%
  LOOP UNTIL x% >= 18 '18 ticks in one second
  PRINT "code " ' program code
LOOP UNTIL INKEY$ = CHR$(27)
DEF SEG ' reset segment to default

END
 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
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: second! = TIMER + 1


Example 3: Using a DOUBLE variable for TIMER(.001) millisecond accuracy in QB64 throughout the day.

 ts! = TIMER(.001)     'single variable
 td# = TIMER(.001)     'double variable

 PRINT "Single ="; ts!
 PRINT "Double ="; td#
 Single = 77073.09
 Double = 77073.094
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:



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