First steps - Testing IR distance / servo reaction


I am taking my first steps into the Arduino environment as well as into electronics.

Please take a look at the following code and feel free to comment on it …

Best regards,


In servoMove, why do you attach and detach the servo. I thought that the attach was typically done in setup, and that detach was rarely used.

Why do you delay after attaching the servo? There is no movement associated with attaching the servo, so there is nothing that I am aware of to wait for.

The vertical and horizontal servos move the same amount. Shouldn't there be independent movements in each direction?

In checkIRDistanceSensor, the servos are moved, but the motion has nothing to do with the value read from the sensor. Is that intentional?

Aside from these observations and questions, the code is very well written. There are subroutines that perform one thing, and only one thing (with the exception of checkIRDistanceSensor, which reads the sensor and moves the servos). The names of the subroutines clearly indicate what the routine does (with the same exception). The code is commented just the right amount, in my opinion.

Good job.

Thanks for the reply.

I received my Arduino a couple of weeks ago and meanwhile I ordered some other hardware - a couple of servos, motors, IR sensor, temperature sensor and some electronic components to make it all work.

Since I am very new at electronics and embedded systems, I tried some things out.

The reason that I attach/detach the servos is because I saw some irregular movements while testing the servos in another sketch. I am not sure where this movements came from. Attaching the servos when I needed them and detaching when the board did other things seemed to solve the problem. From my point of view, it also looked more logical to act this way. I will retry the code with a single attach at setup.

The delay after the attach was added after I saw both servos moving irregularly. Once the delay was added, both servos acted simultaneously, performing the same action synchronously, as they were programmed to do.

This sketch is a first try to find out how to program some interaction between the environment (via the IR sensor) and the hardware. I wanted to find out how the servos would react if other tasks were executed between servo actions. Also how to program the hardware without any HW interrupts.

The current hardware configuration has no real purpose excepts finding out if it works at it should.

I was also looking for some programming style (naming pins, hardware and variables) and adding a reasonable amount of comment. These are also my first steps in C-programming, although I have quite some experience in programming in other languages.

I am not at all sure that this sketch is written as it should be - e.g. repetition of action on the servos, delay time ...


The reason that I attach/detach the servos is because I saw some irregular movements while testing the servos in another sketch.

Can you post the code that had that problem.

Detaching a servo has the disadvantage that there is only friction holding the servo in position. A mechanical force applied to the servo could cause it to rotate out of position. If the servo remains attached, the servo motor will counteract forces that would otherwise cause it to rotate out of position.

There may be some cases where detaching makes sense, but best to find out what was causing the irregular movement and see if you can fix that.

OK, thanks.

I will rewrite the code without detaching the servos. I also thought about running some shutdown sequence, to restore the servo positions (among other things) to the initial setup. This should allow to start the sketch with a minimum of initial movements. At least if the hardware hasn't been moved manually.

The older code (first attempts) is deleted, so I can't show it. Maybe I will try to reproduce the problem if I have similar code to write.

The irregular movements seemed like the servo trying to find its position for a couple of seconds, moving 5 or 10 degrees left and right from the programmed position. Once positioned the rest of the movements went well.

My first wall avoiding robot should be running around in a week or so, if I receive the necessary parts on time.

Best regards,