Using Switch Case with a boolean value

Hello
I'm receiving error with Switch Case using a boolean value. In the four "case" decision, only one will be true

Error received:
Compilation error: the value of 'T1' is not usable in a constant expression`

int T1 = 1;
int T2 = 2;
int T3 = 3;
int T4 = 4;

void DecodeKey(){
   int value = analogRead(ANALOG_PIN);
   if (value>20){
       T1 = abs(value-SW1);
       T2 = abs(value-SW2);
       T3 = abs(value-SW3);
       T4 = abs(value-SW4);

   switch (true){
        case (T1 < T2):
            SW = 1;
            break;
        case (T2 < T3):
            SW = 2;
            break;
        case (T3 <T4):
           SW  =3;
           break;
        case (T3 <T4):
            SW  = 4;
            break;
      }

Martin

assuming that was a correct way to write this and use switch/case (it's not) - what would you expect this to do if you have the following order T1 < T2 < T3 < T4 ➜ which 'case' would be executed in your mind? just one (which one, all the conditions are true) or all of them? also you have twice the case (T3 <T4):... so which one should the compiler pick ?

the "right" way would be more something like this with an else only if you want the conditions to be exclusive from one another and in priority order

if (T1 < T2) {
  ...
}
else if (T2 < T3) {
    ...
}
else if (T3 < T4) {
      ...
}

oops

curious ... what are you trying to do?

looks like you're comparing 4 inputs to one another. but couldn't t2 > t1 as well as t4 > t3?

if there's a priority

        if (T1 < T2)
            SW = 1;
        else if (T2 < T3)
            SW = 2;
        else if (T3 <T4)
           SW  =3;
        else if (T3 <T4)
            SW  = 4;
        else
            ; // ???

Thanks for your reply.

The goal is to use an alternate way to decode an analog switch button.. 4 buttons and resistors create a voltage divider attached to the analog port. I read the value and compare the absolute value of the reading - the expected one foe each button. The

Only one case should return a true value.

I know that the if / else if do return a proper value, I had done it.

At the end, this small code is a test to lean if I could use the switch case with different boolean variable for another project.

With a proper selection of resistors, these could work the same on an Uno:


switch(value/256){
     case 0: SW = 1; break;
     case 1: SW = 2; break;
     case 2: SW = 3; break;
     case 3: SW = 4; break;
     default: ; 
}

// or 

SW = 1+ value/256; 

What is SW supposed to be when none of the switches is pressed?

so there are 5 possible voltages (including no button pressed).
there are 5 thresholds that the measured analog voltages is compared to in priority order to determine which switch is pressed.

comparing the input, val, to various thresholds, checking from one end (if val < T4, it's also < T3)

    int val = analogRead(ANALOG_PIN);
    if (T4 > val)
        SW = 1;
    else if (T3 > val)
        SW = 2;
    else if (T2 > val)
       SW  =3;
    else if (T1 > val)
        SW  = 4;
    else
        SW  = 0;

there are various possible resistor/button configurations

1 Like

What is the analogRead value if NO buttons are pressed?
If button 1 is pressed?
If button 2 is pressed?
If button 3 is pressed?
If button 4 is pressed?

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