Let's Make Robots!

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








Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
this is exactly what i needed as well. much appreciated.

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


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

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

Variables- Mathamatics: page 20

Variables- General: page 10

readadc: 143

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

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

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?