Hope this is OK to post here:

Question about If-ElseIF versus 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.

Code:

#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.

I’d suggest posting this type of question in the “ask the community” forum, rather than general in the future.

I’d suggest using a more meaningful thread title, such as
“Reading multiple digital IN, use IF/THEN/ELSE or Case or what?”

I’d suggest identifying yourself as BEST Robotics, and list your hub, which helps people understand your experience level, use conditions, and helps publicize BEST here in the Vex forums.

Thanks for posting the code you already have, thats a big help.
It looks ok to me as is. Does it seem to work?
You might try being more paranoid about precedence of operations and add parenthesis between the &; ie (var ==1 ) & (var == 1) & (var == 1)
Since your truth table completely covers all possible operations,
you shouldn’t need an “else” at the end, but it is good paranoid practice to put one in, in case someone edits your code and removes a value.

Along similar lines, but simpler code wise.
There are 2^3 = 8 possibilities, so use the natural binary encoding, rather than the one you have chosen.
This is typed freehand, so treat it as pseudo code and figure out your own syntax corrections.

unsigned char Port_set = 0;
  // set CHAR_PG2,1,0 as you have in your code
 IF ( CHAR_PG2 == 1 ) { Port_set = Port_set + 4; } // 2^pg2=4
 IF (CHAR_PG1 == 1 ) { Port_set = Port_set + 2; }  // 2^pg1= 2
 IF ( CHAR_PG0 == 1) { Port_set = Port_set + 1; }  // 2^pg0= 1
return Port_set;  // now port set is a value 0..7 based on digital inputs 

If you become a code-minimization freak like me, you might even try something like this, which assumes that true=1 and false = 0, and that you can multiply a logical operation times a constant in a meaningful way.


return (  (GetDigitalInput(1)==1)*4 + (GetDigitalInput(2)==1)*2 + (GetDigitalInput(3)==1)*1 );

and since that is just one line, you can include it in the main line rather than off in a subroutine.

But really, what you started with looks like it should work fine, even if I don’t understand why you chose that encoding of input to results.

Sorry, I did post it in the EasyC Question forum but wanted to find another place to make sure as many people see it.

Yes, I am the Coach and programming mentor for Thunderstorm Robotics from Payne County Oklahoma, Heartland Trails BEST Hub. Game day is less than 2 weeks away. We don’t even have an actual connection for the digital inputs yet to read in so I haven’t tested the code. I just worked it up this weekend. I have programmed a very little in VB and tried to learn basics of C/C++ and Java, but even then your code examples go over my head. Default, all inputs are ‘1’, and when ground they are ‘0’. So the default case is all 3 inputs return an unsigned char of 1. I see what you are saying about using 0 - 7, but I have only 2 non-programming students doing this this year and I thought it would be easier to return 1 - 8 and then step through a switch case for 8 alternatives to then somehow be visually represented. I hope I’m making sense here.

Thanks for your help though, I’ll work through those examples of yours to see what can be done differently. definitely will use ( ) on the conditions.

Thanks,
Tony Lettkeman
Vision Computer Services
Coach/Mentor for Thunderstorm Robotics
BEST Team #78