Sharp programing on picaxe

Ok so I want a basic obstacle avoiding program with my sharp sensor. It is made on the start here robot. The programming language is picaxe basic. After hours of trying I still cant get it going correctly. So i hope u can help me. Thanks

The idea- It is supposed to read the distance and then decide whether it is far enough away to proceed forward or to go to sub program ahead (go backword, I know confusing haha sorry). So if you have any ideas please tell me. Better yet revise my program or make a new one and show me it. Thanks! First time with sharp.

main: 

readadc 1, b1

if b1 < 170 then : goto behind : else : goto ahead: endif (yes I know they are switched in the names sorry)

wait 2

goto main

 

behind:

backward A ' motor wiring is that way to go forward 

forward B

goto main

 

ahead: 

forward A

backward b

goto main

Debug

Skip the motor for now and just put a debug into your code after readadc. From here you can see what the actual numbers are coming out of the sharp sensor. This should tell you clearly if your sensor is working correctly and what values you should be using as a “turning threashold”.

 

I agree with CtC.

**I am not a PICAXE moderately well versed/expert.

He is correct. You need to know what numbers to expect for a given distance.

The rest of this is more academic.

If the listing you show is the extent of your program, then I fnd it hard to believe you will ever get anything to move. I don’t know if PICAXE basic is case sensitive, but, I am fairly confident that forward/backward, A/B/b are not defined.

PICKING NITS If you care, you should use subroutines and therefore gosub/return rather than, for example, ahead:/goto main. Proper programming would see you replace goto ahead with gosub ahead and then in your subroutine when it is finished return instead of goto main.

Thanks, birdmun

I sorta wondered about that too…

I just went ahead and assumed that this was quickly-written psudeo-code. It’s probably good that you made mention of the fact that it might not be. In the event that this is the actual code used, a little direction might save a lot of frustration.

–Oh, and bird… Picaxe is NOT case sensitive nor does it care about tabbing. Weird…

Questions:

Is this the actual code you are using?

Is forward and backward actual commands? --As I remember, picaxe has some “classroom” commands built in. I.e. wait (seconds) instead of pause (milliseconds) etc. I think “forward” and “reverse” might actually be real commands, designed to work with 4 pre-determined pins. Is this the case?

 

 

thanks but…

ok first of all the numbers i got are from the debug. also yes the forward is just replacing the high 4, low 5. If u look on the start here robot. I used that code just took out the servos. does that help? I have used subs but it seems that they dont do much good. thanks

after the start here program

after i used the start here program I then made my own simplified program. i hope it helps and please reply :slight_smile:

Alright no worries

No problem, you are actually closer than you think.

At this point, you gotta “think” like the program. What I do a lot is to grab a pencil or sylus or whatever and actually point at the screen, line-by-line and “step” through my program manually. As you get to your goto’s and gosubs, you can actually see your pencil-pointer loop its way through the program. This seems silly, I know, but it will help plenty. Take for example, your code above.

  • Start
  • read the sensor
  • if < go here
  • if > go there
  • GOTO here or there
  • You are now here or there (forward or reverse) and we set the motors to go forward or reverse
  • Now it says Goto main
  • We now go back to the TOP of the main routine. We did NOT return to where we left from, we went directly to “go” --there for, we never touched that “wait” command. --We will never get to it--

Try some gosubs and returns instead and also stick your pause into the sub-routine. Another thought is to stick another ADC check into your subroutine. You can keep checking your sensor within the subroutine and if it is still < or still> you can stay in the subroutine. If the sensor says you have moved enough, then you can return to the main loop.

 

thanks so much keep posted !

ok thanks makes good sense. thanks for putting this so i can understand. will show u what i come up with.

**new program **

ok how does this look? from what u said i think i did it right but u know more than me haha

main: 

readadc 1, b1

if b1 < 170 then : gosub behind : end if

if b1 > 180 then : gosub ahead : end if

goto main

 

behind:

backward A 

forward B

readadc 1, b1

wait 1

return

 

ahead: 

forward A

backward b

readadc 1, b1

wait 1

return

ok…

ok for some reason it just goes forward for a bit then backwards. also where do i put my waits or pauses?

does this need to be changed? -

main: 

readadc 1, b1

if b1 > 176 then : gosub behind : else gosub ahead : end if

return

a

That is what is it supposed to do!

Your code is working properly.

  • It starts, reads the sensor and goes forward 1 second
  • it checks the sensor again, sees we are too close and backs up for one sec
  • it check the sensor, sees we are far away now and goes forward
  • etc etc etc

In terms of the readadc in the sub routine --You can do something like this:

behind:

backward A 

forward B

readadc 1, b1

if b1 < 170 then goto behind

else

return

 

 

 

**haha **

oh ok i had them switched haha. i see i have much to learn with sensors. thanks chris for bieng so patient with a begginer. :slight_smile:

ok… this is what i have with what u just said

ok… this is what i have with what u just said but it still goes forward then backward randomly.

main: 

readadc 1, b1

if b1 < 180 then : gosub behind : else : gosub ahead : end if

return

 

behind:

backward A 

forward B

readadc 1, b1

if b1 < 180 then : goto behind : else : return

end if

 

ahead: 

forward A

backward b

return

final mostly working program

main: 

readadc 1, b1

if b1 < 184 then : gosub behind : else : gosub ahead : end if

return

 

behind:

backward A 

forward B

 

readadc 1, b1

if b1 < 184 then : goto behind : else : return

end if

 

ahead: 

forward A

backward b

return