Shop OBEX P1 Docs P2 Docs Learn Events
quick help writing a real short program for a sharp 120 object detector — Parallax Forums

quick help writing a real short program for a sharp 120 object detector

lobolobo Posts: 100
edited 2005-10-22 04:48 in BASIC Stamp
the sharp is on pin 15 and using a lm339, basic stamp 2 on the boe-bot and servos are connected to port 12,13. i would like the debug to remain just so when boe is connected i can see or verify the the sensor are working. what can i do to cause the servo to react when the sensor sees something? I just need something quick and basic for i am a beginner. thank you.

according to the 123 project for the evil genius

'IR Proximity Dectector-Monitor Sharp GD2D120 Proximity Detector Output
'{$STAMP BS2}
' {$PBASIC 2.5}
' IR Proximity Detector - Monitor Sharp GD2D120 Proximity Detector OUTPUT

'Variables
Whisker PIN 15
Flag··· VAR Bit

' Initialization
·Flag=0

· DO
··· IF (Whisker = 0 ) AND (Flag = 0) THEN
····· Flag = 1: DEBUG "Ouch, collision",
······
··CR

····· ELSE

··· ' When Whisker Released, Reset
······· IF (Whisker = 1) THEN Flag = 0
······· ENDIF

······· LOOP

·

Comments

  • metron9metron9 Posts: 1,100
    edited 2005-10-22 04:32
    Here ya go, When Wiska is triggered the servo moves back and forth , when it is not triggered it slowly moves back to a home position.

    'IR Proximity Dectector-Monitor Sharp GD2D120 Proximity Detector Output
    '{$STAMP BS2}
    ' {$PBASIC 2.5}
    ' IR Proximity Detector - Monitor Sharp GD2D120 Proximity Detector OUTPUT
    
    'Variables
    Whisker PIN 15
    Flag    VAR Bit
    position VAR Word
    direction VAR Byte  ' 0=home, 1=count up, 2= count down
    pHOME VAR Word      ' Home position of servo
    pMAX VAR Word       ' Maximum value of position
    pMIN VAR Word       ' Minimum value of position
    
    ' Initialization
    Flag=0
    phome=750
    pmax=1000
    pmin=500
    position=phome
    direction=0
    
    
    DO
    
     IF flag=1 THEN                             'Whisker is on
      IF direction=1 THEN
       position=position+10                      'Count up
       IF position>pmax THEN                     'Check boundrys
        position=pmax
        direction=2                              'Reverse direction
       ENDIF
      ENDIF
      IF direction=2 THEN
       position=position-10                      'Count Down
       IF position<pmin THEN                     'Check boundry
        position=pmin
        direction=1                              'Reverse direction
       ENDIF
      ENDIF
     ENDIF
    
     IF direction=0 THEN                             'If wisker is off
      IF position>phome THEN position=position-1     'Return slowly to home position
      IF position<phome THEN position=position+1
     ENDIF
    
     DEBUG DEC position , CR
     PULSOUT 14,position                              'Talk to servo
     PAUSE 20                                         'Alternet code if you use a clock could be here
                                                      'Sending a pulsout only every 20ms instead of waiting
                                                      'Would allow more processing to be done
    
     IF (Whisker = 0 ) AND (Flag = 0) THEN
      Flag = 1
      DEBUG "Ouch, collision",CR
      direction=1
     ELSE
    
      IF (Whisker = 1) THEN         ' When Whisker Released, Reset
       Flag = 0
       direction=0
      ENDIF
     ENDIF
    LOOP
    
    
  • lobolobo Posts: 100
    edited 2005-10-22 04:48
    Thank you very much God Bless
Sign In or Register to comment.