1

I'm trying to set up an arduino infrared sensor and when I try to use a switch statement it returns "duplicate case value". Any suggestions or explanations on this issue? Btw. I'm a total beginner so don't be suprised if I do some stupid mistake.

I tried using int for the val variable but it says there's an overflow so I used double instead. I tried looking at some other problems similar to mine but none of them apply here.

    //code starts here
    void loop() {
      constexpr long double val[10]= 
 {16724175,16718055,16743045,16716015,16726215,16734885,16728765,16730805,16732845,16738455}; //holds all values from infrared sensor
      int results_val;
      if (reciever.decode(&results)){  //not relevant 
        Serial.println(results.value);
        results_val = results.value;
        reciever.resume();
      }
      switch(results_val){ //should check which button was pressed
        case (int) val[0] : Serial.println(1);
        break;
        case (int) val[1] : Serial.println(2);
        break;
        case (int) val[2] : Serial.println(3);
        break;
        case (int) val[3] : Serial.println(4);
        break;
        case (int) val[4] : Serial.println(5);
        break;
        case (int) val[5] : Serial.println(6);
        break;
        case (int) val[6] : Serial.println(7);
        break;
        case (int) val[7] : Serial.println(8);
        break;
        case (int) val[8] : Serial.println(9);
        break;
        case (int) val[9] : Serial.println(0);
        break;
      }
    }
    //code ends here

I expected to get any result; even if wrong; but it returns "duplicate case value"

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
idk
  • 11
  • 1
  • Also, don't use floats. Always work with the assumption that floats will randomly flip the last bit whenever modified. It's not accurate, but it prevents a whole class of errors. – Mooing Duck Jun 12 '19 at 16:31
  • How big is an `int` on an Arduino? If it's 16 bits, those values are too large, and will be truncated mod 65536. That's probably the source of the conflict. Does the error message tell you what the duplicated value is? – Pete Becker Jun 12 '19 at 16:44
  • Hum. Interesting. The numbers are all distinct modulo 65536. What happens if you replace `long double` with just `long` and drop the `(int)` from the labels? – Bathsheba Jun 12 '19 at 16:44
  • if your just using integers, i would rather go with a long instead of a long double. Long double is a double precision floating point not an integer. – Oddmar Dam Jun 12 '19 at 16:45
  • using long solved the problem (thanks btw) but i'm still interested why dis returns an error so if anyone has a theory I'll be more than glad to hear it. – idk Jun 12 '19 at 17:16
  • Switch case supports only integer or char in the label. But you are providing array with the index at the label. let us consider an example. ``` #include int main() { int x = 2; int arr[] = {1, 2, 3}; switch (x) { case arr[0]: printf("Choice 1\n"); case arr[1]: printf("Choice 2\n"); case arr[2]: printf("Choice 3\n"); } return 0; } ``` above code not work. It will give you ` Compiler Error: case label does not reduce to an integer constant ` – Arun Kumar Jun 12 '19 at 17:19

0 Answers0