My Code

Nav.doc (50176Bytes)

Hi all,

 

I've got my robot working and I'm quite pleased with it, especially as I'm new to programming. I haven't written the code myself. I've downloaded various bits and edited it quite heavily. Non the less, I'd appreciate an experienced eye looking over it and pointing out any mistakes, or where I could be more efficient, etc. Which I imagine is numerous areas.

 

I'm not asking anyone to re-write it for me, just assist and point me in the right direction.

 

I didn't seem to be able to upload the file as an .ino, so I've attached as a .doc. Perhaps someone could advise on this?

 

(Link to ridicule page.)

 

Thanks,

A couple quick points before I really dive in to your code.

Instead of a doc file, next time consider a txt file.

Also, one of the main reasons for functions/procedures(ex. void setup() is a procedure) is to avoid retyping blocks of code over and over. For example, you have a procedure that stops your servos. You have another procedure that stops the servos as well as powers off everything(?). Rather than retyping the code to stop your servos, you could have just as easily called the procedure that stops the servos. Mind you this example is a bit weak, but, it still is useful.

When you become more proficient, maybe you could write a function similar to http://pastebin.com/EXGjMNrz for servos.

There are a few things you might consider with respect to style.

  • Make constants/#defines all caps. #define MAXSPEED 225 or const int MAXSPEED 225; // don’t exceed 225 for PWM
  • Make your variable names intelligible. int ltMtrSpd; // don’t do this int leftMotorSpeed; // do this
  • Use only one version of naming convention with respect to case sensitivity. Do not mix leftMotorSpeed with LeftMotorSpeed or Left_Motor_Speed. These are all 3 different variable names due to case sensitivity.
  • You might consider limiting line lengths to 80 characters.
  • Rather than commenting to the right of your procedures, you might consider commenting just above them as I did in my pastebin link.

While the above bullet points are up for arguement, if you follow them, the coders here will be happier to help debug code, and, you will be able to read your code 6 months, a year, ten years down the road. If you make sure to keep including comments on bits of code that could be considered cryptic, you will also help yourself down the road. :slight_smile:

exceeding pwm >255 has no

exceeding pwm >255 has no effect from what i recall. 225 it’s a safe value.

The 225 was just a number and

the comment was just a comment not necessarily a valid reason. I imagine my thinking was along the lines that maybe 255 was too fast. Or, maybe the lower top speed was to help conserve power. Either way I could have left the comment out and avoided any confusion. :slight_smile:

Code comments

1. Get rid of support for hardware you do not have. It just rattles the brain cells.

2. The scan() function can return a value. Slight code improvement.

3. Your forward/reverse values do not seem to be centered around 90.

I have prettified your code to my usage. Comment or email me if you want a copy.

George

To expand on scan() returning a value

int scan() {

  return sonar.ping_cm();

}

To call this version of the scan function, you need to place the return value in a variable. Like so, distance = scan();

If you look at the functions that libraries have, you may find you can save some work for yourself. Also, with the above version you can move the declaration of distance inside the loop(). You can also get rid of the time variable.

Code comments

You have a recursive calls in the moveLeft & moveRight functions. Recursion is a very usefull technique but it is not well suited for a limited RAM environment such as an Adruino. Each time the function is invoked it will use a portion of the RAM dedicated to the stack. Overflowing the stack will crash the program in a very unpredictable manner.

In your environment, failing to find an acceptable escape path will be fatal.

You can rewrite the function using a do/while or while loop. The robot will spin in place but that, IMO, is better than a random crash.