functions on autonomous

How do you create while loops or functions on autonomous program? I have to do it to operate motors

The autonomous task is the start point for your auton to run. In auton you need to use the sensors or the timers to control how long to run specific motors.

When the competition switch sends you into autonomous mode it starts that task and starts at right there at the top and will end if it reaches the bottom. it can also be cut off if the timer reaches the end of the auton period (15 seconds this year).

So what you want to do is call other functions and loops in the auton task which is just like you do in user control. You just don’t read the joystick in auton.

You can have a while loop to see if you reached a sensor traget and run your motors in the while loop and then end it at the end of the while loop. Let’s look at this simple while loop in your auton.

while(SensorValue[my_favorite_encoder] <= target_value)
{
      motor[left_drive] = motor[right_drive] = 127;
      wait1Msec(5);
}
motor[left_drive] = motor[right_drive] = 0;

This would say while the quad encoder I named my_favorite_encoder is less than or equal to the target value of how far I want to go forward, I jam the motors to full power. Don;t do equals because it is very rare you would get exactly to that value. So once there, you will break the loop and go on to the next statement. Since you want to stop, turn the motors off.

The wait statement is so you don;t hog the CPU and is nice to let the other tasks do their job like filling in the sensor values

You can also use a timer in conjunction with the loop or as the main item in the loop. There are a few timers you can use in RobotC. T1 is the first one.


ClearTimer(T1);
while( time1(T1)<= target_time)
{
      motor[left_drive] = motor[right_drive] = 127;
      wait1Msec(5);
}
motor[left_drive] = motor[right_drive] = 0;

This one goes until it is less than timer T1 hits your target time. Put the less than or equals again as a fail safe to ensure it hits and is not needed exactly there. You are waiting 5 milliseconds between checks…

You can also mix them with an or statement to kick out of a while loop.

References
http://www.education.rec.ri.cmu.edu/products/teaching_robotc_vex/radio/timers/docs/timers.pdf
http://dkc.squarespace.com/code/2010/10/16/learning-robotc-part-7-timers.html