Altered 'Start Here Bot' Code

Im trying to run my start here bot that is has a tamiya twin gearbox and SRF05 instead of the IR sensor on it. All the components work, just not together. I wrote my code (most copied from frits) but my robot just goes forward and back, sometimes sideways about 2 centimetres. Can you post anything wrong in the code that may help to fix this problem

symbol trig = 3
symbol echo = 6
symbol range = b1
Symbol dangerlevel = 65
symbol turn = 300
symbol servo_turn = 700

main:
pulsout trig,2
pulsin echo,1,range
pause 10
readadc 1, b1
if b1 < dangerlevel then
gosub nodanger
else
gosub whichway
end if
goto main

nodanger:
high 5 : high 7 : low 4 : low 6
goto main

whichway:
gosub totalhalt
gosub lturn
pause servo_turn
readadc 1, b1
gosub totalhalt
gosub rturn
pause servo_turn
readadc 1, b2
gosub totalhalt
if b1<b2 then
gosub body_lturn
else
gosub body_rturn
end if
return

body_lturn:
high 5: low 4
pause turn : gosub totalhalt
return

body_rturn:
high 7: low 6
pause turn : gosub totalhalt
return

rturn:
servo 0, 100
return

lturn:
servo 0, 200
return

totalhalt:
low 4 : low 5 : low 6 : low 7
Servo 0,150
wait 1
return








Gosub stack

I’m not an expert on PICAXE programming but I see something that I feel is a bit strange.

main:
    pulsout trig,2 
    pulsin echo,1,range 
    pause 10 
    readadc 1, b1 
    if b1 < dangerlevel then
        gosub nodanger 
    else
        gosub whichway
    end if
    goto main

nodanger:
    high 5
    high 7
    low 4
    low 6
    goto main

From your gosub ‘nodanger’ you’r not returning via a return commant, -but forcing it back on the main loop.
Either you should try returning with a return or you could use a goto nodanger in stead of gosub.
I have no idea if this helps but might be worth a try?

readadc

You still have your read adc in there. If you switched from sharp sensor to a sonar, you dont need this. If nothing is plugged into the ADC channel, it probably returning a 0 thus your code is going to no danger everytime.

Yup, you have got them a lot of places it should not be.

Oh wait, more…

No where in your code do you compare your range to anything. You have triggered the sonar and gotten a number back but you didn’t do anything with that number. Also, I noticed you left the dangerlevel symbol at 65 --this is way too low for your sonar. Do some tests with your sonar and get some good numbers. One more, you do have range defined at the beginning but it is b1 --most of your numbers returning from the sonar won’t fit in just one byte and it will overflow, maybe many times. Use a word instead.

ummm???

Ok. I did a test  with the sonar and round for a 30cm or so distance it was about 35… So whats up with that… If anything I though my dangerlevel was too high…

did you use a byte for range?

Did you use a byte for range? If so, it probably overflowed and landed back on 35.

Replacements

So what should I replace it with to read the SRF05. The pins are kinda scattered everywhere so I have no idea what to write

I used the b1 variable

I used the b1 variable… The value was changing fine when i moved my hand back and forth. What symbol should I replace it with?

You need a word variable

A byte is 8 bits, (8) 1’s or 0’s. It can represent a number between 0 and 255. If it goes over 255 or under 0 it will loop on itself. I.e. 254 + 3 = 1

You need a word variable. W0, W1, W2 etc. A word variable is made of 2 bytes and allows for much bigger numbers to be stored in it.

You did catch the readadc thing, right?

Thanks

I kinda get it now. What command do i have to use to read a word variable?

None.

And there was no command to “read” b1 either. Please read the manual in reference to variables (General) and readadc.

I dont get it

I dont really understand what you mean… I read all the manuals but still dont know what to do… Can you make a comment with my code and your changes in it?? Maybe it will help me to understand

Ummm…

I want to be sure we are on the same page here. Tell me what page of the manual where you found the information about the variables and which page number you found the information about readadc (and ADC inputs) and I can help you much better. If we are talking about 2 different things I can’t help you.

Just post the page numbers (from the manauls) for “variables general” , “variables math” and “readadc” and I can then give you the code you need.

 

Thats the thing

Thats the thing… Ive tried all the read commands from page 143- 152 but none work… I dont need to understand readadc because im not using it, arent I?

Pages

Variables- Mathamatics: page 20

Variables- General: page 10

readadc: 143

Nice, thank you.

Good, I’m sorry about making you jump through the hoops with the page numbers and all but there is a reason for it. Now I know you were going through the “read” command --none of which is what you need. You don’t need to “read” any variable. Consider it a number (it can be a letter too but don’t worry about that now). For example:

pulsout trig,2   <<<<<<< This tells the sonar to fire
pulsin echo,1,w1 <<<<<<<< This gets the echo and puts that number into w1 – w1 now equals what the sonar’s range
pause 10

Now we can compare that variable with other things or display it or do whatever we want.

if w1<357 then do something

if w1=35 then let w1=0

let w1=w1+35

 

Here is your code:

'cut and copy this into your picaxe programming software. I added '** where I changed something.

symbol trig = 3
symbol echo = 6
Symbol dangerlevel = 65
symbol turn = 300
symbol servo_turn = 700

main:
gosub sonar '
if w1 > dangerlevel then '

gosub nodanger
else
gosub whichway
end if
goto main

sonar: '
pulsout trig,2
pulsin echo,1,w1 '

pause 10
return

nodanger:
high 5 : high 7 : low 4 : low 6
return '

whichway:
gosub totalhalt
gosub lturn
pause servo_turn
gosub sonar '

let w2=w1 'this is to remember the sonar reading when looking left
gosub totalhalt '** don’t need this
gosub rturn
pause servo_turn
gosub sonar '** now w1 is the sonar reading from the right
gosub totalhalt '** don’t need this
if w1<w2 then 'if looking left is farther than looking right then
gosub body_lturn
else
gosub body_rturn
end if
return

body_lturn:
high 5: low 4
pause turn : gosub totalhalt
return

body_rturn:
high 7: low 6
pause turn : gosub totalhalt
return

rturn:
servo 0, 100
return

lturn:
servo 0, 200
return

totalhalt:
low 4 : low 5 : low 6 : low 7
Servo 0,150
wait 1
return


Thank you sooooo much

thanks for all your help… I think the sonar is fixed, its just now my motors arent moving at all… It just looks around, the sensor makes a rattley noise then doesnt move. Any ideas???

dont worry

Dont worry about it. The code is working and thats all that matters… Its just my motors being as gay as possible…
Thanks sooo much for the help

thanks
this is exactly what i needed as well. much appreciated.