Let's Make Robots!

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

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

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

 

 

 

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

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

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. :)

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.

 

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

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 thanks makes good sense. thanks for putting this so i can understand. will show u what i come up with.

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?

 

 

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