@ device pic12F675, LP_OSC, wdt_on, mclr_on, protect_off
'Place the sensor is on GPIO.2
sensorPin VAR GPIO.2 : TrisIO.2=1
'Place a LED on port B.4
testLED VAR GPIO.1 : trisio.1 = 0 : testLED = 0
'this variable holds (buffers) the previous state of the sensor
tilt VAR BYTE
sensorPrev VAR BYTE
sensorPrev = tilt ' this is not used i would say they uesd it in a
'interrupt handier
'a counter- increases when the sensor moves
sensorCount VAR WORD
sensorCount = 0
'a couter - increases on every loop iteration
gapcount VAR WORD
gapcount = 0
'this variable determines how often we check for sensor changes.
gap VAR WORD
gap = 500
CMCON = $7
ANSEL = $0
'take a breather
PAUSE 1000
'beginning of main loop routine
main:
gapcount = gapcount + 1
'we use this if statement to activate the sensor routine -
'- once every {gap} cycles. In this code - once every 500 loops.
IF gapcount > gap THEN
'reset the counter
gapcount = 0
'if the sensor pin has changed since last checked
IF sensorPin <> sensorPrev THEN
'increment by 5 unless the counter is already very high
IF sensorCount < 50 THEN
sensorCount = sensorCount+5
ENDIF
'else if the sensor did not change states
ELSE
'slowly decrease the sensor unless its already at 0
IF sensorCount> 0 THEN
sensorCount = sensorCount-1
ENDIF
ENDIF
'if the sensor moved repeatedly - we have real motion!
IF sensorCount> 25 THEN
testLED = 1
ELSE
testLED = 0
ENDIF
'set the sensor buffer to current sensorPit, toward next check
sensorPrev = sensorPin
ENDIF
'loop again...
GOSUB main