#define ROTATION_ANGLE    60
//This angle at 100% power results in a 90 rotaion before trachocounter has time to break

task main() {

     SetSensorLight(S3);
     int wait_time, signed_rotation, light, light_tresh = 0, lr=0;
     bool correction = false;
     signed_rotation = ROTATION_ANGLE;
     ResetAllTachoCounts(OUT_A);
     
     Wait(500);                             //for the sensor to initialise
     
     light_tresh = Sensor(S3)+5;            //set threshold to ambiant light


     //We start at horizontal potition. Now we need to tilt the seesaw to initiate motion
     OnFwdEx(OUT_A,100,RESET_NONE);
     while (MotorTachoCount(OUT_A) < signed_rotation);
     OffEx(OUT_A,RESET_NONE);


     signed_rotation = -signed_rotation;    //Inverts rotaiton
     
     while(true){
        light = Sensor(S3);

        while( light > light_tresh ) {    //If sensor detects ball
              light = Sensor(S3);
              correction = true; //Establish that a correction is needed
        }

        //Correction loop
        if(correction && lr == 0){
            OnRevEx(OUT_A,100,RESET_NONE);
            while (MotorTachoCount(OUT_A) > signed_rotation);
            OffEx(OUT_A,RESET_NONE);
            signed_rotation = -signed_rotation;
            correction = false;
            lr = 1;
            }
        if(correction && lr == 1){
            OnFwdEx(OUT_A,100,RESET_NONE);
            while (MotorTachoCount(OUT_A) > signed_rotation);
            OffEx(OUT_A,RESET_NONE);
            signed_rotation = -signed_rotation;
            correction = false;
            lr = 0;

            } //End of scanning/Adjusting thread
       
}//End True
}//End of Main



