switch case and logical test

I'm writing a code to restore a relay based flipper with an arduino hw. I then need to test several DI at the same time to identify the following actions. In other word I read a a byte where each bit is setted by a contact and I need to do action if the bit is on.
Instead of a sequence of if statement I thought it is more practical to use the switch case instructions.
The problem I have is that in each case I need to test if a single bit is on or off inside the DI byte .

To test this idea I created the following code but it is in error where I try to compare the incoming byte with a bit mask.

Any idea to solve correctly my problem?

many thanks

// costanti

int incomingByte = 0;
int mask1 = 0x01;
int mask2 = 0x02;
int mask3 = 0x04;
int testbyte = 0;

void setup() {
  // put your setup code here, to run once:

  Serial.begin(9600);

}
void loop()
{

  if (Serial.available() > 0) {
    // read the incoming byte:
    incomingByte = Serial.read();

    // say what you got:
    Serial.print("I received: ");
    Serial.println(incomingByte, HEX);
    testbyte = incomingByte;
  }
  switch (incomingByte) {
    case ((testbyte&&mask1)>0):
      { Serial.println("caso 1 vero");
      }
    case ((testbyte&&mask2)>0):
      { Serial.println("caso 2 vero");
      }
    case ((testbyte&&mask3)>0):
      { Serial.println("caso 3 vero");
      }
      break;
  }




}

test_switch_case.ino (733 Bytes)

int incomingByte = 0;

Clearly, the type of a variable named incomingByte should be long float.

int mask1 = 0x01;
int mask2 = 0x02;
int mask3 = 0x04;
int testbyte = 0;

Pissing away memory uselessly.

    testbyte = incomingByte;
  }
  switch (incomingByte) {
    case ((testbyte&&mask1)>0):

There is NO reason to copy incomingByte to testbyte.

Your case statement evaluates to true or false. A case value MUST be a constant.

There are times when using a switch statement is like wearing a pink tutu to a funeral. You look silly in a tutu.

An example to examine and modify to meet your needs

void setup()
{
  Serial.begin(115200);
}

void loop()
{
  if (Serial.available())
  {
    byte inByte = Serial.parseInt();
    Serial.print("\nReceived : ");
    Serial.println(inByte);
    for (int bit = 0; bit < 8; bit++)
    {
      Serial.print("bit : ");
      Serial.print(bit);
      if (bitRead(inByte, bit))
      {
        Serial.println(" on");
      }
      else
      {
        Serial.println(" off");
      }
    }
  }
}

What part of the error messages do you not understand?

sketch_aug16a.ino:7:5: note: 'int testbyte' is not const
sketch_aug16a:33: error: the value of 'testbyte' is not usable in a constant expression
     case ((testbyte&&mask3)>0):

Each case must be a constant. Those constants are compared to the value in switch().

The switch/case statement is not a good match for your needs. Perhaps 'if' statements would work.

johnwasser:
What part of the error messages do you not understand?

sketch_aug16a.ino:7:5: note: 'int testbyte' is not const

sketch_aug16a:33: error: the value of 'testbyte' is not usable in a constant expression
    case ((testbyte&&mask3)>0):




Each case must be a constant. Those constants are compared to the value in switch().

The switch/case statement is not a good match for your needs. Perhaps 'if' statements would work.

This is what was not clear to me. I wanted to perform a computation in the case statement

many thanks

UKHeliBob:
An example to examine and modify to meet your needs

void setup()

{
  Serial.begin(115200);
}

void loop()
{
  if (Serial.available())
  {
    byte inByte = Serial.parseInt();
    Serial.print("\nReceived : ");
    Serial.println(inByte);
    for (int bit = 0; bit < 8; bit++)
    {
      Serial.print("bit : ");
      Serial.print(bit);
      if (bitRead(inByte, bit))
      {
        Serial.println(" on");
      }
      else
      {
        Serial.println(" off");
      }
    }
  }
}

thanks for the suggestion . This is similar to my other code I tested, but I was attracted by the switch instruction. It seemed to me more elegant but unfortunately it does'nt work as I would like. Now I know.

I am a big fan of switch/case but you must use the right horse for the course. By using a for loop my code has only one if in it and you can, of course, do whatever you want if/when you find a bit set. You could even use switch/case on the bit variable to determine which code or function to execute when you find the bit set or unset.