//roverbot go back to its starting position after its proximity sensor  is activated
//you may notice that it does not exactly go to its initial position if 
//say its wheels slip or the ground is uneven

int returntime ;
int reflected_value;

void go_forward()
{
   ClearTimer(0);               //start timer(0)
   OnFwd (OUT_A+OUT_C);
   until (SENSOR_2 >   reflected_value + 150);
   returntime = Timer(0);       //store value of Timer(0) when above condition is satisfied
   
}

void go_back()
{
   
   OnFwd(OUT_A); OnRev(OUT_C); Wait (220);   //to make a 180 degree turn. you might have to change the wait time
   ClearTimer(0);                            //timer(0) started
   OnFwd(OUT_A+OUT_C);
   until ( Timer(0) > returntime );
   Off (OUT_A+OUT_C);                        //switch off motor after coming back
}

task send_signal()
{
   while (true)
   {
      SendMessage(0); Wait(20);             //send IR signal every 0.20 sec
   }
}

task check_signal()
{
   while (true)
   {
       reflected_value = SENSOR_2;
       if (SENSOR_2 >   reflected_value + 150)
       {
           go_back();
       }
   }
}
                               
task main()
{  
   SetSensorType(SENSOR_2, SENSOR_TYPE_LIGHT);
   SetSensorMode(SENSOR_2, SENSOR_MODE_RAW);  //light sensor attached to sensor 2
   start send_signal;
   start check_signal;
   
   go_forward();
   until (SENSOR_2 >   reflected_value + 150);
   go_back();
   stop send_signal;
   stop check_signal;                         //these 2 tasks must be stopped. if not, try putting your hands closer and 
                                              // closer to the light sensor, the roverbot will make a 180 degree turn.
  
}   
      

    Source: geocities.com/fuhan75