If ElseIf or Switch Case

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.

#include "Main.h"

unsigned char Func_DataPort ( void )
{ // runs the routine to retrieve data port assignments and create a variable with the results
      unsigned char Port_Set = 1; 

      CHAR_PG2 = GetDigitalInput ( 1 ) ;
      CHAR_PG1 = GetDigitalInput ( 2 ) ;
      CHAR_PG0 = GetDigitalInput ( 3 ) ;
      if ( CHAR_PG2 == 0 & CHAR_PG1 == 1 & CHAR_PG0 == 1 )
      {
            Port_Set = 2 ;
            return Port_Set ;
      }
      else if ( CHAR_PG2 == 1 & CHAR_PG1 == 1 & CHAR_PG0 == 0 )
      {
            Port_Set = 3 ;
            return Port_Set ;
      }
      else if ( CHAR_PG2 == 1 & CHAR_PG1 == 0 & CHAR_PG0 == 1 )
      {
            Port_Set = 4 ;
            return Port_Set ;
      }
      else if ( CHAR_PG2 == 1 & CHAR_PG1 == 0 & CHAR_PG0 == 0 )
      {
            Port_Set = 5 ;
            return Port_Set ;
      }
      else if ( CHAR_PG2 == 0 & CHAR_PG1 == 1 & CHAR_PG0 == 0 )
      {
            Port_Set = 6 ;
            return Port_Set ;
      }
      else if ( CHAR_PG2 == 0 & CHAR_PG1 == 0 & CHAR_PG0 == 1 )
      {
            Port_Set = 7 ;
            return Port_Set ;
      }
      else if ( CHAR_PG2 == 0 & CHAR_PG1 == 0 & CHAR_PG0 == 0 )
      {
            Port_Set = 8 ;
            return Port_Set ;
      }
      else if ( CHAR_PG2 == 1 & CHAR_PG1 == 1 & CHAR_PG0 == 1 )
      {
            Port_Set = 1 ;
            return Port_Set ;
      }
}

Thanks for your answers.

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

You can use my example and replace GetJoyStickDigital with GetDigitalInput

        b = GetDigitalInput ( 1 ) ;  //Port Number 
        b = b | GetDigitalInput ( 2 ) << 1 ;
        b = b | GetDigitalInput ( 3 ) << 2 ;

I knew your were from BEST :-p as you are all calling, and e-mailing the same question.

Also, please don’t post the same question in multiple places.

Sorry about the multiple posts, I just figured that some people only read certain forums and thus trying to get the most looks at it.