Shop OBEX P1 Docs P2 Docs Learn Events
Motion detection with Ping — Parallax Forums

Motion detection with Ping

lardomlardom Posts: 1,659
edited 2013-04-16 04:53 in Accessories
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

  • davejamesdavejames Posts: 4,047
    edited 2013-04-15 23:43
    ...I'm guessing this is done in Spin?
  • lardomlardom Posts: 1,659
    edited 2013-04-16 04:53
    Yes, it's Spin. Thanks for bringing this up. There was an error in the code that I missed when I posted it. The highlighted expression is the correction.
    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
    
Sign In or Register to comment.