EventsPerInterval

How to Count a Number of Events Per Time Interval

This explain how to use the Cricket to count a number of events per a fixed time interval. This can be used to record a number of steps per minute (e.g., making a pedometer with an inclination sensor that measures steps).

The code assumes the step-measuring sensor is plugged into the sensora port. It will record 10 minutes of activity, sampling your step-count every 10 seconds. (This is a total of 60 samples, one per 10 seconds.)

global [steps]

to main
when [switcha][setsteps steps + 1]
repeat 60 [
  setsteps 0
  wait 100
  record steps]
end

The method is to use the Cricket's when statement which can trigger code that runs in the background. The when statement is used in conjunction with a global variable to accumulate the step count. During the wait 100 statement, the when command is active and accumulates the step count.

You can add a beep statement in the body of the when to help hear how the counts are being recorded.