Is this program correct?

My robot is only moving forward with this program. am usinf a SHARP IR 2Y0A21.. when i place my hands in front of it,it stops and then starts moving forward and wont turn to any side. this is my program..-

Symbol danger = 160

symbol turn = 200

symbol servo_turn = 700

main: readadc 0, b1 if b1 < danger then gosub nodprob else gosub whichway end if

goto main

noprob: high 5 : high 6 : low 4 : low 7 gosub whichway return

whichway: wait 1

gosub lturn

pause

robo_turn

readadc 0, b1

wait 1

gosub rturn

pause robo_turn

readadc 0, b2

gosub stopinst

if b1<b2 then

gosub robo_rturn

else

gosub robo_lturn

end if

return pause turn:

gosub stopinst

robo_lturn:high 6 : low 5 : low 7 : high 4

pause turn:gosub stopinst

return

robo_rturn:high 5:low 6:high 7:low 4

pause turn:gosub stopinst

return

rturn:

servo 0, 100

return

lturn:

servo 0, 200

return

stopinst:

low 4 : low 5 : low 6 : low 7

servo 0,150

wait 1

return

Delete the other 3

Ok, I have labeled and noted on the other 3 copies of this post that you need to delete them. Please do. --And an iPod might not be the correct device to use to post here it seems…

 

I have taken the liberty (again) to re-format your code, insert the proper CR’s and whitespace so people can read it. There are problems with this code, but I just spent 10 minutes commenting on your other 3 posts and cleaning up this one --I am sorta out of anwering energy. 

 

Symbol danger = 160

symbol turn = 200

symbol servo_turn = 700

main:

readadc 0, b1

if b1 < danger then gosub nodprob

else gosub whichway

end if

goto main

noprob: high 5 : high 6 : low 4 : low 7 gosub whichway return

whichway:

wait 1

gosub lturn

pause robo_turn

readadc 0, b1

wait 1

gosub rturn

pause robo_turn

readadc 0, b2

gosub stopinst

if b1  

gosub robo_rturn

else

gosub robo_lturn

end if

return

pause turn:

gosub stopinst

robo_lturn:high 6 : low 5 : low 7 : high 4

pause turn:

gosub stopinst

return

robo_rturn:high 5:low 6:high 7:low 4

pause turn:gosub stopinst

return

rturn:

servo 0, 100

return

lturn:

servo 0, 200

return

stopinst:

low 4 : low 5 : low 6 : low 7

servo 0,150

wait 1

return

I’m assuming this is picaxe.

I’m assuming this is picaxe. You know the picaxe editor has a syntax checker that would have pointed you to a number of errors you could have fixed.
Formatting makes a big difference to readability. Not just to anyone helping but yourself as well to make finding errors easier.
Put all instructions on separate lines as well as sub headings. I left the motor pin assignments grouped on the same line because they are quite readable this way
I have commented the errors. I corrected them with the syntax checker but then ctrl_z them back again so you can learn something. Every line with an error is not commented. the same error exists  a couple times on different lines. If you look at each comment and fix that line you can then see which other lines have the same mistake and fix them.

Symbol danger = 160
symbol turn = 200
symbol servo_turn = 700

main:
    readadc 0, b1         ;is zero correct name for adc pin? manual seems to call them [letter.number] format look at
    if b1 < danger then     ;break up your if else statements on separate lines
    gosub nodprob         ;spelling mistake nodprob should be noprob
    else
    gosub whichway
    end if
    goto main

noprob:
    high 5 : high 6 : low 4 : low 7
    gosub whichway         ; why are we going to whichway from here? main takes care of that
    return

whichway:
    wait 1
    gosub lturn
    pause robo_turn
    readadc 0, b1        ;
    wait 1
    gosub rturn
    pause robo_turn        ;no such symbol robo_turn did you mean turn?
    readadc 0, b2       
    gosub stopinst        ; probably better to call stop before you check left and right
                    ;if b1 what? what are you comparing here? you need a comparison of b1 to b2
    if b1             ; or a value to compare each against like the danger value
    gosub robo_rturn
    else
    gosub robo_lturn
    end if
    return
    pause turn            ; these two statements do nothing as they are after the return statement
    gosub stopinst

robo_lturn:
    high 6 : low 5 : low 7 : high 4
    pause turn            ; using a delay to time turns is messy to adjust to right amount of time
    gosub stopinst
    return

robo_rturn:
    high 5:low 6:high 7:low 4
    pause turn
    gosub stopinst
    return

rturn:
    servo 0, 100
    return

lturn:
    servo 0, 200
    return

stopinst:
    low 4 : low 5 : low 6 : low 7
    servo 0,150
    wait 1
    return

Mr merser nd chris
thanks a Lott. Let me check it nd il post it…

servo problem… :frowning:

nw the problems with my servo… its nt moving according to the program…
how to correct it…?? 

Could you elaborate?

What exactly is it doing or not doing?

Is it something as simple as missing a delay?

Frpm shoutbox

It is probably not a pin number issue, “READADC 0” is an analog pin. “SERVO 0” is a digital pin. 

Then again, as I said above, you have not really told us anything about your problem, so I am just guessing and trying to fill in some gaps.

Paul, please give more information . . .

Paul,  we can help you here but we do need a bit more information to go on.

Firstly, which PicAxe chip are you using, and are you using one of the pre-built PicAxe boards or is this on a board of your own design?  Also, how is the robot steering?  From the code it looks like you’re controlling direction both with motors and a servo - Is this right?   A good photograph of the robot and a circuit diagram would help us enormously.

The first thing that stands out in the code is that you’re using pin 0 both as an analogue input for the range sensor and an output for the servo.  Pin 0 can do both, but not in the same circuit. Which pins are these actually connected to?

Post back with the answers and we’ll see what we can do for you.

I think Andy’s got me on that one

Its been a while since the last time I worked with picaxe. I am indeed a bit foggy on analog/digital pin designations.

A better point of his is the version of the chip etc.  The newer chips have moved on to using a letter/number combination and I can’t remember if simply using an analogRead command means the chip knows to use A0 as instead of a digital pin. Point is, more info is good.

**I think I may have been speaking Arduino for too long now… Gotta get back to the basics once and a while, it looks like. (you can take or leave the pun as you all see fit) 

my code and parts…

am using PICAXE 28x1 IC ,a picaxe 28 project board, a  SHARP IR 2Y0A21 and this is my code-

 

Symbol danger = 160

symbol turn = 200

symbol servo_turn = 700

 

main: 

readadc 0, b1 

if b1 < danger then

gosub noprob 

else

gosub whichway

end if

goto main 

 

noprob:

high 5 : high 6 : low 4 : low 7

return

whichway:

gosub stopinst

gosub lturn 

readadc 0, b2

pause turn 

 

gosub stopinst

gosub rturn

readadc 0, b3

pause turn 

gosub stopinst

if b2 < b3 then

gosub robo_rturn

else

gosub robo_lturn

end if

pause turn:

gosub stopinst

return

robo_lturn:high 6 : low 5 : low 7 : high 4

pause turn

gosub stopinst

return

 

robo_rturn:high 5:low 6:high 7:low 4

pause turn

gosub stopinst

return

 

rturn:

servo 0, 100

return

 

lturn:

servo 0, 200 

return

 

stopinst:

low 4 : low 5 : low 6 : low 7 

Servo 0,150 

return

my bot…

Picture_114.jpg

my board.

Picture_117.jpg

this is my problem.

my servo keeps on jettring when i place my hand in front of the SHARP IR… and when i remove my hand servo wont move to any side nd my robot would jst move forward.

 

Hi Paul. You’ve made a

Hi Paul. You’ve made a symbol Servo_turn but you haven’t used it anywhere. The servo needs time to reach it’s position after you tell it to turn. The reason being the picaxe executes commands much faster than the servo can move. So you slow the processor down with a pause.
Could be something else but I’d start there. Stick a Pause servo_turn in both the lturn and rturn and stopinst  subs just after the servo command.

I see a few (and I got it)

if b1 < danger then  should be if b1 > danger then

if b2 < b3 then might be backward as well

 

Also, this guy here:

gosub lturn 

readadc 0, b2

pause turn 

In this case, you are moving your servo, reading your sensor, THEN doing the pause (to allow time for the servo to move).

The stopinst: routine does not have a pause after the servo move either

One more

When you tested your Sharp sensor by itself, what were the numbers coming out? Is 160 a proper threshold?

160 is correct. wn I changed
160 is correct. wn I changed b1>danger it did nt work properly. It ws stoping when norhing was der in front.

Ok, you just need to isolate

It appears we have exhausted all the “standard” stuff. At this point, you need to break everything down and do this systematically. The first thing I would do is write the simplest program you can to do the following:

  • Center sensor servo at start-up (leave it centered)
  • Drive forward
  • Keep checking sensor
  • if sensor is closer than threshold then stop (as long as something is there, the bot should remain stopped  --remove your hand and the bot should start going forward again)

That’s it. Its really your code, without a servo sweep or any turning. This will tell you if the most basic flow of your code is correct. Once this is established, you can start putting your look-left/look-right back in as well as the turns.

I suspect this simple test program will uproot the problem with your main code quickly.

Have a go with this code Paul

symbol danger = 160
symbol turn = 500
symbol servo_turn = 400
symbol frontVal = b1
symbol leftVal = b2
symbol rightVal = b3

main:
readadc 0, frontVal
if frontVal < danger then
gosub noprob
else
gosub whichway
end if
goto main

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

whichway:
gosub stopinst
gosub lookLeft
readadc 0, leftVal
gosub lookRight
readadc 0, rightVal
gosub lookFront
if leftVal < rightVal then
gosub turnLeft
else
gosub turnRight
end if
return

turnLeft:
high 6 : low 5 : low 7 : high 4
pause turn
gosub stopinst
return

turnRight:
high 5:low 6:high 7:low 4
pause turn
gosub stopinst
return

lookLeft:
servo 0, 200
pause servo_turn
return

lookRight:
servo 0, 100
pause servo_turn
return

lookFront:
Servo 0,150
pause servo_turn
return

stopinst:
low 4 : low 5 : low 6 : low 7
return

 

 

oss & merser

i had installd the resistor chip…
merser the robot did nt turn… :(