IRremote Code Thoughts

So I tried using the sample code for the IR Remote from the tutorial and no matter what I did I could not get it to work. I stumbled across a bit of code that would decode HEX number for each button. I then wrote the code below to produce an output in the serial monitor. It is not pretty but it gets the job done. I would like to get others thoughts on this bit of code.

#include "IRremote.h"

IRrecv IR(11);
int ledPin = 8;

void setup() 
{
 IR.enableIRIn();
 pinMode(ledPin, OUTPUT);
 Serial.begin(9600);
}

void loop() 
{
 if(IR.decode())
 {
   if(IR.decodedIRData.decodedRawData == 0xBA45FF00){Serial.println("POWER");}
   if (IR.decodedIRData.decodedRawData ==0xB847FF00) {Serial.println("FUNC/STOP");}
   if (IR.decodedIRData.decodedRawData == 0xB946FF00) {Serial.println("VOL+");}
   if (IR.decodedIRData.decodedRawData == 0xBB44FF00) {Serial.println("FAST BACK");}
   if (IR.decodedIRData.decodedRawData == 0xBF40FF00) {Serial.println("PAUSE");}
   if (IR.decodedIRData.decodedRawData == 0xBC43FF00) {Serial.println("FAST FORWARD");}
   if (IR.decodedIRData.decodedRawData == 0xF807FF00) {Serial.println("DOWN");}
   if (IR.decodedIRData.decodedRawData == 0xEA15FF00) {Serial.println("VOL-");}
   if (IR.decodedIRData.decodedRawData == 0xF609FF00) {Serial.println("UP");}
   if (IR.decodedIRData.decodedRawData == 0xE619FF00) {Serial.println("EQ");}
   if (IR.decodedIRData.decodedRawData == 0xF20DFF00) {Serial.println("ST/REPT");}
   if (IR.decodedIRData.decodedRawData == 0xE916FF00) {Serial.println("0");}
   if(IR.decodedIRData.decodedRawData == 0xF30CFF00) {Serial.println("1");}
   if(IR.decodedIRData.decodedRawData == 0xE718FF00) {Serial.println("2");}
   if(IR.decodedIRData.decodedRawData == 0xA15EFF00) {Serial.println("3");}
   if(IR.decodedIRData.decodedRawData == 0xF708FF00) {Serial.println("4");}
   if(IR.decodedIRData.decodedRawData == 0xE31CFF00) {Serial.println("5");}
   if(IR.decodedIRData.decodedRawData == 0xA55AFF00) {Serial.println("6");}
   if(IR.decodedIRData.decodedRawData == 0xBD42FF00) {Serial.println("7");}
   if(IR.decodedIRData.decodedRawData == 0xAD52FF00) {Serial.println("8");}
   if(IR.decodedIRData.decodedRawData == 0xB54AFF00) {Serial.println("9");}

   delay(1000);
   IR.resume();
 }
}

#include "IRremote.h"

IRrecv IR(11);
const int ledPin = 8;

void setup()
{
  IR.enableIRIn();
  pinMode(ledPin, OUTPUT);
  Serial.begin(115200);
}

void loop()
{
  if (IR.decode())
  {
    uint16_t Code = IR.decodedIRData.decodedRawData >> 16;
    switch (Code) {
      case (0xBA45) : Serial.println("POWER"); break;
      case (0xB847) : Serial.println("FUNC/STOP"); break;
      case (0xB946) : Serial.println("VOL+"); break;
      case (0xBB44) : Serial.println("FAST BACK"); break;
      case (0xBF40) : Serial.println("PAUSE"); break;
      case (0xBC43) : Serial.println("FAST FORWARD"); break;
      case (0xF807) : Serial.println("DOWN"); break;
      case (0xEA15) : Serial.println("VOL-"); break;
      case (0xF609) : Serial.println("UP"); break;
      case (0xE619) : Serial.println("EQ"); break;
      case (0xF20D) : Serial.println("ST/REPT"); break;
      case (0xE916) : Serial.println("0"); break;
      case (0xF30C) : Serial.println("1"); break;
      case (0xE718) : Serial.println("2"); break;
      case (0xA15E) : Serial.println("3"); break;
      case (0xF708) : Serial.println("4"); break;
      case (0xE31C) : Serial.println("5"); break;
      case (0xA55A) : Serial.println("6"); break;
      case (0xBD42) : Serial.println("7"); break;
      case (0xAD52) : Serial.println("8"); break;
      case (0xB54A) : Serial.println("9"); break;
    }
    IR.resume();
  }
}
1 Like

Can you explain to me what "case" and "break mean"? Plus it did not work. I got "?" for every button I pushed. Thank you for your time.

Try removing the parentheses

case 0xBA45 : Serial.println("POWER"); break;
case 0xB847 : Serial.println("FUNC/STOP"); break;

Search Switch statement to understand the case and break

The Arduino language reference has an entry for the switch-case structure.

no matter

Please read more about how to set the serial monitor's baud rate to match the one in the sketch.

I gave it a shot but no dice buddy. Is there anything wrong with how I did my original code that I was able to get to work?

you say second time your code is working as expected. so what solution do you looking for?

1 Like

I apologize for the confusion. The original code I posted that is nothing like the tutorial produces the desired result by printing the designation of the button in the serial monitor when the button is pressed. The code you gave me which is very similar to the code in the tutorial does not work for me and I cannot quite figure out why. I also want to know if there is anything wrong with the way I did it in the code I posted. Thank you for your time. I am very new to this and coding.

nothing wrong if it works. if you compare you will note that i have replaced if construction with switch construction, they are working identical, looks better. the second thing i made the received data is half shorter by removing similar part "0xFF00". it save processor time. last thing - delay() is removed.
but in no place of code is serial output "?" and if you see it than something wrong with setup. could you show picture from serial monitor?

and output of this

#include "IRremote.h"

IRrecv IR(11);
const int ledPin = 8;

void setup(){
  IR.enableIRIn();
  pinMode(ledPin, OUTPUT);
  Serial.begin(115200);
}

void loop(){
  digitalWrite(ledPin, LOW);
  if (IR.decode()){
    digitalWrite(ledPin, HIGH);
    uint16_t Code = IR.decodedIRData.decodedRawData >> 16;
    Serial.println(Code, HEX);
    IR.resume();
  }
}

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