If the IR board’s gone, then that would significantly change the strategy… Maybe you’ll be able to purchase another before the actual regional, so in case you get one working, this is what we used for the IR board:
// IR Inputs
#define IR_DIG_1 rc_dig_in04
#define IR_DIG_2 rc_dig_in05
#define IR_DIG_3 rc_dig_in06
#define IR_DIG_4 rc_dig_in07
#define IR_DISCONNECTED (IR_DIG_1 && IR_DIG_2 && IR_DIG_3 && IR_DIG_4)
#define IR_1 (IR_DIG_1 && !IR_DISCONNECTED)
#define IR_2 (IR_DIG_2 && !IR_DISCONNECTED)
#define IR_3 (IR_DIG_3 && !IR_DISCONNECTED)
#define IR_4 (IR_DIG_4 && !IR_DISCONNECTED)
That way the you can use if (IR_1) or the like to use your routines. If you want each to run a seperate routine, you’re better off setting a variable and then using a switch, something like this:
static int routine = 0;
if(IR_1){
routine=1;
}
if(IR_2){
routine=2;
}
if(IR_3){
routine=3;
}
if(IR_4){
routine=4;
}
switch (routine){
case (1):
routine_1();
break;
case (2):
routine_2();
break;
case (3):
routine_3();
break;
case (4):
routine_4();
break;
default:
routine_0();
break;
}
Even if you don’t get your IR board functional, you can still try a state machine sort of thing. You can get fancy with this sort of thing as well. You’d start by breaking whatever you want to do into a typedef, which looks something like this:
typedef enum{
AUTO_DRIVE_1
AUTO_TURN
AUTO_DRIVE_2
AUTO_COMPLETE
} auto_state;
In case you’re not familiar with enumeration, this will make AUTO_DRIVE_1 a macro of 0, AUTO_TURN a macro of 1, etc. That way you can reference a switch statement with it. This would be the bulk of your case state machine:
usigned char do_half_lap (void){
static unsigned char current_state = 0; //This is the current progress
switch (current_state){
case (AUTO_DRIVE_1):
current_state += drive_x_dist(100);
break;
case (AUTO_TURN):
current_state += turn_90_degrees();
break;
case (AUTO_DRIVE_2):
current_state += drive_x_dist(50);
break;
case (AUTO_COMPLETE):
return 1;
break;
}
}
return 0;
All of the example functions would return an unsigned char, 1 or 0, based on completeness, like do_half_lap. This means the state is getting pushed up to the next level each time one of the steps is complete. Of course, this means you need to make the subfunctions. If you want, these can be state machines as well!
I’ll leave the subfunction implementation code up to you; but it should be pretty easy. Let me know if any of this is too complicated. Keep in mind these are all samples and haven’t been tested. I advise not copy/pasting anything and writing your own with this as a reference.