Switch case questions

Hi all. I am wondering if you can use a switch case with multiple variables. IE

switch (button,switch) {

case (12,LOW):
lcd.print("1"); button=0; //print then clear button to 0
break;

case (10,HIGH):
lcd.print("2"); button=0; //print then clear button to 0
break;

default:
// hop up and down because there is nothing here to do
break;

The idea is that I have a keypad that is writing a value that corresponds to the button that was pushed and a switch that is either on or off. They are both written into the variables button and switch. Is this possible or am I constrained to if/else statements

if (button==12 && switch==HIGH)
{//do forward gymnastics here}
if (button==10 && switch ==LOW)
{//do reverse gymnastics here}

pretty sure that my example syntax is wrong but you get the idea of what I am attempting

Thanks for looking
amachinetech

No.

with a little math you could

 switch ((button * 10)  + (switch == HIGH) ? 1:0) {

  case (120):
   lcd.print("1"); button=0; //print then clear button to 0
    break;

  case (101):
   lcd.print("2"); button=0; //print then clear button to 0
    break;

  default: 
    // hop up and down because there is nothing here to do
  break;

As zhomeslice demonstrates, you can use a computed value.

However, the entire point of a switch statement is that ideally the compiler can crunch it down to a jump table. So it's best if your values are tightly packed rather than sparse, rather than spaced 10 values apart.

So the short answer is yes you can do it but it takes work around kung fu to "make it work". So it is simpler to use the if/else and save the switch/case for another day.
Thanks for looking all

amachinetech

You might want to give a slightly more real-life example so it's a little clearer what you need to achieve but an array of function pointers comes to mind (maybe in combination with a struct).

The below uses a struct with two elements; the first one is the buttonvalue (number) and the second one is a pointer to a function that needs to be executed. Written to give you an idea what can be done

First you can define the struct

struct BUTTONACTION
{
  byte number;
  void (*action)(byte switchstate);
};

The action is a pointer to a function that takes the 'switchstate' as a parameter

Next you need to write the functions.

void button12(byte switchstate)
{
  if(switchstate == HIGH)
  {
    lcd.print("1");
  }
}

void button10(byte switchstate)
{
  if(switchstate == LOW)
  {
    lcd.print("2");
  }
}

Now you can populate an array of structs with buttons and associated functions

BUTTONACTION buttonactions[] =
{
  {12, button12},
  {10, button10},
  {11, NULL},
};

For button value 11, there is no function specified so nothing will be executed; just to show how it can be used.

setup() will be your standard setup
and loop() can look like below

  byte buttonval = readKeypad();
  byte switchstate = digitalRead(A0);

  // loop through the possible buttons
  for(int cnt=0;cnt<sizeof(buttonactions)/sizeof(buttonactions[0]);cnt++)
  {
    // if the entered button number matches the nunber in the action table
    if(buttonval == buttonactions[cnt].number)
    {
      // and if there is a function specified
      if(buttonactions[cnt].action != NULL)
      {
        // execute the function
        buttonactions[cnt].action(switchstate);
      }

      // optionally break from for-loop after match
      break;
    }
  }

  // reset the enter button number
  buttonval = 0;
}

You can finetune to your needs :wink:

Total

// variable declarations and defines
...
...

struct BUTTONACTION
{
  ...
  ...
};

// button actions
void button12(byte switchval)
{
  ...
  ...
}

...
...

void setup()
{
  ...
  ...
}

void loop()
{
  ...
  ...
}