Listing 9.2: Cut and paste this program into the code window of the OOPic Multi-language Compiler

'A simple program to control two DC motors (through a
'Pololu Micro Dual Serial Motor Controller) and two
'bump switches.

Dim S As New oSerialx
Dim R As New oDio1
Dim LSwitch As New oDio1
Dim RSwitch As New oDio1

Sub Main()

'Turn internal "pullup resistor" on
OOPic.Pullup = cvTrue

'Assign bump switches to I/O pins 8 and 10
LSwitch.IOLine = 8
LSwitch.Direction = cvInput
RSwitch.IOLine = 10
RSwitch.Direction = cvInput

R.IOLine= 31 'reset pin
S.Baud = cv4800
S.IolineS = 30 'motor controller pin
S.Operate = cvTrue
R.Direction = cvOutput
OOPic.Delay = 50

'Reset the motor controller
R.Invert 'invert reset pin
R.Invert 'invert reset pin

'Run "Do...Loop"
Do
'Check if either switch is depressed
If (LSwitch.Value = 0) or (RSwitch.Value = 0) then

'If depressed, back robot up and turn

'Motor 1
S.Value = 128 'Start byte
S.Value = 0 'Device type
S.Value = 1 'Motor and direction
S.Value = 120 'Motor speed

'Motor 2
S.Value = 128 'Start byte
S.Value = 0 'Device type
S.Value = 2 'Motor and direction
S.Value = 60 'Motor speed

OOPic.Delay = 200

'Reset the motor controller
R.Invert 'Invert reset pin
R.Invert 'Invert reset pin

End If
'Send robot forward

'Motor 1
S.Value = 128 'Start byte
S.Value = 0 'Device type
S.Value = 0 'Motor and direction
S.Value = 127 'Motor speed

'Motor 2
S.Value = 128 'Start byte
S.Value = 0 'Device type
S.Value = 3 'Motor and direction
S.Value = 127 'Motor speed

OOPic.Delay = 5

Loop

End Sub