Reading from a Sharp GP2D120

Hi,

Using an ATmega8, and i'm having trouble reading from my sharp GP2D120.

Here's the code i'm using, which is from the manual of my kit:

while(1)
{
sensor=0;
for (i=0;i<5;i++)
{
sensor=(sensor+analog(0)); // Read GP2D120 5 Times
}
sensor=(sensor/5); // Average Data
if (sensor>260) // data more than 10 CM ?
{
backward(50);sleep(800); // away from object
s_left(50);sleep(600);
}
else
{
forward(50); // do not any object "Let'go"
}
}

 

so my question is why do i need to take an average over 5 readings? can't i just read it, deal with and go on?

also i was wondering if it's ok to mount that sensor vertically, as opposed to horizontally.

Thanks.

 

qin__23

why not add "
why not add " Serial.print(sensor);" to get the out put for the sensor…then decide whether to use the average or not

Averaging it will take care

Averaging it will take care of any weird fluctuations you may get and it shouldnt cause too much overhead.</p><p>If you want to detect objects moving on the horizontal plane the datasheet says that its best mounted vertically, but horizontal should work fine too.

I guess i’ll average it

I guess i’ll average it then.

I am currently mounting it horizontally, but i wanted to mount it vertically. Basically i want to have two sharp sensors on the left and right edge of my bot to tell in which direction i should turn: if there’s something on the right edge that’s closer, or on the left edge.

Thanks for the responses.