Motion detection with Ping
lardom
Posts: 1,659
I'm working on a 'just for fun' autonomous mobile robot project and needed a way to know when movement started and stopped so I could start another 180° scan. I hope I'm not the only one who finds this code useful. Please tell me what you think.
'Larry McGrew, author 4/10/13
'The led should stay on as long as there is movement in front of the sensor
OBJ
echo : "Ping"
PUB Activity_Test | j, A, B
dira[17]~~ 'LED
repeat 8 'stabilize ping
B := echo.Inches(27)
waitcnt(clkfreq/10+cnt)
repeat 'motion starts
A := B := echo.Inches(27)
repeat until A <> B
B := echo.Inches(27)
waitcnt(clkfreq/10+cnt)
outa[17]~~ 'LED
repeat j from 1 to 6
A := echo.Millimeters(27) ' Millimeters Centimeters
waitcnt(clkfreq/40+cnt)
B := echo.Millimeters(27)
waitcnt(clkfreq/40+cnt)
if A <> B
j := j-1
outa[17]~ 'motion stops

Comments
OBJ echo : "Ping" PUB Activity_Test | j, A, B dira[17]~~ 'LED repeat 8 'stabilize ping B := echo.Inches(27) waitcnt(clkfreq/10+cnt) repeat 'motion starts A := B := echo.Inches(27) repeat until A <> B B := echo.Inches(27) waitcnt(clkfreq/10+cnt) outa[17]~~ 'LED repeat j from 1 to 6 A := echo.Millimeters(27) ' Millimeters Centimeters waitcnt(clkfreq/40+cnt) B := echo.Millimeters(27) waitcnt(clkfreq/40+cnt) if A <> B [COLOR="#FF0000"]j := 1[/COLOR] outa[17]~ 'motion stops