0

I am working on a project where I've mapped an IR remote control to certain functions. I am currently using a massive switch statement to control the program based on the IR remote button pressed.

Is there a an alternative to using a switch statement, or is a switch the best way to go?

decode_results results; // Initialize the results

IRrecv irrecv(irPin); // Initialize the IR sensor


int buttonPressed;

void setup()
{
  Serial.begin(115200);
  Serial.println("Tile Controller");
  analogReference(DEFAULT);
  irrecv.enableIRIn(); // Start the receiver

  Wire.begin(); // Join the I2C network
}

void loop()
{  
  if (irrecv.decode(&results))
  {
    if(results.value != buttonHeld)
    {
      buttonPressed = results.value;
      Serial.println(buttonPressed,HEX);
    } 

    switch(buttonPressed)
    {
      case 0x3AC5: // 11 - Brightness Up
      {
        if(results.value == buttonHeld)
        {
          Serial.println("Brightness Up Held");
          brightnessUpHeld();
        }
        else 
        {
          Serial.println("Brightness Up Pressed");
          brightnessUpPressed();
        }
        break;
      }

      case 0xFFFFBA45: // 12 - Brightness Down
      {
        if(results.value == buttonHeld)
        {
          Serial.println("Brightness Down Held");    
          brightnessDownHeld();      
        }
        else 
        {
          Serial.println("Brightness Down Pressed");
          brightnessDownPressed();
        }
        break;
      }

      case 0xFFFF827D: // 13 - Pause
      {
        if(results.value == buttonHeld)
        {
          Serial.println("Pause Held");
          pauseHeld();
        }
        else 
        {
          Serial.println("Pause Pressed");
          pausePressed();
        }
        break;
      }

      case 0x2FD: // 14 - Power
      {
        if(results.value == buttonHeld)
        {
          Serial.println("Power Held");
          powerHeld();
        }
        else 
        {
          Serial.println("Pause Pressed");
          powerPressed();
        }
        break;
      }

      case 0x1AE5: // 21 - Red
      {
        if(results.value == buttonHeld)
        {
          Serial.println("Red Held");
          redHeld();
        }
        else 
        {
          Serial.println("Red Pressed");
          redPressed();
        }
        break;
      }

      case 0xFFFF9A65: // 22 - Green
      {
        if(results.value == buttonHeld)
        {
          Serial.println("Green Held");
          greenHeld();
        }
        else 
        {
          Serial.println("Green Pressed");
          greenPressed();
        }
        break;
      }

      case 0xFFFFA25D: // 23 - Blue
      {
        if(results.value == buttonHeld)
        {
          Serial.println("Blue Held");
          blueHeld();
        }
        else 
        {
          Serial.println("Blue Pressed");
          bluePressed();
        }
        break;
      }

      case 0x22DD: // 24 - White
      {
        if(results.value == buttonHeld)
        {
          Serial.println("White Held");
          whiteHeld();
        }
        else 
        {
          Serial.println("White Pressed");
          whitePressed();
        }
        break;
      }

      case 0x2AD5: // 31 - Red 1
      {
        if(results.value == buttonHeld)
        {
          Serial.println("Red One Held");
          redOneHeld();
        }
        else 
        {
          Serial.println("Red One Pressed");
          redOnePressed();
        }
        break;
      }

      case 0xFFFFAA55: // 32 - Green 1
      {
        if(results.value == buttonHeld)
        {
          Serial.println("Green One Held");
          greenOneHeld();
        }
        else 
        {
          Serial.println("Green One Pressed");
          greenOnePressed();
        }
        break;
      }

      case 0xFFFF926D: // 33 - Blue 1
      {
        if(results.value == buttonHeld)
        {
          Serial.println("Blue One Held");
          blueOneHeld();
        }
        else 
        {
          Serial.println("Blue One Pressed");
          blueOnePressed();
        }
        break;
      }

      case 0x12ED: // 34 - White 1
      {
        if(results.value == buttonHeld)
        {
          Serial.println("White One Held");
          whiteOneHeld();
        }
        else 
        {
          Serial.println("White One Pressed");
          whiteOnePressed();
        }
      }

      case 0xAF5: // 41 - Red 2
      {
        if(results.value == buttonHeld)
        {
          Serial.println("Red 2 Held");
          redTwoHeld();
        }
        else 
        {
          Serial.println("Red 2 Pressed");
          redTwoPressed();
        }
        break;
      }

      case 0xFFFF8A75: // 42 - Green 2
      {
        if(results.value == buttonHeld)
        {
          Serial.println("Green 2 Held");
          greenTwoHeld();
        }
        else 
        {
          Serial.println("Green 2 Pressed");
          greenTwoPressed();
        }
        break;
      }

      case 0xFFFFB24D: // 43 - Blue 2
      {
        if(results.value == buttonHeld)
        {
          Serial.println("Blue 2 Held");
          blueTwoHeld();
        }
        else 
        {
          Serial.println("Blue 2 Pressed");
          blueTwoPressed();
        }
        break;
      }

      case 0x32CD: // 44 - White 2
      {
        if(results.value == buttonHeld)
        {
          Serial.println("White 2 Held");
          whiteTwoHeld();
        }
        else 
        {
          Serial.println("White 2 Pressed");
          whiteTwoPressed();
        }
        break;
      }

      case 0x38C7: // 51 - Red 3
      {
        if(results.value == buttonHeld)
        {
          Serial.println("Red 3 Held");
          redThreeHeld();
        }
        else 
        {
          Serial.println("Red 3 Pressed");
          redThreePressed();
        }
        break;
      }

      case 0xFFFFB847: // 52 -  Green 3
      {
        if(results.value == buttonHeld)
        {
          Serial.println("Green 3 Held");
          greenThreeHeld();
        }
        else 
        {
          Serial.println("Green 3 Pressed");
          greenThreePressed();
        }
        break;
      }

      case 0x7887: // 53 - Blue 3
      {
        if(results.value == buttonHeld)
        {
          Serial.println("Blue 3 Held");
          blueThreeHeld();
        }
        else 
        {
          Serial.println("Blue 3 Pressed");
          blueThreePressed();
        }
        break;
      }

      case 0xFFFFF807: // 54 - White 3
      {
        if(results.value == buttonHeld)
        {
          Serial.println("White 3 Held");
          whiteThreeHeld();
        }
        else 
        {
          Serial.println("White 3 Pressed");
          whiteThreePressed();
        }
        break;
      }

      case 0x18E7: // 61 - Red 4
      {
        if(results.value == buttonHeld)
        {
          Serial.println("Red 4 Held");
          redFourHeld();
        }
        else 
        {
          Serial.println("Red 4 Pressed");
          redFourPressed();
        }
        break;
      }

      case 0xFFFF9867: // 62 - Green 4
      {
        if(results.value == buttonHeld)
        {
          Serial.println("Green 4 Held");
          greenFourHeld();
        }
        else 
        {
          Serial.println("Green 4 Pressed");
          greenFourPressed();
        }
        break;
      }

      case 0x58A7: // 63 - Blue 4
      {
        if(results.value == buttonHeld)
        {
          Serial.println("Blue 4 Held");
          blueFourHeld();
        }
        else 
        {
          Serial.println("Blue 4 Pressed");
          blueFourPressed();
        }
        break;
      }

      case 0xFFFFD827: // 64 - White 4
      {
        if(results.value == buttonHeld)
        {
          Serial.println("White 4 Held");
          whiteFourHeld();
        }
        else 
        {
          Serial.println("White 4 Pressed");
          whiteFourPressed();
        }
        break;
      }

      case 0x28D7: // 71 - Red DIY Up
      {
        if(results.value == buttonHeld)
        {
          Serial.println("Red DIY Up Held");
          redDIYUpHeld();
        }
        else 
        {
          Serial.println("Red DIY Up Pressed");
          redDIYUpPressed();
        }
        break;
      }

      case 0xFFFFA857: // 72 - Green DIY Up
      {
        if(results.value == buttonHeld)
        {
          Serial.println("Green DIY Up Held");
          greenDIYUpHeld();
        }
        else 
        {
          Serial.println("Green DIY Up Pressed");
          greenDIYUpPressed();
        }
        break;
      }

      case 0x6897: // 73 - Blue DIY Up
      {
        if(results.value == buttonHeld)
        {
          Serial.println("Blue DIY Up Held");
          blueDIYUpHeld();
        }
        else 
        {
          Serial.println("Blue DIY Up Pressed");
          blueDIYUpPressed();
        }
        break;
      }

      case 0xFFFFE817: // 74 - Quick
      {
        if(results.value == buttonHeld)
        {
          Serial.println("Quick Held");
          quickHeld();
        }
        else 
        {
          Serial.println("Quick Pressed");
          quickPressed();
        }
        break;
      }

      case 0x8F7: // 81 - Red DIY Down
      {
        if(results.value == buttonHeld)
        {
          Serial.println("Red DIY Down Held");
          redDIYDownHeld();
        }
        else 
        {
          Serial.println("Red DIY Down Pressed");
          redDIYDownPressed();
        }
        break;
      }

      case 0xFFFF8877: // 72 - Green DIY Down
      {
        if(results.value == buttonHeld)
        {
          Serial.println("Green DIY Down Held");
          greenDIYDownHeld();
        }
        else 
        {
          Serial.println("Green DIY Down Pressed");
          greenDIYDownPressed();
        }
        break;
      }

      case 0x48B7: // 73 - Blue DIY Down
      {
        if(results.value == buttonHeld)
        {
          Serial.println("Blue DIY Down Held");
          blueDIYDownHeld();
        }
        else 
        {
          Serial.println("Blue DIY Down Pressed");
          blueDIYDownPressed();
        }
        break;
      }

      case 0xFFFFC837: // 84 - Slow
      {
        if(results.value == buttonHeld)
        {
          Serial.println("Slow Held");
          slowHeld();
        }
        else 
        {
          Serial.println("Slow Pressed");
          slowPressed();
        }
        break;
      }

      case 0x30CF: // 84 - DIY 1
      {
        if(results.value == buttonHeld)
        {
          Serial.println("DIY 1 Held");
          diyOneHeld();
        }
        else 
        {
          Serial.println("DIY 1 Pressed");
          diyOnePressed();
        }
        break;
      }

      case 0xFFFFB04F: // 84 - DIY 2
      {
        if(results.value == buttonHeld)
        {
          Serial.println("DIY 2 Held");
          diyTwoHeld();
        }
        else 
        {
          Serial.println("DIY 2 Pressed");
          diyTwoPressed();
        }
        break;
      }

      case 0x708F: // 84 - DIY 3
      {
        if(results.value == buttonHeld)
        {
          Serial.println("DIY 3 Held");
          diyThreeHeld();
        }
        else 
        {
          Serial.println("DIY 3 Pressed");
          diyThreePressed();
        }
        break;
      }

      case 0x708F: // 84 - DIY 3
      {
        if(results.value == buttonHeld)
        {
          Serial.println("DIY 3 Held");
          diyThreeHeld();
        }
        else 
        {
          Serial.println("DIY 3 Pressed");
          diyThreePressed();
        }
        break;
      }

      case 0xFFFFF00F: // 84 - DIY 3
      {
        if(results.value == buttonHeld)
        {
          Serial.println("Auto Held");
          autoHeld();
        }
        else 
        {
          Serial.println("Auto Pressed");
          autoPressed();
        }
        break;
      }

    }
    irrecv.resume(); // Receive the next IR value
  }
}
  • 1
    Can we have a glimpse at the portion of your code in question? This helps us to better understand how the decisions in your program are made and will lead to better answers. – Kwasmich Apr 18 '17 at 9:54
  • 1
    You could have an array of structs, with each struct containing the key code, a name, a "pressed" function pointer and a "held" function pointer. – Mark Smith Apr 18 '17 at 10:02
  • The array of structs sounds like a good idea. I've never used a function pointer before so I guess I've got some learning to do :) – Matt Apr 18 '17 at 10:09
2

Your switch statement isn't that big - I've seen bigger.

However, as @MarkSmith says, using an array of structs with key codes and function pointers would be a way to go. For instance, you may have the struct:

struct button {
    uint32_t keycode;
    void (*press)();
    void (*hold)();
};

Populated with data it would look like:

const struct button buttons[] = {
    { 0x3AC5, brightnessUpPressed, brightnessUpHeld },
    { 0xFFFFBA45, brightnessDownPressed, brightnessDownHeld },
    ... etc ...
    { 0xFFFFF00F, autoPressed, autoHeld },
    { 0, 0, 0 }
};

Note the last entry of { 0, 0, 0 } - this is being used as an "end of list" marker. An alternative is to keep track of how many entries are in the list, but this makes adding / removing entries easier.

Then you can iterate through the list until you find a keycode that matches or the special keycode 0:

for (int i = 0; buttons[i].keycode != 0; i++) {
    if (buttons[i].keycode == buttonPressed) {
        if (results.value == buttonHeld) {
            buttons[i].held();
        } else {
            buttons[i].press();
        }
        break; // We found the button, no need to continue
    }
}

This method decreases the amount of code you have, but has the adverse effect of increasing the amount of RAM you use. It's a tradeoff between making the code simpler and using more RAM. If you can afford the RAM usage it can make things so much easier to read, since you have a simple table for the commands and a simple loop for finding the right entry in the table.

  • Wow, thanks for that reply & code examples. I'm definitely going to give it a try. – Matt Apr 18 '17 at 10:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.