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

you can’t RETURN when you

you can’t RETURN when you came just from a GOTO

which in your case is actually an implicit goto

you have to options :

 

  • 1) use a GOTO instead of a RETURN

 

if b1<b2 then

gosub bodyturnleft
else
gosub bodyturnright
end if
GOTO MAIN

 

  • 2) GOSUB instead of GOTO

 

if range > 20 then GOSUB drivef

if range < 20 then GOSUB search

but this will need drivef to RETURN

drivef:

high 7 : high 5 : low 4 : low 6

RETURN

at first, I thought you were over the stack limit with all your nested procedures

but the Picaxe DEBUGGER was clear : STACK ERROR - RETURN WITHOUT GOSUB

do you use the ‘Yellow Play Arrow’ ?

So whats the problem? Is it
So whats the problem? Is it not transferring to the robot is it getting stuck, whats going on with the code as written?

This is a mess…

Alright here we go:

Is this the WHOLE code… Everything you are sending to the bot? The sonar sensor you are using does not input into a ADC channel. The servo pause time should really be in the slookright and slookleft subroutines. You need to put the sonar code in its own subroutine. Have you run a seperate test using just the sonar (with a debug command) to be sure that 20 is the distance you want to turn at? Actually, I would eliminate the whole /58 stuff and just use the raw number coming out of the sonar. Why do you have it turning for a longer time to the right instead of the left? In the "search" run-down you are repeating the total-stop subroutine. Other than that…

 

 

 

Someone should write a book
Someone should write a book on how to write code for the picaxe. If we only had a book or a manual… I nominate Chris!

yeah!
yeah! We should have like maybe 3 manuals. One that is about the feauteres of the 'axe and how it works. Then the second could be about codes’n’programming ■■■■■ Then the third could cover other stuff like electronics and connections. That would’ve been totally awesome!

yellow play arrow? no cant
yellow play arrow? no cant say that i do. Is it usefull?

it goes to the robot and the
it goes to the robot and the robot goes foward till there is an obstical, it turns its head both ways but then intead of going in the right dirrection it allways turns to one side.

that is the entire code,for

that is the entire code,

for the sonar i used the code here node/66 , the distance i get back in in centimeters and is very accurate.

i dont know why i have it turning longer to one side, must have been distected.

will try to fix the code and post it

 

Look at the code.

Look at the code.

lookleft:
gosub slookleft
pause servo_turn
gosub totalstop
readadc range, b1

lookright:
gosub slookright
pause servo_turn
gosub totalstop
readadc range, b2

It is looking left, then totalstop makes it look straight. So when it gets to readadc is is always looking forward. Since b1=b2 it goes right. Change each function to start with totalstop.

 

If you are only using your

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

:slight_smile:
:slight_smile:

Blinking LED…
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!
+1!

yea , i agree with that, i

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.

Yo.
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.

thanks your code realy
thanks your code realy helped me understand how the code works !!! thanks to you i was able to write my own.