Switch Case ..I Just don't get it)

Hello

I did used a ton of the similar function many years ago in Excel, but I just don't get it using C++ I did look at a ton of example, but after three hours, time to ask for help and go to bed.

What is the proper structure to write a switch case code?
How can I switch for a Resitance value greater or minus a define target?

A resistance is calculated using a an output voltage and a known resiatnce.
Now, I want to use different set of parameter depending on the value of the Resistance

 long Resistance = (V_Out_0 *  R_Pad)/(V_IN - V_Out_0);
  
  switch (Resistance){
  case  (Resistance >= 60000):
    float LogResitance = log(Resistance);
    float temp = 1 / (A_MIN + (B_MIN*LogResistance) + (C_MIN * LogResitance * LogResitance * LogResitance);

case  (Resistance < 60000) && (Resistance > 25000)
    float LogResitance = log(Resistance);
    float temp = 1 / (A_MID + (B_MID*LogResistance) + (C_MID * LogResitance * LogResitance * LogResitance);
    break;

    Celsius = temp - Kelvin;
}

Any help appreciated

Martin

Switch / Case is for single values. Allowed data types: int, char

Why don't you just use an if statement?


if (Resistance >= 60000)
  // Do stuff
else if (Resistance < 60000 && Resistance > 25000)
  // Do other stuff
else
  // Do default stuff

Where did you get this syntax from :thinking:

The reason is that you declare a variable inside the case without bracket. See how to solve it in Switch Case statement does not work correctly

unsigned long resistance;

void decide()
{
  switch (resistance) {
    case 0 ... 24999:
      // do something
      break;
    case 25000 ... 60000:
      // do somehting
      break;
    default:
      // do something
      break;
  }
}

void setup()
{
  decide();
}
void loop()
{

}

PS: for two thresholds like in this example, I would just use and if - else...

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