autonomous timing

how can i program the robot to do something for a amount of time because i want to program autonomous to just go toward the rack.
so far i got this but im not sure how the while loops are going to work
while(autocounter2<=10)
{
pwm01=200;
pwm02=254;
autocounter2=autocounter2++;
}
if(autocounter2==2)
{
while(autocounter<=50)
{
pwm01=254;
pwm02=254;
autocounter=autocounter++;
}
}

I’m not sure what value you have autocounter2 initialized as but according to your code the second while loop will never occur.
at the end of your fist loop assuming autocounter2 is initialized to 0 autocounter2 will equal 11 and since 11 is not equal to 2 it skips the second loop.

I’m also assuming from looking at your motor speed values that you are turning a little then driving full speed forward. if thats the case then here is my suggestion.

take out the if statement then when the first loop finishes and autocounter2 equals 11 the second loop will start and go till autocounter2 = 51
it would look like this:

while(autocounter2<=10)
{
pwm01=200;
pwm02=254;
autocounter2++; 
}
while(autocounter<=50)
{
pwm01=254;
pwm02=254;
autocounter2++;
}

by the way use autocounter2++; instead of autocounter2=autocounter2++;

Do not use while loops.

Remember, the code loops already. Simply increment the counter each loop and have an if / then statement to do whatever when it reaches the loop count that you want.

count++;
if (count <= 10)
{
pwm01=200;
pwm02=254;
}
else if ((count >=10) && (count <= 50))
{
pwm01=254;
pwm02=254;
}

Technically speaking you don’t need the count >= 10 in else if because it won’t make it there if the count IS less than 10 - it will do the first if then kick out. But I have the guys I work with do it that way for clarity’s sake.

You should also reference some loop time or the loops will happen so fast even with a fairly large loop count the robot wont have time to do anything.

I’m not sure how to refrence the clock or even use the 26ms loop time
using the default code but I’m sure someone here does.

Every loop in the user code is roughly 26.xx ms. It loops about 38 times a second (by design).

thanks guys you were very helpful, but i still want to know if there is any way other than using while loops to count time. im so exited i cant think right now. our regional starts in 2 and 1/2 hours.

There are two good ways to keep track of time. Using interrupts is precise and reliable, but it requires specific knowledge of how to do it. Counting communication packets is easy and usually good enough, and it’s trivial to explain.

The middle section of User_Autonomous_Code() executes only when there’s new data from the radio link. It’s where the default code has the comment “/* Add your own autonomous code here. */” If you increment an integer at that point, you will have a rudimentary timer that you can read and compare against. The radio communication occurs every 26.2 milliseconds, so you can use that to figure out how long to wait.

If you add 26 to the counter each time, you can measure time in milliseconds, which might be easier for the programmer to keep track of. You’ll be running a little slow, but by less than one percent – the timer should read 14872 ms after 15 real seconds.

The one real thing to worry about is lost communication packets. The timer will fail to increment if no radio data is received. Since the robot will end up being disabled anyway if that happens too often, it probably isn’t worth compensating for, but you can do so if you want by reading the packet number.