EventTimeStamp

How to Record the Time Stamp of an Event

The Cricket has an internal elapsed-time primitive that can be used to keep track of real time.

The timer primitive is a free-running timer that indicates elapsed time. The primitive reports elapsed time in milliseconds (with an accuracy of four milliseconds). The maximum reading is 32768 milliseconds, after which the timer wraps back to 0. The resett primitive will reset the timer back to 0.

What we'd like to do is just record the elapsed time value, e.g. (triggered by switcha). Here is code that would record the first 100 events:

resett
repeat 100 [
  waituntil [switcha]
  record timer
]

The problem here is that the record primitive only stores the low byte of the value, so we would only be able to record up to 0.256 seconds with this method.

This can be resolved by recording both the high byte and low byte of the timer. The best way to do this is to copy the timer value in to temporary variable, and then recording the two bytes:

global [temp]

resett
repeat 100 [
  waituntil [switcha]
  settemp timer
  record high-byte temp
  record low-byte temp
]

This will work, but the timer only counts up to about 32 seconds (it is a 16-bit value in milliseconds), so it necessary to do a little more work to record elapsed time of longer periods.

In this example, we will time stamp events with a resolution of 1 second. We will use the when command and a new global named seconds to keep track of elapsed seconds. Here is the full program:

global [seconds temp]

to main
settime 0
resett
when [timer > 1000][setseconds seconds + 1]
repeat 100 [
  waituntil [switcha]
  settemp seconds
  record high-byte temp 
  record low-byte temp
]
end

When uploading the timestamp data, use the "2 data points per line" Then the high byte will be in the first column, and the low byte in the second column. In the third column of the spreadsheet, use the following formula to calculate the total elapsed time:

=(A1*256)+B1