How do create a Switch case for this?

I would like to reform this if-else using Switch case statement. Please do help me...

void loop() {
 
  int value1 = pulseIn(channel_1, HIGH); // Receiving signal from channel 1 to variable value1
  int value2 = pulseIn(channel_2, HIGH); // Receiving signal from channel 2 to variable value2
  int value3 = pulseIn(channel_3, HIGH); // Receiving signal from channel 3 to variable value3
  int value4 = pulseIn(channel_4, HIGH); // Receiving signal from channel 4 to variable value4
  int value5 = pulseIn(channel_5, HIGH); // Receiving signal from channel 5 to variable value5

if(value1 == 0, value2 == 0, value3 == 0, value4 == 0, value5 == 0 ) //Initial all values to 0
  {
    nomov();
  }
  
else if( value1 > 1530) //Max reverse value on RC channel 1
  {
    pwm = map(value1, 1530, 2000, 0, 255);
    forward();
  }
  
else if(value1 < 1460) //Max forward value on RC channel 1
  {
     pwm = map(value1, 1460, 1000, 0, -255);
     
     if (pwm < 0)
     {
       reverse();
     }
  }

 else if (value2 < 1460) //Max forward value on RC channel 2
  {
     
      pwm = map(value2, 1460, 1000, 0, -255); // Mapping value to pwm
      pwm1 = -pwm;

      if(pwm<0)
      {
        rtank();
      }
  }

 else if (value2 > 1530) //Max reverse value on RC channel 2
  {
      
     pwm = map(value2, 1530, 2000, 0, -255); // Mapping value to pwm
     pwm1 = -pwm;

      if(pwm<0)
      {
        ltank();
      }
  }

  else
  {
    nomov();
  }
delay(20); 
}

Use the 'or' operator:
if(value1 == 0 || value2 == 0 || value3 == 0 || value4 == 0 || value5 == 0 ) // If any value is 0

Using range in switch case in C/C++ - GeeksforGeeks

It is not standard C++ though

Does it work with Arduino?

Edit: Works just fine

uint8_t val;
uint8_t min_val = 0;
uint8_t max_val = 255;

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

void loop() {
  val = random(min_val, max_val);
  switch (val) {
    case 0 ... 127:
      Serial.print("0 - 127: ");
      break;
    case 128 ... 255:
      Serial.print("128 - 255: ");
      break;
  }
  Serial.println(val);
  delay(1000);
}
128 - 255: 141
128 - 255: 155
0 - 127: 125
128 - 255: 158
0 - 127: 16
0 - 127: 54
128 - 255: 157
0 - 127: 56
0 - 127: 53
0 - 127: 118
0 - 127: 49
128 - 255: 163
0 - 127: 35
0 - 127: 84
0 - 127: 116

It is compiler extension, if someone would use different toolset or switch off extension support then it will fail. Bottom line this code won’t be portable.

Fair enough, but it also fulfils OP's request for a switch.

Personally, I'd use a stack of ifs though.

I wonder how compiler handles range case internally, does it generate n number of cases in between or uses if > && if < condition

Given it's likely a switch with more than 6 cases, and the cases are in sequence, then I'd assume it creates a LUT, but it's also possible that it completely optimizes the switch away.
Compile it and check out the ASM to see. For this one it could just make a single if/else.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.