ROBOTC for MINDSTORMS - Timing  

An internal 32-bit clock is maintained by the NXT firmware. It counts in units of 1-millisecond. Four timers (T1, T2, T3 and T4) are built using this timing capability. These four timers can be individually reset to zero within a program. Theses timers are useful for measuring elapsed time of events.

16-bit integers are the default variable type for ROBOTC on the NXT. All of the timing functions operate with 16-bit signed variables. This does mean that caution should be exercised in programming to avoid overflow of timer values. A 16-bit signed variable has positive values in the range 0 to 32,767 and programs should periodically reset any timers that they use to prevent overflow.


wait1Msec(nMSec);
wait10Msec(nTenMSec);   
Program execution will wait for the specified number of clock units. Units can be in either 1-millisecond or 10-millisecond counts.The maximum interval that can be specified is either 32.767 seconds or 327.67 seconds depending on which function is used.An alternative, and far less efficient, method to perform a wait is to continually execute a tight code loop looking to see if a timer has reached the desired interval. It is best to use the wait functions to insert a programmed delay in a program because tasks that are waiting do not consume any CPU cycles. This makes the most number of CPU cycles available for other tasks.
Example:  (program example can be found at: "Something.c")

wait1Msec(1000);   //The program will wait for 1 second before moving on. (1ms * 1000) 
wait10Msec(1000);  //The program will wait for 10 seconds before moving on. (10ms * 1000)

ClearTimer(theTimer);
Timers start counting as soon as the NXT is powered on. A user's program should reset a timer before using it, so use this function to reset the value of the specified timer to zero.
Example:  (program example can be found at: "Something.c")

ClearTimer(T1);  //Resets the value of Timer "T1" back to zero seconds.

time1[]
time10[]
time100[]

These three arrays hold the current value of the respective timers. Each of the timer values can be retrieved in units of 1, 10 and 100 milliseconds depending on which array is used. For example, time1[T1] retrieves the value of timer T1 in units of 1-msec and time10[T1] retrieves the value using a 10-msec tick. And time100[T1] retrieves the value using 100-msec tick. Note that the arrays are “linked”. Setting time1[T1] = 0; will also reset the value of time10[T1] and time100[T1]. The value returned is a signed integer, so each array will meet its upper bounds at a value of 32,768 ticks< /FONT > .
Example:  (program example can be found at: "Something.c")

int valTime1, valTime10, valTime100;  //Create three integers to read the value of the timer.

valTime1 = time1[T1]; 
//Gets the value of Timer T1 in 1ms increments and stores it in a variable.
valTime10 = time10[T1];  //Gets the value of Timer T1 in 1ms increments and stores it in a variable.
valTime100 = time100[T1];  //Gets the value of Timer T1 in 1ms increments and stores it in a variable.

nSysTime
This variable contains the value of the lower 16-bits of the internal 1-msec clock. This variable is reset when NXT is first powered on.
Example:  (program example can be found at: "Something.c")

int varSysTime;  //Creates a variable
varSysTime = nSysTime; 
//Stores the current value of nSysTime to a variable

nPgmTime
This variable contains the value of the lower 16-bits of the internal 1-msec clock. This variable is reset when user program first starts running. This clock does not increment when the program is in a debugger "suspended" state which is useful during single step debugging as the clock does not increment.
Example:  (program example can be found at: "Something.c")

int varPgmTime;  //Creates a variable
varPgmTime = nPgmTime; 
//Stores the current value of nPgmTime to a variable

nClockMinutes
This read/write variable provides access to the NXT clock described above. The value ranges from 0 to 1439 before it wraps around back to 0. Note: there are 1440 minutes in 24 hours.
Example:  (program example can be found at: "Something.c")

int varClock;  //Creates a variable
varClock = nClockMinutes; 
//Stores the current value of the NXT Clock to a variable


Copyright © 2012 Robotics Academy/Robomatter - http://robotc.net/