I ve been trying to write a code for my robot, but no luck. This is for a school project so it is sort of urgent. I tried adapting Frits Start here robot code. I will learn coding but i dont have the time at the moment.
if anybody could help that would be great !
symbol trig = 3 ' Define output pin for Trigger pulse
symbol echo = 6 ' Define input pin for Echo pulse
symbol range = w1 ' 16 bit word variable for range
symbol servo_turn = 700
main:
pulsout trig,2 ' produce 20uS trigger pulse (must be minimum of 10uS)
pulsin echo,1,range ' measures the range in 10uS steps
pause 10
let range = range * 10 / 58
if range > 20 then drivef
if range < 20 then search
goto main
drivef:
high 7 : high 5 : low 4 : low 6
goto main
totalstop:
low 4 : low 5 : low 6 : low 7
Servo 0,125
wait 1
return
search:
gosub totalstop
lookleft:
gosub slookleft
pause servo_turn
gosub totalstop
readadc range, b1
lookright:
gosub slookright
pause servo_turn
gosub totalstop
readadc range, b2
if b1<b2 then
gosub bodyturnleft
else
gosub bodyturnright
end if
return
bodyturnleft:
low 4 : high 5 : low 6 : low 7
pause 500
gosub totalstop
return
bodyturnright:
low 5 : high 7 : low 4 : low 6
pause 800
gosub totalstop
return
slookleft:
servo 0,175
return
slookright:
servo 0,75
return
Here is the new code with a few modifications, no mater what the distance in front of the sonar it allways trys to go right, realy odd.
symbol trig = 3
symbol echo = 6
symbol range = w1
symbol servo_turn = 700
main:
pulsout trig,2
pulsin echo,1,range
pause 10
let range = range * 10 / 58
if range > 20 then GOSUB drivef
if range < 20 then GOSUB search
goto main
drivef:
high 7 : high 5 : low 4 : low 6
goto main
totalstop:
low 4 : low 5 : low 6 : low 7
Servo 0,125
wait 1
return
search:
gosub totalstop
lookleft:
gosub slookleft
pause servo_turn
gosub totalstop
readadc range, b1
lookright:
gosub slookright
pause servo_turn
gosub totalstop
readadc range, b2
if b1<b2 then
gosub bodyturnleft
else
gosub bodyturnright
end if
return
bodyturnleft:
low 4 : high 5 : low 6 : low 7
pause 500
gosub totalstop
return
bodyturnright:
low 5 : high 7 : low 4 : low 6
pause 500
gosub totalstop
return
slookleft:
servo 0,175
return
slookright:
servo 0,75
return
Well here is the working version, thanks for all the help!!
It is the SRF005 version of the Start here robot.
There is still one problem, I need to teach the robot to back if the object in front of it on both sides are to close, I need to figure out how to programme it, I was thinking of : if b1 and b2 < 15 then goback. Unfortunately it isnt as simple as that, will look at the picaxe manual and see if it has anything in it.
symbol trig = 3
symbol echo = 6
symbol range = w1
main:
gosub sonarrange
if range > 30 then drivef
if range < 30 then search
goto main
search:
gosub totalstop
lookleft:
servo 0,175
pause 1000
gosub sonarrange
b1=range
lookright:
servo 0,75
pause 1000
gosub sonarrange
b2=range
strait:
servo 0,125
pause 1000
gosub sonarrange
b3=range
if b3<31 then
goto others
else
goto drivef
end if
goto main
others:
if b1>b2 then
goto bodyturnleft
else
goto bodyturnright
end if
goto main
drivef:
high 7 : high 5 : low 4 : low 6
goto main
totalstop:
low 4 : low 5 : low 6 : low 7
Servo 0,125
return
bodyturnleft:
low 4 : high 5 : low 6 : low 7
pause 400
gosub totalstop
goto main
bodyturnright:
low 5 : high 7 : low 4 : low 6
pause 400
gosub totalstop
goto main
sonarrange:
pulsout trig,2
pulsin echo,1,range
pause 10
let range = range * 10 / 58
return