Switch Statement For IR Remote Inputs showing Duplicate Cases

Hello Arduino Forums,

Context:

I am using an Arduino Uno, a 2x16 LCD Screen, and an IR Remote. What I want to accomplish with it is that I want to make it so that I can use VOL+/- buttons and CH+/- buttons to change the value for the hours and minutes, then display them onto the LCD Screen's bottom line. When the user inputs their desired time, they would press the EQ button to finalize their decision, and exit the switch statement.

Essentially, I'm trying to make it so a user can input a time (whilst seeing the numbers change on the screen as well), and press EQ to finalize.

Problem:
Right now, I'm trying to use a switch statement to accomplish this task. I made it so each case is the (decoded) number code for the remote buttons that need to be pressed in order to fulfill this function. However, when I run this code in TinkerCad, it displays an error that says there is a duplicate case value(case -32641 with all the others).

When I delete case -32641, it runs, but the Serial Monitor only outputs readResults as -32641 whenever I press any button. Does anyone have any advice on how to fix this so it can work properly?

Here is my code:

//LCD code
#include <LiquidCrystal.h>

LiquidCrystal lcd(7, 6, 5, 4, 3, 2);
int hrs = 0;
int mins = 0;

//Remote Code
#include <IRremote.h> //including infrared remote header file
int RECV_PIN = 8; // the pin where you connect the output pin of IR sensor
IRrecv irrecv(RECV_PIN);
decode_results results;

void setup() {

hrs = 0;
mins = 0;

//LCD Code
lcd.begin(16,2);
Serial.begin(9600);

lcd.clear();
lcd.setCursor(0,0);
lcd.print("Time: 17:20");
lcd.setCursor(0,1);
lcd.print("Input alarm time");
delay(1000);

lcd.clear();
lcd.setCursor(0,0);
lcd.print("Time: 17:20");
lcd.setCursor(0,1);
lcd.print("CH+/CH- for mins");
delay(1000);

lcd.clear();
lcd.setCursor(0,0);
lcd.print("Time: 17:20");
lcd.setCursor(0,1);
lcd.print("+/- for hrs");
delay(1000);

lcd.clear();
lcd.setCursor(0,0);
lcd.print("Time: 17:20");

//IR Remote
irrecv.enableIRIn();

}

void loop() {

int readResults = results.value;// Results of decoding are stored in result.value

//Remote input code
if (irrecv.decode(&results)){// Returns 0 if no data ready, 1 if data ready.

int readResults = results.value;// Results of decoding are stored in result.value
Serial.println(readResults);
switch(readResults){
case 16613503 :
{
//VOL+
hrs +=1;
//Makes sure user doesn't input invalid hours
if(hrs > 23){
hrs = 0;
}

Serial.println(hrs + ":" + mins);
}
case 16617583 :
{
//VOL-
hrs -=1;
//Makes sure user doesn't input invalid hours
if(hrs < 0){
hrs = 23;
}

}
case 16601263 :
{
//CH+
mins +=1;

//Makes sure user doesn't input invalid minutes
if(mins > 59){
mins = 0;
}

}
case 16584943 :
{
//CH-
mins -=1;

//Makes sure user doesn't input invalid minutes
if(mins < 0){
mins = 59;
}

}
case 16625743 :
{
//EQ
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Time: 17:20");
lcd.setCursor(0,1);
lcd.print("Input Valid");
break;
}
case -32641 :
{
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Time: 17:20");
lcd.setCursor(0,1);
lcd.print(hrs + ":" + mins);
}

}//End of Switch Statement
}//End of If statement

}

 uint32_t readResults = results.value;

Or, more simply switch ( results.value) {

Please remember to use code tags when posting code.

int readResults

case 16625743 :

What is the maximum and minimum number that can be stored in an int?

PerryBebbington:
int readResultsWhat is the maximum and minimum number that can be stored in an int?

The answer to which is, regretably, as always, "it depends".

TheMemberFormerlyKnownAsAWOL:

 uint32_t readResults = results.value;

Or, more simply

switch ( results.value) {

Please remember to use code tags when posting code.

This seems to have solved the case duplication error. However, the results.value still seems to always become -32641 after any button press. To add to this, it just seems to continually loop like this, and doesn't display hrs or mins on the board (but does show the ":" ).

However, the results.value still seems to always become -32641 after any button press.

uint32_t is an unsigned type - it can't ever be negative.

You need to show your code.

Dat username tho...

TheMemberFormerlyKnownAsAWOL:
uint32_t is an unsigned type - it can't ever be negative.

You need to show your code.

//LCD code
#include <LiquidCrystal.h>

LiquidCrystal lcd(7, 6, 5, 4, 3, 2);  
int hrs = 0;
int mins = 0;

//Remote Code
#include <IRremote.h> //including infrared remote header file
int RECV_PIN = 8; // the pin where you connect the output pin of IR sensor
IRrecv irrecv(RECV_PIN);
decode_results results;


void setup() { 
 
  hrs = 0;
  mins = 0;
  
 //LCD Code
 lcd.begin(16,2); 
 Serial.begin(9600);
 
 lcd.clear();
 lcd.setCursor(0,0); 
 lcd.print("Time: 17:20"); 
 lcd.setCursor(0,1);
 lcd.print("Input alarm time");
 delay(1000);
  
 lcd.clear();
 lcd.setCursor(0,0); 
 lcd.print("Time: 17:20"); 
 lcd.setCursor(0,1);
 lcd.print("CH+/CH- for mins");
 delay(1000);  
   
 lcd.clear();
 lcd.setCursor(0,0); 
 lcd.print("Time: 17:20"); 
 lcd.setCursor(0,1);
 lcd.print("+/- for hrs");
 delay(1000);
  
 lcd.clear();
 lcd.setCursor(0,0); 
 lcd.print("Time: 17:20");
  
 //IR Remote
 irrecv.enableIRIn();
  
}

void loop() {  
  
  uint32_t readResults = results.value;// Results of decoding are stored in result.value
  
  //Remote input code
  if (irrecv.decode(&results)){// Returns 0 if no data ready, 1 if data ready.
  	
    int readResults = results.value;// Results of decoding are stored in result.value
    Serial.println(readResults);
    switch ( results.value) {
      case 16613503 :
      {
        //VOL+
        hrs +=1;   
        //Makes sure user doesn't input invalid hours
        if(hrs > 23){
            hrs = 0;
        }

        Serial.println(hrs + ":" + mins);
      }
      case 16617583 :
      {
        //VOL-
        hrs -=1;
        //Makes sure user doesn't input invalid hours
        if(hrs < 0){
            hrs = 23;
        }

      }
      case 16601263 :
      {
        //CH+
        mins +=1;

       //Makes sure user doesn't input invalid minutes
        if(mins > 59){
            mins = 0;
        }

      }
      case 16584943 :
      {
        //CH-
        mins -=1;

        //Makes sure user doesn't input invalid minutes
        if(mins < 0){
            mins = 59;
        }

      }
      case 16625743 :
      {
        //EQ
        lcd.clear();
        lcd.setCursor(0,0); 
        lcd.print("Time: 17:20"); 
        lcd.setCursor(0,1);
        lcd.print("Input Valid");
        break;
      }
      case -32641 : 
      {
      	lcd.clear();
        lcd.setCursor(0,0); 
        lcd.print("Time: 17:20"); 
        lcd.setCursor(0,1);
        lcd.print(hrs + ":" + mins);
      }
    
    }//End of Switch Statement
  }//End of If statement
  
}

This is my code.

You're printing the wrong value.

Replaced it. Here is the code with the correct code. It still does the exact same thing as before, though.

Here is the code printing results.value instead of readResults:

//LCD code
#include <LiquidCrystal.h>

LiquidCrystal lcd(7, 6, 5, 4, 3, 2);  
int hrs = 0;
int mins = 0;

//Remote Code
#include <IRremote.h> //including infrared remote header file
int RECV_PIN = 8; // the pin where you connect the output pin of IR sensor
IRrecv irrecv(RECV_PIN);
decode_results results;


void setup() { 
 
  hrs = 0;
  mins = 0;
  
 //LCD Code
 lcd.begin(16,2); 
 Serial.begin(9600);
 
 lcd.clear();
 lcd.setCursor(0,0); 
 lcd.print("Time: 17:20"); 
 lcd.setCursor(0,1);
 lcd.print("Input alarm time");
 delay(1000);
  
 lcd.clear();
 lcd.setCursor(0,0); 
 lcd.print("Time: 17:20"); 
 lcd.setCursor(0,1);
 lcd.print("CH+/CH- for mins");
 delay(1000);  
   
 lcd.clear();
 lcd.setCursor(0,0); 
 lcd.print("Time: 17:20"); 
 lcd.setCursor(0,1);
 lcd.print("+/- for hrs");
 delay(1000);
  
 lcd.clear();
 lcd.setCursor(0,0); 
 lcd.print("Time: 17:20");
  
 //IR Remote
 irrecv.enableIRIn();
  
}

void loop() {  
  
  uint32_t readResults = results.value;// Results of decoding are stored in result.value
  
  //Remote input code
  if (irrecv.decode(&results)){// Returns 0 if no data ready, 1 if data ready.
  	
    int readResults = results.value;// Results of decoding are stored in result.value
    Serial.println(results.value);
    switch ( results.value) {
      case 16613503 :
      {
        //VOL+
        hrs +=1;   
        //Makes sure user doesn't input invalid hours
        if(hrs > 23){
            hrs = 0;
        }

        Serial.println(hrs + ":" + mins);
      }
      case 16617583 :
      {
        //VOL-
        hrs -=1;
        //Makes sure user doesn't input invalid hours
        if(hrs < 0){
            hrs = 23;
        }

      }
      case 16601263 :
      {
        //CH+
        mins +=1;

       //Makes sure user doesn't input invalid minutes
        if(mins > 59){
            mins = 0;
        }

      }
      case 16584943 :
      {
        //CH-
        mins -=1;

        //Makes sure user doesn't input invalid minutes
        if(mins < 0){
            mins = 59;
        }

      }
      case 16625743 :
      {
        //EQ
        lcd.clear();
        lcd.setCursor(0,0); 
        lcd.print("Time: 17:20"); 
        lcd.setCursor(0,1);
        lcd.print("Input Valid");
        break;
      }
      case -32641 : 
      {
      	lcd.clear();
        lcd.setCursor(0,0); 
        lcd.print("Time: 17:20"); 
        lcd.setCursor(0,1);
        lcd.print(hrs + ":" + mins);
      }
    
    }//End of Switch Statement
  }//End of If statement
  
}

Only one of your cases has a break. Each case should have a break otherwise it falls through to the next case, and the next...

ToddL1962:
Only one of your cases has a break. Each case should have a break otherwise it falls through to the next case, and the next...

Ok, thanks for the advice. It's not doing what it was before, which is good. However, once I press one button, the serial monitor seems to output the same button code over and over again, no matter what i press after. Also, the lcd outputs "Input Valid" despite me not pressing EQ, and starts to bug out more and more with each loop iteration.
Here's the updated code:

//LCD code
#include <LiquidCrystal.h>

LiquidCrystal lcd(7, 6, 5, 4, 3, 2);  
int hrs = 0;
int mins = 0;

//Remote Code
#include <IRremote.h> //including infrared remote header file
int RECV_PIN = 8; // the pin where you connect the output pin of IR sensor
IRrecv irrecv(RECV_PIN);
decode_results results;


void setup() { 
 
  hrs = 0;
  mins = 0;
  
 //LCD Code
 lcd.begin(16,2); 
 Serial.begin(9600);
 
 lcd.clear();
 lcd.setCursor(0,0); 
 lcd.print("Time: 17:20"); 
 lcd.setCursor(0,1);
 lcd.print("Input alarm time");
 delay(1000);
  
 lcd.clear();
 lcd.setCursor(0,0); 
 lcd.print("Time: 17:20"); 
 lcd.setCursor(0,1);
 lcd.print("CH+/CH- for mins");
 delay(1000);  
   
 lcd.clear();
 lcd.setCursor(0,0); 
 lcd.print("Time: 17:20"); 
 lcd.setCursor(0,1);
 lcd.print("+/- for hrs");
 delay(1000);
  
 lcd.clear();
 lcd.setCursor(0,0); 
 lcd.print("Time: 17:20");
  
 //IR Remote
 irrecv.enableIRIn();
  
}

void loop() {  
  
  uint32_t readResults = results.value;// Results of decoding are stored in result.value
  
  //Remote input code
  if (irrecv.decode(&results)){// Returns 0 if no data ready, 1 if data ready.
  	
    int readResults = results.value;// Results of decoding are stored in result.value
    Serial.println(results.value);
    switch ( results.value) {
      case 16613503 :
      {
        //VOL+
        hrs +=1;   
        //Makes sure user doesn't input invalid hours
        if(hrs > 23){
            hrs = 0;
        }

        lcd.clear();
        lcd.setCursor(0,0); 
        lcd.print("Time: 17:20"); 
        lcd.setCursor(0,1);
        lcd.print(hrs + ":" + mins);
        delay(1000);
        break;
      }
      case 16617583 :
      {
        //VOL-
        hrs -=1;
        //Makes sure user doesn't input invalid hours
        if(hrs < 0){
            hrs = 23;
        }

        lcd.clear();
        lcd.setCursor(0,0); 
        lcd.print("Time: 17:20"); 
        lcd.setCursor(0,1);
        lcd.print(hrs + ":" + mins);
        delay(1000);
        break;
      }
      case 16601263 :
      {
        //CH+
        mins +=1;

       //Makes sure user doesn't input invalid minutes
        if(mins > 59){
            mins = 0;
        }
		lcd.clear();
        lcd.setCursor(0,0); 
        lcd.print("Time: 17:20"); 
        lcd.setCursor(0,1);
        lcd.print(hrs + ":" + mins);
        delay(1000);
        break;
      }
      case 16584943 :
      {
        //CH-
        mins -=1;

        //Makes sure user doesn't input invalid minutes
        if(mins < 0){
            mins = 59;
        }

        lcd.clear();
        lcd.setCursor(0,0); 
        lcd.print("Time: 17:20"); 
        lcd.setCursor(0,1);
        lcd.print(hrs + ":" + mins);
        delay(1000);
        break;
      }
      case 16625743 :
      {
        //EQ
        lcd.clear();
        lcd.setCursor(0,0); 
        lcd.print("Time: 17:20"); 
        lcd.setCursor(0,1);
        lcd.print("Input Valid");
        delay(1000);
        break;
      }
    
    }//End of Switch Statement
  }//End of If statement
  
}

I've attached a screenshot of what's happening.

As you can see, the "Input Valid" is moving to the left and off screen, and the serial monitor has the same button input code for each loop iteration no matter what buttons I'm pressing.

Take a close look at the example code for the IRremote library (e.g. IRrecvDemo), then a close look at your code, and see if you've missed something out.

TheMemberFormerlyKnownAsAWOL:
Take a close look at the example code for the IRremote library (e.g. IRrecvDemo), then a close look at your code, and see if you've missed something out.

Ok, after some tinkering, I figured out that I was missing the irrecv.resume(); Line at the after the switch statement. Thank you all for the help.

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