Problem with code for IR remote controlled robot

I recently wrote the code for this project and when i press a button on the remote nothing shows up on the Serial Monitor and the robot doesn’t function as a result. Please tell me how to fix this!

#include <IRremote.h>
#include <AFMotor.h>
AF_DCMotor rightMotor(1); // Right motor connected to port 1
AF_DCMotor leftMotor(2); // Left motor connected to port 2
IRrecv irrecv(A2);// IR receiver connected to pin A2(ANALOG PINS ON MOTOR SHIELD USED AS DIGITAL INS!)
decode_results results;
#define Straight 0xC1CC42BD
#define Right 0xC1CCA25D
#define Left 0xC1CC22DD
#define Back 0xC1CCC23D

void setup(){
  Serial.begin(9600); // Establishing Serial Communication, THIS IS IMPORTANT!!
  irrecv.enableIRIn(); // Start the receiver
  }
void loop() {
  if (irrecv.decode(&results)){ 
      Serial.println(results.value, HEX);

      if((results.value)== Straight)
      {
        moveForward();
       
      } 
      if((results.value)== Right)
      {
      turnRight();
      
      }
      if((results.value)== Left)
      {
      turnLeft();
      
      }
      if((results.value)== Back)
      {
      moveBackward();
      
      }
      else Stop();
    
    irrecv.resume();
    }
  
}
void moveForward() {
    
    leftMotor.setSpeed(255);
    rightMotor.setSpeed(255);
    leftMotor.run(FORWARD);
    rightMotor.run(FORWARD);
}
    
    
void turnRight(){
  
  leftMotor.setSpeed(200);
  rightMotor.setSpeed(200);
  leftMotor.run(FORWARD);
  rightMotor.run(BACKWARD);
  delay(400);
 
  leftMotor.run(FORWARD);
  rightMotor.run(FORWARD);      
} 

void turnLeft() {
  
  leftMotor.setSpeed(200);
  rightMotor.setSpeed(200);
  leftMotor.run(BACKWARD);
  rightMotor.run(FORWARD);
  delay(400);
  
  leftMotor.run(FORWARD);
  rightMotor.run(FORWARD);
}  
void moveBackward() {
    
    leftMotor.setSpeed(200);
    rightMotor.setSpeed(200);
    leftMotor.run(BACKWARD);
    rightMotor.run(BACKWARD);
}

Hello,

the first Thing I would do is to put code in the else that Serial prints the value received.

You may well be receiving a Signal but not one that you are expecting and there could be a number of reasons for that.

The first Thing you Need to know is, are you receiving NOTHING or SOMETHING. From there you work on interpreting what you have received.

Sounds like the IR receiver is not wired correctly or something else is maybe connected to Pin A2 causing interference. Or maybe your remote's battery has died.

It’s definitely not a hardware problem because when I upload the sketch below i get the hexadecimal values on the Serial Monitor and it works. Please help!

IRrecv irrecv(A2);

decode_results results;

void setup()
{
  Serial.begin(9600);
  irrecv.enableIRIn(); // Start the receiver
}

void loop() {
  if (irrecv.decode(&results)) {
    Serial.println(results.value, HEX);
    irrecv.resume(); // Receive the next value
  }
  delay(100);
}

Put these two lines at the bottom of setup().

  Serial.println("In Setup");
  delay(1000);

If that message shows up after your sketch has started it means a power drain or electrical noise has caused your board to reset. If starting the motors is causing a reset that could prevent the serial output from getting to your PC.