Connecting Multiple SRF08 Sonar Modules to the OOPic 

Introduction
The SRF08 modules use the I2C bus for communication. This example shows how to connect two SRF08's to the OOPic, however it is expandable to up 16 SRF08's on the I2C bus. The SCL (clock) and SDA (data) lines are connected to the OOPic LSCL and LSDA - the OOPic Local I2C bus. The OOPic is able to power two SRF08's. If you want to use more then you will need to use a separate 5v supply or replace the 5v regulator on the OOPic with a higher current one. There are Instructions on the OOPic website to do this. Do not connect the SRF08's to the OOPic connectors labeled I2C, these are the OOPic network connectors and are not used by the oI2C object.

Circuit Schematic for connecting two SRF08 Sonar Modules to the OOPic

Changing the SRF08 I2C Address
Before you can use the SRF08's you will need to re-program their I2C addresses from the default address of 0xE0 they are supplied with. The simple program below will do this. Make sure you only have one SRF08 connected when you do this. You only have to change the 1st line of the program below to the address you want. For example if you want the SRF08 to be at address 0xF2, then change the 1st line to read;
Const NewAddress = 242
Now download the program to the OOPic, you will see rapid brief flashes on the Red Led on the SRF08 indicating that the change of address was successful. It is wise to make a note of the new address on the SRF08 itself. 
To use the example code described later on this page, set one SRF08 to address 0xE0 and the other to 0xE2. The following program can be downloaded here.

Const NewAddress = 224            ' CHANGE THIS NUMBER FOR THE REQUIRED ADDRESS
                                  ' See table below.

' This program can be used to set the I2C address of the SRF08 Sonar Module
' Valid address are 0xE0 to 0xFE
' The following table shows the decimal number to set the .node to, for any
' desired hex address.
' E0 -> 224
' E2 -> 226
' E4 -> 228
' E6 -> 230
' E8 -> 232
' EA -> 234
' EC -> 236
' EE -> 238
' F0 -> 240
' F2 -> 242
' F4 -> 244
' F6 -> 246
' F8 -> 248
' FA -> 250
' FC -> 252
' FE -> 254
' Important! You must only have one SRF08 connected up when you set the I2C
' address. You don't want two sonars at the same address do you?
' You might want to write the address on the sonar after setting it, so
' that you know which is which afterwards.
'
' When you run this software, it will change the I2C address and then use
' that address to continuously "ping" the sonar. You should see the LED
' giving rapid, brief flashes.

Dim Srf08 As New oI2C              'Create the SRF08 object
Dim Ver as new oByte

Sub main()
SRF08.Node = 0                     ' Use general broadcast address, because we
                                   ' don't know where SRF08 currently is.
SRF08.Mode = cv10bit               ' I2C mode is 10-Bit Addressing.
SRF08.NoInc = 1                    ' Don't increment
SRF08.Width = cv8bit               ' 1 byte wide transfer
SRF08.Location = 0                 ' Command register
SRF08 = 160                        ' Hex A0
SRF08 = 170                        ' Hex AA
SRF08 = 165                        ' Hex A5
SRF08 = NewAddress                 ' Selected Address, as defined above

' Because of the way the OOPIC works, it requires addresses to be right
' shifted by 1. So address 0xE0 is 112, which is the Decimal of
' Hex address 0xE0 shifted right by 1 (or divided by 2 - same thing)
SRF08.Node = NewAddress/2

Do
  SRF08 = 81                        ' One of the Ranging Commands
  Do
    Ver = SRF08
  Loop While Ver=255                ' Wait for ranging to complete
Loop                               ' Keep pinging forever

End Sub


Displaying Light Sensor and Range readings on the LCD
 Now that you have your SRF08's re-programmed to their new I2C addresses (0xE0 and 0xE2) the following sample code will display the light sensor reading and the 1st and 2nd ranges, for each SRF08 on a 20x4 line LCD display.
The sample code below can be downloaded here.

Dim D As New oDio4              ' Initializes a new object called D as a nibble I/O
Dim E As New oDio1              ' Initializes a new object called E as a single bit I/O
Dim RS As New oDio1             ' Initializes a new object called RS as a single bit I/O
Dim LCD as New oDataStrobe      ' Initializes a new object called LCD as a Data Strobe
Dim Srf08 As New oI2C           ' Create the SRF08 object
Dim Ver as new oByte
Dim Light as new oByte
Dim Range1 as new oWord
Dim Range2 as new oWord
Dim S As New oBuffer(5)
Dim Sw1 As New oDio1
Dim Sw2 As New oDio1

Sub main()
  IOSetup                       ' Sets up the I/O lines for the LCD
  LCDSetup                      ' Initialize the LCD Display
  SRF08Setup                    ' Initialize the SRF08's
  ClearLCD
  LocateRow1
  LCD.String = "SRF08/ooPIC Example"

  Do
    ReadSonar(112)              ' 112 is decimal of 0xE0 shifted right by 1
    LocateRow2
    LCD.String = Str$(Light) + " "
    LCD.String = Str$(Range1) + " "
    LCD.String = Str$(Range2)

    ReadSonar(113)              ' 113 is decimal of 0xE2 shifted right by 1
    LocateRow3
    LCD.String = Str$(Light) + " "
    LCD.String = Str$(Range1) + " "
    LCD.String = Str$(Range2)
  Loop
End Sub

Sub ReadSonar(Addr As oByte)
  SRF08.Node = Addr             ' I2C Address of SRF08 sonar
  SRF08.Location = 0            ' Command Register
  SRF08.Width = cv8bit          ' 1 byte wide transfer
  SRF08 = 81                    ' Ranging Command - Result in cm

' Use these three lines
  Do                            ' Wait for ranging to complete
    Ver = SRF08                 ' This will wait forever if your sonar
  Loop While Ver=255            ' becomes disconnected, so you may prefer

' or this one
' OOPic.Delay = 7               ' to simply wait 70mS instead

  SRF08.Location = 1            ' Light Sensor Register
  Light = SRF08                 ' Get Light Sensor value
  SRF08.Width = cv16bit         ' 2 byte wide transfer
  SRF08.Location = 2            ' 1st Range Register
  Range1 = SRF08                ' Get Range to 1st object
  SRF08.Location = 4            ' 2nd Range Register
  Range2 = SRF08                ' Get Range to 2nd object
End Sub 'ReadSonar

Sub LocateRow1()
'Routine to locate the cursor at row 1, column 1
  RS = 0                        ' Sets Register Select to Control Register
  LCD.Value = 128:              ' Sets the address of the cursor
  RS = 1                        ' Sets Register Select to Data Register
End Sub

Sub LocateRow2()
'Routine to locate the cursor at row 2, column 1
  RS = 0                        ' Sets Register Select to Control Register
  LCD.Value = 192:              ' Sets the address of the cursor
  RS = 1                        ' Sets Register Select to Data Register
End Sub

Sub LocateRow3()
'Routine to locate the cursor at row 2, column 1
  RS = 0                        ' Sets Register Select to Control Register
  LCD.Value = 148:              ' Sets the address of the cursor
  RS = 1                        ' Sets Register Select to Data Register
End Sub

Sub LocateRow4()
'Routine to locate the cursor at row 2, column 1
  RS = 0                        ' Sets Register Select to Control Register
  LCD.Value = 212:              ' Sets the address of the cursor
  RS = 1                        ' Sets Register Select to Data Register
End Sub

Sub ClearLCD()
  RS = 0                        ' Sets Register Select to Control Register
  LCD.Value = 1:                ' Sets the address of the cursor
  RS = 1                        ' Sets Register Select to Data Register
End Sub

Sub IOSetup()
'This routine sets the IO lines that are connected to the LCD display
'and configures the Virtual Circuit to do the Data Strobe.
  D.Iogroup = 3                 ' I/O group used for nibble out to LCD
  D.Nibble = 0                  ' Lower nibble - I/O 24-27
  D.Direction = 0               ' Direction of data flow in or out
  E.IOLine = 23                 ' Which I/O line to use for Display enable and strobe
  E.Direction = 0               ' Direction of data flow in or out
  E = 0                         ' Initialize E to 0
  RS.IOLine = 22                ' Which I/O line to use for Register Select
  RS.Direction = 0              ' Direction of data flow in or out
  RS = 0                        ' Initialize RS to 0
  LCD.Output.Link(D)            ' Connect the datastrobe's data to the LCD's data I/O line
  LCD.Strobe.Link(E)            ' Connect the datastrobe's datastrobe to the LCD's E I/O line
  LCD.Operate = cvTrue          ' Turn the Datastrobe object on.
End Sub

Sub LCDSetup()
'This routine performs LCD reset and initialization and
'configures LCD display to operate in 4 bit nibble mode
'Note: LCD Module starts out in 8 bit mode'
'This sequence of codes were found in the Optrix handbook.
  RS = 0                        ' Sets Register Select to Control Register
  LCD.Mode = cv8Bit:            ' Select 8-bit mode
  LCD.Value = 3:                ' Power on sequence
  LCD.Value = 3:                ' Power on sequence
  LCD.Value = 3:                ' Power on sequence
  LCD.Value = 2:                ' Sets data length to 4 bits
  LCD.Mode = cv4Bit:            ' Select 4-bit mode
  LCD.Value = 40:               ' Set # of display lines & font
  LCD.Value = 8:                ' Set display off
  LCD.Value = 12:               ' sets display on
  LCD.Value = 6:                ' Set entry mode
  RS = 1                        ' Sets Register Select to Data Register
End Sub

Sub Srf08SetUp()
  SRF08.Node = 112              ' Decimal of Hex address 0xE0 shifted right by 1
  SRF08.Mode = cv10bit          ' I2C mode is 10-Bit Addressing.
  SRF08.NoInc = 1               ' Don't increment
  SRF08.Width = cv8bit          ' 1 byte wide transfer
  SRF08.Location = 2            ' Range Register
  SRF08 = 140                   ' Limit 1st sonar Range to 6m, 140 x 43mm = 6m
  SRF08.Node = 113              ' Decimal of Hex address 0xE2 shifted right by 1
  SRF08 = 140                   ' Limit 2nd sonar Range to 6m, 140 x 43mm = 6m
End Sub

You can find more information on the SRF08 here