I have the code that follows setup to read in three digital inputs (set as global unsigned chars) in a user function and then it runs through the possibilities and returns an unsigned char back to the main routine. First, am I setting the conditions correctly in the If’s? Also, would this be better set as a switch case??? And if so, then how to implement the multiple variables in the Switch statement.
Also, all variables are default to = 1 so that the last result is true and kicks out of the user function back to main.
Using case and switch won’t really change anything and would probably add more code.
void main ( void )
{
switch ( var_x )
{
case 1 : // if var_x is 1
{
// do stuff
break ;
}
case 2 : // if var_x is 2
{
// do stuff
break ;
}
case 3 : // if var_x is 3
{
// do stuff
break ;
}
default : // If var_x is not equal to and of the cases
{
// do stuff
}
}
I got a similar question by e-mail so I figured I’d post a solution based on what you asked for. This can also be reworked with the digital inputs to select autonomous modes. This takes the 1 or 0 that JoystickButtons() returns shifts it by x number of places. Then OR’s it against the current value of the variable, which makes it count. You make upto 15 button combinations with the 4 buttons on Channel 7.
| = OR ( 00000001 | 00000010 ) becomes 00000011
x << 1 = Bitshift x Up on Place, so form 00000010 to 00000100
void main ( void )
{
int b;
while ( 1 )
{
b = GetJoystickDigital ( 1 , 7 , 1 ) ;
b = b | GetJoystickDigital ( 1 , 7 , 2 ) << 1 ;
b = b | GetJoystickDigital ( 1 , 7 , 3 ) << 2 ;
b = b | GetJoystickDigital ( 1 , 7 , 4 ) << 3 ;
PrintToScreen ( "Button Total = %d\n" , b ) ;
switch ( b )
{
case 1 :
{
//button D
break ;
}
case 2 :
{
//button U
break ;
}
case 3 :
{
//button U & D
break ;
}
case 4 :
{
// button L
break ;
}
default :
{
//nothing pressed or not recognized
}
}
Wait ( 10 ) ;
}
}
I realize I didn’t identify myself as I should have. My name is Tony and I’m with BEST team #78 Thunderstorm Robotics from Payne County Oklahoma. No autonomous modes here. Just getting values of 3 digital inputs and comparing to a table of 8 possible combinations and then sending that back to a switch case to provide some kind of visual output depending on the unsigned char that’s returned.
Thanks,
Tony Lettkeman
Vision Computer Services
Coach/Mentor for Thunderstorm Robotics
BEST Team #78