{LET} wordvariable = TIME

{LET} TIME = expression

if TIME > value then


Use the elapsed time counter variable.

The M2 series have an internal elapsed time counter. This is a word variable called 'time' which increments once per second (at 4 or 16MHz operation). The time variable is designed as an approximate elapsed seconds counter, it is not a real time clock. This seconds counter starts automatically on a power-on reset, but can also be enabled/disabled by the disabletime / enabletime commands.

Note that the background internal timer used to count seconds is also used by several other commands such as servo. Therefore when re-enabling the time variable incrementing function with enabletime (after a disabletime command has previously been used) the first tick could happen any time during that initial one second time period. When the time function is disabled the time variable may be altered safely with a command such as 'let time = 0'.

The 'time' variable will not increment while a sleep command is executing.

Effect of increased clock speed

The time function will increment once every second at 4 MHz or 16 MHz. At 2 MHz and 8 MHz the interval will be 2s, at 32 MHz the time interval will be 0.5s. The number of seconds per time variable increment is shown below -

32 MHz 0.5s  
16 MHz 1s  
8 MHz 2s  
4 MHz - 1s
2 MHz - 2s
1 MHz 16s  
500 kHz 32s  
250 kHz 64s  
125 kHz - 32s
31 kHz - 128s
Applies To:
08M2, 14M2, 18M2, 20M2
See Also:
Related Create:

    Time example

    Activates an LED after 10 seconds

    Code Example:
    let w0 = time + 10
      if time > w0 then 
        high B.0
      end if
    Copy Code Submit an Example

    Flashing a LED

    Flashes a LED every two seconds

    Code Example:
    'flashes every 0.5 seconds
    	setfreq m32
    	w0 = time // 2
    	if w0 = 0 then
    	        high b.0
    	        low b.0
    	end if
    	goto main
    Copy Code Submit an Example

    Submit Your Own Code!

    You must be logged in to submit code examples. Login now.


    • Posted by racingtomars

      I think I'm misunderstanding the code; but it appears to me (mathematically) that the variable 'time' could never be larger than the variable 'w0' because 'w0' is a sum that is always 10 larger than time. Could you clarify please?

    • Posted by rossko57

      w0 is not a sum that is _always_ larger than 'time'. A "snapshot" is taken at the beginning, of 'time' plus 10. Thereafter, 'time' magically increments by itself - the whole purpose of this special variable - and will eventually be larger than w0

    • Posted by racingtomars

      Thanks for the explanation rossko57, your "snapshot" phrase makes this really clear!

      Cheers :-)

    • Login to leave a comment.