/* the function turn(...) lets the robot spin around its axis until the tower is
/* directly in the straight-ahead-position */              

void turn(int k, int speedr, int speedl, int uirdl, int uirdr)
{

/* the following do-while statement defines the variable k which is non-zero if */
/* the tower is not straight ahead from the robots upper IR's. */
/* while spinning into the straight-ahead-position, k gets smaller */
/* when it reaches a tolerance value smaller than 4, the robots' turn is finished */ 
	do
	{
		uirdr = UPPER_RIGHT_IR;
		uirdl = UPPER_LEFT_IR;
		if (uirdr > uirdl)
		{
			speedl = SLOW_SPEED_L;
			speedr = ZERO_SPEED;
			k=uirdr-uirdl;
		}
		else
		{
			speedl = ZERO_SPEED;
			speedr = SLOW_SPEED_R;
			k=uirdl-uirdr;
		}
		wait(200);   /* wait for hardware delay*/
		if (k<4)
			k=0;
		motorp(RIGHT_MOTOR, speedr);
		motorp(LEFT_MOTOR, speedl);
	} while (k);
	motorp(LEFT_MOTOR, ZERO_SPEED);
	motorp(RIGHT_MOTOR, ZERO_SPEED);      /* stop the robots' spinning */
	wait(1000);                           /* wait for hardware delay */
}