Detect events within time interval

boriz

Senior Member
Questions about this sort of thing arise from time to time, so I've written this generalized routine that should work for most people. In this example, it detects when 3 events occur within any 10 seconds. Change the Events constant and the Interval constant as required. Uses the M2 parts system variable Timer. Events can be anything that cause a resettable logic level change on a pin. A button for example, but would also work with a sound detector, vibration, temperature etc..

Code:
'Detect events within time interval
'Boris Burke JUN 2013
'
'Detects when a set number of events happen within a set amount of time
'Requires one byte variable
'Works only on M2 parts

symbol events=3   'number of events to detect
symbol interval=10   'number of interval seconds, must be 1 or more
symbol event_count=b0   'stores current number of valid events

do
	if pin1=1 then   'detect event on pin1
		do: loop until pin1=0   'wait for event reset
		inc event_count   'increment event count
		if time>=interval then   'test if event is outside time interval
			time=0   'reset timer
			event_count=1   'reset event count
		endif
	endif
loop until event_count=events   'continue until correct number of events occur within time
 

okasional

New Member
I'm just starting with PICAXE (waiting on kit and chips to be shipped) and this fits right in with my first project. So much new stuff to read, I'm overwhelmed. I am trying to write a snippet of code you might call 'count events with timeout'. I am designing a gadget that has a manually operated switch for inputing a calibration parameter, or changing it, at any time while running. The switch is being monitored, perhaps every tenth of a second in a main loop. If the switch is actuated, the main loop is interrupted for a 'calibrate' subroutine wherein a variable is set to a value of "1" and a 3.5-second timer is started. Each time the switch is actuated the variable is incremented and the timer is reset to zero. If no switch actuation occurs within 3.5 seconds, calibration is complete, the system uses the calibration value stored in the variable, and operation returns to the main loop.

The use of the terms 'time' and 'interval' in Boris' snippet has me stumped, as in 'if time>=interval then . . .'

The constant 'interval' is given a value of simply '10'. How does the program know that means seconds and not milliseconds, or days? I suspect 'time' must be some sort of universal seconds counter, but where can I find it defined or explained? Sorry for the dumb question, but that's what happens when you let us yanks join your forum.
 

BeanieBots

Moderator
Welcome to the forum!

It's all explained in the manuals, worth having a good read through all the commands.
The M2 series have an internal elapsed time counter. This is a word variable called 'time' which increments once per second. This seconds counter starts automatically on a power-on reset, but can also be enabled/disabled by the disabletime / enabletime commands.
There are many other commands (eg pulsin) which rely on units of time which will depend on what clock speed you are running at so it's not easy to give you a generic answer.
There are also other timers available on other chip types which have different 'intervals'.
 
Top