IR Sensor Sometimes Receives Correctly, Sometimes Doesn't

I have just got an IR sensor with an IR remote. All I want to do is to get the Arduino board to print the button which is being pushed.

For that, I got the codes of the buttons online, but the problem is, they sometimes get printed, sometimes don't.

For example, I push the button "1", the sensor lights up and 1 gets printed. I push it again, the sensor lights up again, but no 1 is printed this time. (completely random) The sensor always lights up, which means it shouldn't be a problem with the remote or the sensor, but it should be remote sending different values every time.

I really don't know how I can get it to send the same value every time, hope you can help with it.

The code I wrote:

#include <IRremote.h>

IRrecv irrecv(2);
decode_results results;

#define UP 0xFF629D
#define LEFT 0xFF22DD
#define OK 0xFF02FD
#define RIGHT 0xFFC23D
#define DOWN 0xFFA857
#define BUTON1 0xFF6897
#define BUTON2 0xFF9867
#define BUTON3 0xFFB04F
#define BUTON4 0xFF30CF
#define BUTON5 0xFF18E7
#define BUTON6 0xFF7A85
#define BUTON7 0xFF10EF
#define BUTON8 0xFF38C7
#define BUTON9 0xFF5AA5
#define STAR 0xFF42BD
#define BUTON0 0xFF4AB5
#define HASH 0xFF52AD

void setup(){
  Serial.begin(9600);
  irrecv.enableIRIn();
}

void loop(){
  if (irrecv.decode(&results)){
    if (results.value == UP){
      Serial.println("UP");
    }
    if (results.value == LEFT){
      Serial.println("LEFT");
    }
    if (results.value == OK){
      Serial.println("OK");
    }
    if (results.value == RIGHT){
      Serial.println("RIGHT");
    }
    if (results.value == DOWN){
      Serial.println("DOWN");
    }
    if (results.value == BUTON1){
      Serial.println("1");
    }
    if (results.value == BUTON2){
      Serial.println("2");
    }
    if (results.value == BUTON3){
      Serial.println("3");
    }
    if (results.value == BUTON4){
      Serial.println("4");
    }
    if (results.value == BUTON5){
      Serial.println("5");
    }
    if (results.value == BUTON6){
      Serial.println("6");
    }
    if (results.value == BUTON7){
      Serial.println("7");
    }
    if (results.value == BUTON8){
      Serial.println("8");
    }
    if (results.value == BUTON9){
      Serial.println("9");
    }
    if (results.value == STAR){
      Serial.println("STAR");
    }
    if (results.value == BUTON0){
      Serial.println("0");
    }
    if (results.value == HASH){
      Serial.println("HASH");
    }
    irrecv.resume();
  }
}

Instead of all those IF's, use a switch statement instead:

void loop() {
  if (irrecv.decode(&results)) {
    switch(results.value) {
      case (OK): Serial.println("OK"); break;
      case (STAR): Serial.println("STAR"); break;
      case (HASH): Serial.println("HASH"); break;
      case (UP): Serial.println("UP"); break;
      case (DOWN): Serial.println("DOWN"); break;
      case (LEFT): Serial.println("LEFT"); break;
      case (RIGHT): Serial.println("RIGHT"); break;
      case (BUTON1): Serial.println("BUTON1"); break;
      case (BUTON2): Serial.println("BUTON2"); break;
      case (BUTON3): Serial.println("BUTON3"); break;
      case (BUTON4): Serial.println("BUTON4"); break;
      case (BUTON5): Serial.println("BUTON5"); break;
      case (BUTON6): Serial.println("BUTON6"); break;
      case (BUTON7): Serial.println("BUTON7"); break;
      case (BUTON8): Serial.println("BUTON8"); break;
      case (BUTON9): Serial.println("BUTON9"); break;
      case (BUTON0): Serial.println("BUTON0"); break;
      default:
        //The default block is executed if there is no matching "case"
        Serial.print("Unknown: ");
        Serial.println(results.value, HEX);
        break;
    }
    irrecv.resume();
  }
}

Doing it this way will catch all button presses and make it easier to debug the problem.

Okay so doing this helped me get the unknown values but mostly they are all different! I would assign more than one value to BUTTON1 but as it is nearly always random, that looks impossible...

How do people accomplish this perfectly?

Sounds like the library is not compatible with the remote, the value for a single button should not change and become a random value.

70% of the time, it works accurately but the rest is random values. I strongly believe the library is compatible as the remote and the receiver is very commonly used and sold.

Try to press BUTTON1 multiple times, and check if there is a pattern in the output. If the code send and the code received does not match in size, this could cause a "rotary" value, example sequence: BUTTON1 random random random random BUTTON1 and so on..

Can it be happening because I am using an Arduino Mega 2560?