Let's Make Robots!

Picaxe programming

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

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

If you are only using your SRF05, I don't know why this readADC thing,

 

for me:

symbol trig = 3
symbol echo = 6
symbol range = w1
symbol servo_turn = 700

main:

gosub sonar_range

if range > 20 then GOSUB drivef
if range < 20 then GOSUB search

goto main

drivef:
high 7 : high 5 : low 4 : low 6
return

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 sonar_range

b1=range

gosub totalstop

lookright:
gosub slookright
pause servo_turn

gosub sonar_range

b2=range

gosub totalstop

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

 

sonar_range:

pulsout trig,2
pulsin echo,1,range
pause 10
let range = range * 10 / 58 

return

thanks your code realy helped me understand how the code works !!!!! thanks to you i was able to write my own.
You really should get a LED blinking and learn from there. To have built your whole bot, come right up to the deadline and then have someone else code it for you maybe sorta isn't cool.
+1!

yea , i agree with that, i dont think it is right to use somebody elts code and call it yours, that is why i amy trying to write my own,

i already made to robot do things but that was basic stuff, use LEDs, go in a preprogramed root, the most complicated thing i have coded for it was for it to use bumper switched witch was easy in comparasont to this.   

I'm hip to what you are saying... I tried to rebuild other people's code for quite a while when I started, and quickly found I had to learn each part step-by-step before I could really get it.