' Demo program for the Sharp GP2D02 IR Range finding module ' By Chris Harriman NetMedia, Inc. 1999 Modified by Kwong Tat and F. Manning ' VCC Connected to +5 or pin 21 ' GND Connected to GND or pin 23 ' Vin Connected to pin 5 through a 1N4148 diode (diode band facing BX-24) ' Vout Connected to pin 6 ' Data is sent out via BasicX Com1 at 19200,n,8,1 Dim icom1(1 to 10) As byte Dim ocom1(1 to 30) As byte Dim data as byte '--------------------------------------------------------- Sub main() ' call putpin(21, 0) call openqueue(ocom1, 30) call openqueue(icom1, 10) call opencom(1, 19200, icom1, ocom1) call putpin(5, 1) call sleep(30) do call putpin(5, 0) if (getpin(6) = 1) then call read call inttostring(data, 10) call putqueuestr(ocom1, chr(13) & chr(10)) call putpin(5, 1) call sleep(30) else end if loop End Sub '-------------------------------------------------------- Sub read() Dim bit As byte Dim z as integer data = 0 bit = 128 For z = 1 To 8 call putpin(5, 1) call putpin(5, 0) if (getpin(6) = 1) then data = data or bit end if bit = bit \ 2 next call putpin(5, 1) End Sub '--------------------------------------------------------- Sub IntToString( _ ByVal i As Byte, _ ByVal base As Byte) Dim x(1 To 9) As Byte Dim j As Byte, NDigits As Byte For j = 1 To 16 NDigits = j x(CInt(j)) = HexConv(CByte(Abs(i Mod base))) i = i \ base If i = 0 Then Exit For End If Next For j = NDigits To 1 Step -1 Call PutQueue(ocom1, x(CInt(j)), 1) Next End Sub '--------------------------------------------------------- Function HexConv( _ ByVal b As Byte) As Byte If b > 9 Then HexConv = b + 55 Else HexConv = b + &H30 End If End Function '---------------------------------------------------------