I am using an Arduino Uno board to control a non modified Hitec servo (0 to 180 degree) and a sharp IR. The sensor is glued on so that it is facing "forward" when I drive the servo to 90 degrees. My goal is to have the servo do a sweep from 0 degrees to 180 degrees while I take measurements from the Sharp IR so I can get a feel for what is in front of my robot. I have posted the following test code I am working on to make this work. I have the line that actually stores what the sharp IR is reading commented out below. With it commented out this is the fastest I can get him to turn his “neck” from 0 degrees to 180 degrees.

For now I would be happy to get a reading every 5 degrees. So I store the reading from the sensor every 5 degrees in my array and then print it out when it is done turning his neck. My problem is he is about 15 degrees off. I know this because i set the delays for much longer so he turned his head very slow. I place objects directly in front of him and on his sides. Then I compared this slow accurate output to my output with the following code--but with the one line un-commented. I can get accurate readings if I make the delay large enough that he is turning his head about ˝ or ⅓ the speed he is currently turning.

I feel like I shouldn’t have to increase the delay because at the point in code where he makes a reading, if he is pointing to where I think he should be (the last neckservo.write position), the reading should be accurate. Does anyone have any ideas.

p.s. I realized on my read through that when I said accurate I am not clear. I believe I am receiving accurate data in distance but that it is off by 15 degrees. So my accurate readings show an object close by at val[90] and val[95] my code below shows the object close by in val[115] and val[120].

I wrote some similar code with the standard servo library. Then again I put an object infront of the robot. If I run this code with a delay of 15 milliseconds and then again with 5 milliseconds it will see the object almost 15 degrees later. The point is if I run this code I get different values. If my servo is turning 180 degrees in both cases why am I getting different values. Here is the code I used.

You can read new values from the Sharp IR sensor every 39ms. If you read them faster, you read the same value multiple times until the sensor updates it. So, you have to wait 39ms between sensor reads. If this gets too slow, use 2 or 3 sensors mounted at 90 (or 45) degrees to each other and do a 90 degrees sweep.