Help with a project !

Hi !
i'm working on a "scout" function (going around without hitting something) for my robot, wich uses 2 360° servomotors as wheels motors.
When it finds an obstacle, it has to calculate where there aren't. the problem is that i can't let him turn left and right because it turns in one direction forever. How can i solve this ? For example it Could be fine if the servos stop after 1 or 2 seconds .
Here's the code :

void Scout(){
int DistanzaCorpo=0, DistanzaTesta=0, DistanzaSinistra=0, DistanzaDestra=0;
DistanzaCorpo = average_value(100, Sensore_Corpo);
while (DistanzaCorpo > DistanzaLimite) {
Avanti();
DistanzaCorpo = average_value(100, Sensore_Corpo);
char dato=mySerial.read();
if (dato=='e') {
Stop();
return;
}
}
ServoTestaOrizzontale.write(180);
DistanzaSinistra = average_value(100, Sensore_Testa);
ServoTestaOrizzontale.write(0);
DistanzaDestra = average_value(100, Sensore_Corpo);
ServoTestaOrizzontale.write(90);
if (DistanzaSinistra >= DistanzaDestra && DistanzaSinistra>15) {
//TurnLeft
}
}
else if (DistanzaDestra >= DistanzaSinistra && DistanzaDestra>15) {
//TurnRight
}
else {
Indietro();
}
}

Why are you reading from the Serial port within the function?

You need to post a complete program so we can see the code for all the subsidiary functions.

It would also be useful to have an English language guide to what te code is trying to do.

...R