Quote:
|
Originally Posted by Texan
Yeah, that is te basic idea. You would have a static variable at the top of that function named something appropriate like, say, state that is initailized to zero. Like this:
Code:
// this is just after the function header
static unsigned char state = 0;
In your states you would then increment it when the condition for a state being done is satisfied. I.E.:
Code:
switch(state)
{
case 0: // This is your turn state
if(supposed to turn left) // This would be however you can tell whether you are supposed to turn left or right
{
// So, we turn left however you do that
}
else
{
// This is where you turn right
}
// now we check to see if we can go to the next state
if(done turning condition) // This is your encoders or whatever you use to tell if you've turned far enough
state++; // Go to the next state
break;
case 1: // Next state
// Same idea for a long as you want
break;
default:
// If you reach here something went wrong
}
So, the idea is that you increment a state variable when you are done with a state.
PS Just as a side note, I have my defaults in my various switches calling a function named stupid(). All it does is printf() Stupid! and calls itself until it crashes. Anyways, that was kind of random. 
|
Might I suggest using an enum or define statements to name the states. state = 3.14159 is a bit more esoteric than #define AT_TARGET 3.14159 and then saying state = AT_TARGET (a bit less demanding on programmer's all-too-vexed memory too). This also helps because you don't always want to say sate++ -- for example, in my code I have a state to turn left and another to turn right, and it all depends on which sensor sees it; so there is an if statement assigning state a different value based on the condition.