|
Re: quick question: TIMERS
A few things:
At the most basic level, you don't need to worry about the int looping over. Ints in C are two byte variables; an unsigned one will store up to 32000+. If you need more than that, you can use long, which is a four byte variable.
Second, you don't need two seperate if statements when you are setting pwm01 and pwm02--they do the same thing, so put all the code you need in one! You'll save processing power and increase readability.
State machines can be a little confusing at first, but they will make your code much more readable in the end, and much faster. A state machine uses the switch command, which will look at one variable, and jump to the "case" that the variable number points to. So you would have an int variable to count another int variable that would be the state.
int count;
int state = 0;
void InitAuto(void){
count = 0;
//any other variables that you need to reset
}
void User_Auto_Code(void){
switch(state){ //look at the state variable for code to jump to
case 0:
//do some stuff
if(count > 100){
state++; //move to state 1
InitAuto();
}
break;
case 1:
//do some other stuff
if(count > 50){
state++; //move to state 2
InitAuto();
}
break;
etc etc etc....
}//close switch
count++;
As you can see, its really easy to make readable code this way, and you know exactly how many program counts are in each state without having to do any math =). This is also a much lighter load on your processor; your program will execute a lot faster and be cleaner in memory.
A couple symantic things: the break; statement is needed at the end of every case, to tell the compiler that you are done with the case. Just set up your cases in order, and tell your motors what to do =).
If you can implement real timers, it will make the program that much better (254 uses a millisecond timer), but you should be fine with the program counts.
Good luck!
__________________
~Alex Baxter
Programming, Arms operation, Team 254
Last edited by 10intheCrunch : 11-03-2004 at 02:08.
|