I have a Duplicate Case Value and cannot figure out why

I'm trying to control a 28BYJ-48 stepper motor with an IR remote, and up until now I've had success getting it to move, but now I can't get rid of this error.

Below is my code

#include <IRremote.h>
#include <Stepper.h>
int RECV_PIN = 3;
IRrecv irrecv(RECV_PIN);
int steps = 2038;
Stepper myStepper = Stepper(steps, 8, 10, 9, 11);

void setup() {
  //remote
  Serial.begin(9600);
  irrecv.enableIRIn(); 
  IrReceiver.begin(RECV_PIN);
}

void loop() {
  
 if (IrReceiver.decode()) {
    Serial.println(IrReceiver.decodedIRData.decodedRawData, HEX);
    delay(1500);
    switch(IrReceiver.decodedIRData.decodedRawData, HEX){
      case 0xE31CFF00:
                     myStepper.setSpeed(500);
                     steps = 2048;
                     delay(2000);
                     break;
     case 0xE718FF00:
                     myStepper.setSpeed(0);
                     steps = 0;
                     delay(2000);
                     break;
     case 0xAD52FF00:
                     myStepper.setSpeed(500);
                     steps = -2048;
                     delay(2000);
                     break;
    }
    IrReceiver.resume();
 }

}


Here is the error message.
Arduino: 1.8.19 (Windows 10), Board: "Arduino Uno"

C:Documents\Arduino\IR_Test\IR_Test.ino:21:7: note: previously used here

   case 0xE31CFF00:

   ^~~~

exit status 1

duplicate case value

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

Please do not post pictures of text. Copy and paste the error message into your post, between code tags.

Why did you put the "HEX" there. Have you ever seen that in any other code?

Despite the fact that you wrote things in hexidecimal in your sketch, to the processor it is ALL binary. When you are using Serial.print then you can put that "HEX" out there to tell print to use hexidecimal when it prints. But you can't just stick "HEX" wherever you want.

Lose the ", HEX" first. That evaluates to whatever HEX is because of your inadvertent use of the comma operator.

Next

The case value must be either int or char type.

so I don't think those long constants can fly here.

a7

Look very carefully at this statement. This is the source of your problem.

what microcontroller are you using?
on a UNO you get a warning

warning: overflow in implicit constant conversion [-Woverflow]
       case 0xE31CFF00:
       ^~~~

because switch(var) var is an integer

suggest you use an if() statement comparing unsigned long ints

Edit: think @van_der_decken has identified the problem

@jbherb if you did not get a warning, it is because you have not yet gone to the IDE preferences to dial up all verbosity and warnings.

Do it now. Right now. Then whenever you verify or upload, heed the red ink. Most warnings are easily avoided, and many warnings are really telling you things you need to pay attention to.

Why they are not turned on by default has always baffled me.

a7

Because the core code throws so many compiler warnings that it can sometimes be nearly impossible to scroll through and find the error that actually relates to your code.

If they would correct some of the issues in the core then I would agree with you. But as of right now leaving the warnings all the way up creates a lot of extra distracting stuff every time I compile.

I've said this before and I have yet to hear anyone give a good excuse for why it shouldn't be right. "Core code should not throw warnings!"

LOL. I guess I am better at ignoring noise. So good, in fact, that I often fail to see my own mistakes what spill the red ink. Until I'm grasping at straws and pulling a few remaining hairs off my head.

THX, I do believe I've asked and you've answered in the past. Obvsly it does not the hold in my memory.

a7

I end up switching them on and off. When I'm writing basic code I'll usually have the warnings to a minimum until I'm chasing a bug. Then I'll crank it up to "All" and start going through them. If I'm writing code to share like a library or something, then I'll kick them up right at the end as a sort of check to make sure I didn't miss something.

1 Like

Thanks,
I took the HEX out, and it stopped giving me that error. You also mentioned the case values. How should I set them up using the decoded raw data?

I don't know what you mean. The only error you revealed was the duplicate case label error, which would not have been solved with that edit.

Since you are still asking about the real problem, I assume you are still getting the duplicate case label error.

I don't know the best what to proceed, but it looks like your case label are unique in the high order 16 bits, so

    unsigned long longIRCode = IrReceiver.decodedIRData.decodedRawData >> 16;
    unsigned int intIRCode = longIRCode;

    switch (intIRCode) {
      case 0xE31C:
                     myStepper.setSpeed(500);
                     steps = 2048;
                     delay(2000);
                     break;
     case 0xE718:
                     myStepper.setSpeed(0);
                     steps = 0;
                     delay(2000);
                     break;
     case 0xAD52:
                  

would just use the high order bits. Use better names than I did, and maybe print out the values - I'm under the umbrella just now or I would test my suggestion.

a7

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