' {$STAMP BS1}

'TEST CODE FOR SHARP GP2D02 IR RANGE SENSOR
'
'

DIR6=1      ' Assume IRs Vin, green wire, is on Pin 6 of Stamp 1
DIR4=0      ' Assume IRs Vout, yellow wire, is on Pin 4 of Stamp 1


loop:

   B3 = 0     'Initialize range variable to 0
   HIGH 6     'These three lines tell IR sensor to take a reading
   PAUSE 20        ' First rest, in case there just took a reading
   LOW 6           ' Now bring Vin low for at 70 milliseconds
   PAUSE 70

   FOR B2=0 TO 7     'Now clock out the 8 serial bits of range info
                     'Most significant bit comes first
      PULSOUT 6,15   'Ask for next bit
      B3=2*B3         'Read the bit and use Horner's rule to decode
      IF PIN4 = 0 THEN skip      ' to an integer between 0 and 255 inclusive
      B3 =B3 + 1
      skip:
   NEXT

   DEBUG "DISTANCE= ",B3,CR       'Print range value to screen

GOTO loop                        'Repeat; get new range value