Step 16Source Code
SOURCE CODE FOR THE SONAR OPTION IN THE NEXT STEP (SPECIAL STUFF)
Follow the directions for your microcontroller and cut and paste this code into the editor. Compile and send to the robot.
The code is ultra simple but the bot avoids most obstacles it sees. It is easy to take my code and expand upon it.
'SuperSimpleBeginnersRobot
'www.instructables.com
'Ted Macy
'ted@oobug.com
'2/16/09
'-----------------------------------------------------------------
' Create and Setup Objects
'-----------------------------------------------------------------
Dim Servo_Right As New oServo
Dim Servo_Left As New oServo
Dim SRF04Servo As New oServo
Dim GP2D12 As oIRRange(3,8,cvOn)
Dim SRF04 As oSonarDV(8,9,cvOn)
SRF04Servo.IOLine=29 'Set the servo to use I/O Line 30.
SRF04Servo.Center=28 'Set the servos center to 28. (see manual)
SRF04Servo.Operate=cvTrue 'Last thing to do, Turn the Servo on.
Servo_Right.IOLine = 30 'Set the servo to use I/O Line 30.
Servo_Right.Center = 28 'Set the servos center to 28. (see manual)
Servo_Right.Operate =cvTrue 'Last thing to do, Turn the Servo on.
Servo_Left.IOLine = 31 'Set the servo to use I/O Line 31.
Servo_Left.Center = 28 'Set the servos center to 28. (see manual)
Servo_Left.Operate =cvTrue 'Last thing to do, Turn the Servo on.
'-----------------------------------------------------------------
' End Create and Setup Objects
' Main routine is your primary routine called upon power up!
'-----------------------------------------------------------------
Sub main()
Do
Call IR
Call Forward_All
Call SServo
Loop
End Sub
'-----------------------------------------------------------------
' End of Main routine
' Start of Drive System Subroutines
'-----------------------------------------------------------------
Sub Spin_Left()
Servo_Left.Invert=0
Servo_Left = 62
Servo_Right = 60
End Sub
Sub Spin_Right()
Servo_Right.Invert=1
Servo_Left = 62
Servo_Right = 60
End Sub
Sub Forward_All()
Servo_Right.Invert=0
Servo_Left.Invert=1
Servo_Right = 60
Servo_Left = 62
End Sub
Sub REVERSE()
Servo_Right.Invert=1
Servo_Left.Invert=0
Servo_Right = 60
Servo_Left = 62
End Sub
Sub STOP()
Servo_Left = 0
Servo_Right = 0
Call SServo
End Sub
'-----------------------------------------------------------------
' End of Drive System Subroutines
' Start of Sensor Subroutines
'-----------------------------------------------------------------
Sub SServo()
SRF04Servo.Position=15
SRF04.Operate.Pulse(1,1,250)
If SRF04.Value<128 Then
Call Spin_Right
Else Call Forward_All
End If
ooPIC.Delay=600
SRF04Servo.Position=31
SRF04.Operate.Pulse(1,1,250)
If SRF04.Value<64 Then
Call REVERSE
Else Call Forward_All
End If
ooPIC.Delay=600
SRF04Servo.Position=46
SRF04.Operate.Pulse(1,1,250)
If SRF04.Value<128 Then
Call Spin_Left
Else Call Forward_All
End If
ooPIC.Delay=600
SRF04Servo.Position=31
SRF04.Operate.Pulse(1,1,250)
If SRF04.Value<64 Then
Call REVERSE
Else Call Forward_All
End If
ooPIC.Delay=600
End Sub
Sub IR()
If GP2D12.Value<64 Then
Call STOP
End If
End Sub
'-----------------------------------------------------------------
' End of Sensor Subroutines
'-----------------------------------------------------------------
| « Previous Step | Download PDFView All Steps | Next Step » |
3
comments
|
Add Comment
|
![]() |
Add Comment
|
























































