My first code! can someone review it?!

Hello all! Im new here to LMR and Ive decided to get on board and build my first robot! Im shamelessly borrowing ideas from the Tenacious A and Shark Fin Soup bots listed here... if you have not seen them, check them out! My base will be a morphibians RC car and the "brain" will be a picaxe 28X1 (the same setup as in the start here robot). It uses nearly the same code except that i do not want a servo rotating head. I just wanted to try something a little different and have the whole car turn to the left and right to look around (eventually it will be used to make a "Cars" robot for my little bro.) So here is the code, I was just hoping someone could look it over and make sure it all makes sense. My picaxe is in the mail but Ive been learning picbasic for nearly a month, lets hope I learned something!

 

 

symbol dangerlevel= 70

symbol turning= 300

'

'

Main:

readadc 0, B1

if B1< dangerlevel then

gosub nodanger

else

gosub Whichway

endif

goto Main

'

'

Nodanger:

High 5: High 6: Low 4: Low 7:

return

'

'

LookL:

High 4: High 6: Low 7: Low 5:

Pause Turning

gosub totalhalt

return

'

'

LookR:

High 5: High 7 Low 4: Low 6:

Pause turning 

gosub totalhalt

return

'

'

Whichway:

gosub totalhalt

gosub LookL

readadc 0, b1

gosub LookR

Pause 500

Gosub LookR

readadc 0, b2

gosub LookL

if b1<b2 then

gosub LookL

else

gosub LookR

endif

return

'

'

Totalhalt:

Low 4: Low 5: Low 6: Low 7:

pause 500

return

 

Looks pretty good

The “end if” in your main needs to be “endif” though. Good job, can’t wait to see the results.

Did it work?

When you ran this code on your picaxe chip, did it complile? What did it do in the robot?

reply

well I dont actually have the chip yet. Everything is on order, being shipped in. Ive been learning Picbasic and wanted to see if it even made sense. 

I see a couple problems(?)
there is no return after the nodanger routine. Also, you have nadanger and then the routine named nodanger. I don’t know if PICAXE basic is case sensitive (ie gosub whichway and then have a routine named Whichway)

Great thanks!

Thank you! I needed the “peer review” :slight_smile: From what i know, Picaxe is not case sensitive but Ill take note of it just in case something seems off about it. I made the changes, good eye!

You can get the PICAXE

You can get the PICAXE Programming software and check your program with it, no chip necessary. Make sure you have the chip you’re going to use chosen in the options menu, load your program, and hit the Syntax button. It will error check for you. You can also run the simulation and watch the program execute one line at a time. Handy tools to have, chip in hand or not.

Thanks for catching those other things I missed too, birdmun! D’oh! lol

so I figured out how to run

so I figured out how to run a syntax check, but how do you do a simulation? Im using MacAXEpad if it matters…

Windows here

Simulate is a button on the toolbar between Syntax and Program for the Windows programming editor. I don’t know about MacAXEpad. If the button’s not there then I’m no help to ya, sorry m8.

I use Linux often.

wine works for me to run the windows version of the editor.

looks great

servo-head-less bots are the way to go (like this one and this one)! Less possibility for hardware failure.

A few things to consider:

1. Your main loop runs “whichway” as the default and “nodanger” as the test result. Maybe switch that around so the default is to go forward instead of assuming something is in the way? This is really just theory, though, so not that important…what you have will work.

2. IMPORTANT: Don’t get too carried away on your program before you can actually load it. You want to be able to test each line / concept / routine as you’re writing it in order to troubleshoot. Start with the movement sub routine. That works? Okay, then test the whichway, then test them together, etc. Calculon has wasted many hours creating code that he thinks is elegant and awesome, only to find out that it doesn’t do ■■■■ when he gets home and loads it in his Picaxe. And the only way to troubleshoot it is to start over. 

3. As far as headless navigation, Calculon has found that the “whichway” subroutine makes more sense without the left/right choice, and an “old direction vs new direction” choice instead. Hit Calculon back if you want to know more.