在 Case 语句中使用十六进制值

问题描述 投票:0回答:1

嗨,我正在尝试使用遥控器来完成我正在做的项目。我已在 case 语句中输入远程十六进制值,但是收到一条错误,指出“错误:重复的 case 值”。我假设它是因为十六进制值全部以 0x 开头。这不是我的代码,因为我还没有达到自己编码的水平,所以我使用其他人的代码,但是他们在代码中的值是 2数字,但我的遥控器输出十六进制值,或者有什么方法可以从遥控器获取 2 位数字读数?

下面是我在项目中使用的带有十六进制值的代码。(不是开始到结束的代码,而是使用十六进制值的部分)。这个项目正在 arduino 上使用,我正在使用 arduino IDE 来编译它。

void monitorIR(){
  if(IrReceiver.decode()){
    int ircommand = IrReceiver.decodedIRData.command;
    Serial.println(ircommand);
    Serial.println(IrReceiver.decodedIRData.decodedRawData, HEX);
    IrReceiver.resume();
      
    if(lastCommandReceived!=ircommand){
        lastCommandReceived = ircommand;

        //When you press a button on your remote you should see values showing up in the Serial Monitor...
        switch(ircommand){
            // IR UP BUTTON - OPEN FACE
            case 0xE718FF00: // <--- CHANGE THE # IF DIFFERENT 
            if(facePlateCurMode == FACEPLATE_CLOSED){
                facePlateOpenFx();
            }
            break;
            // IR DOWN BUTTON - CLOSE FACE
            case 0xAD52FF00: // <--- CHANGE THE # IF DIFFERENT 
            if(facePlateCurMode == FACEPLATE_OPEN){
                facePlateCloseFx();                    
            }     
            break;
            // IR BUTTON 1 - TURN ON EYES
            case 0xBA45FF00: // <--- CHANGE THE # IF DIFFERENT 
            if(facePlateCurMode == FACEPLATE_CLOSED){
                ledEyesOn();
            }
            break;
            // IR BUTTON 2 - TURN OFF EYES
            case 0xB946FF00: // <--- CHANGE THE # IF DIFFERENT 
            if(facePlateCurMode == FACEPLATE_CLOSED){
                ledEyesOff();
            }
            break; 
            //PLAYING ADDITIONAL SOUNDS
            // IR BUTTON 3 
            case 0xB847FF00: // <--- CHANGE THE # IF DIFFERENT 
                playSoundEffect(SND_JARVIS_EXTRA_A);
            break; 
            // IR BUTTON 4  
            case 0xBB44FF00: // <--- CHANGE THE # IF DIFFERENT 
                playSoundEffect(SND_JARVIS_EXTRA_B);
            break; 
            // IR BUTTON 5    
            case 0xBF40FF00: // <--- CHANGE THE # IF DIFFERENT 
                playSoundEffect(SND_JARVIS_EXTRA_C);
            break; 
            // IR BUTTON 6 
            case 0xBC43FF00:  // <--- CHANGE THE # IF DIFFERENT      
                playSoundEffect(SND_JARVIS_EXTRA_D);
            break; 
            // IR BUTTON 7  
            case 0xF807FF00: // <--- CHANGE THE # IF DIFFERENT      
                playSoundEffect(SND_JARVIS_EXTRA_E);
            break; 
            // IR BUTTON 8 
            case 0xEA15FF00: // <--- CHANGE THE # IF DIFFERENT      
                playSoundEffect(SND_JARVIS_EXTRA_F);
            break; 
        }
         
    } 
  }
}

Below is the code I used to get the readout from the remote before inputting the value into the above code.

#include <Arduino.h>

#include <IRremote.hpp>

const int IR_RECEIVE_PIN = 13;
//const int redPin = 10;

//decode_results results;

void setup()
{
  Serial.begin(9600);
  IrReceiver.begin(IR_RECEIVE_PIN, true); // Start the receiver
  //pinMode(redPin, OUTPUT);
}

void loop(){
  if (IrReceiver.decode()) {
    Serial.println(IrReceiver.decodedIRData.decodedRawData, HEX);
    IrReceiver.printIRResultShort(&Serial);

    switch(IrReceiver.decodedIRData.decodedRawData)
    {
      case 0xE31CFF00: //Keypad button "5"
        Serial.println("5");
        //digitalWrite(redPin, HIGH);
        delay(2000);
        //digitalWrite(redPin, LOW);
        break;
      default:
        break;
    }

    IrReceiver.resume(); // Enable receiving of the next value
  }
}

任何指示或帮助都会很棒。

非常感谢,

马特

我在网上查看过,但找不到任何人遇到此问题或在 Case 语句中使用十六进制,所以我不知道如何解决此问题。

arduino hex case
1个回答
0
投票

switch(i)
根据 i

的数据类型而表现不同
uint32_t i = 54321;

void setup() {
 Serial.begin(115200);
 switch (i) {
  case 0x10001: Serial.println(0x10001, HEX); break;
  case 0x20001: Serial.println(0x20001, HEX); break;
  default: Serial.println(i, HEX); break;
 }
}

void loop() { }

这很好用,但如果 i 是 16 位 int,则大小写常量也被视为 16 位,从而切断较高的部分并导致重复的大小写

© www.soinside.com 2019 - 2024. All rights reserved.