My 2 wheeled remote controlled ir receiver and hc sr04 robot was working fine but now its taking code of only firstly pressed button but not subsequen

my 2 wheeled remote controlled ir receiver and hc sr04 robot was working fine but now its taking code of only firstly pressed button but not subsequent button codes in serial monitor on arduino ide. My code is #include <IRremote.hpp> // Official library (v3.0+)

// Motor Control Pins (L298N)

const uint8_t ENA_PIN = 5;

const uint8_t ENB_PIN = 6;

const uint8_t IN1_PIN = 7;

const uint8_t IN2_PIN = 8;

const uint8_t IN3_PIN = 9;

const uint8_t IN4_PIN = 10;

// HC-SR04 Pins

const uint8_t TRIG_PIN = 12;

const uint8_t ECHO_PIN = 13;

// IR Receiver Pin

const uint8_t IR_RECEIVE_PIN = 11;

// Obstacle Threshold (cm)

const uint16_t OBSTACLE_DISTANCE = 20;

void setup() {

Serial.begin(115200);



// Initialize motor control pins

pinMode(ENA_PIN, OUTPUT);

pinMode(ENB_PIN, OUTPUT);

pinMode(IN1_PIN, OUTPUT);

pinMode(IN2_PIN, OUTPUT);

pinMode(IN3_PIN, OUTPUT);

pinMode(IN4_PIN, OUTPUT);



// Initialize ultrasonic sensor

pinMode(TRIG_PIN, OUTPUT);

pinMode(ECHO_PIN, INPUT);



// Start IR receiver (official library method)

IrReceiver.begin(IR_RECEIVE_PIN, ENABLE_LED_FEEDBACK);



Serial.println(F("Ready to receive IR signals"));

}

void loop() {

// Check for obstacles when moving forward

if (isMovingForward() && checkObstacle()) {

    emergencyStop();

    return;

}



// Handle IR input (official library method)

if (IrReceiver.decode()) {

    handleIRCommand(IrReceiver.decodedIRData.command);

    IrReceiver.resume();  // Enable receiving next value

}

delay(10);

}

// IR Command Handler (using NEC protocol codes)

void handleIRCommand(uint16_t command) {

switch (command) {

    case 0x1B:  // NEC Button 1 (Customize for your remote)

        moveForward();

        break;

    case 0xF:  // NEC Button 2

        moveBackward();

        break;

    case  0xC:  // NEC Button 3

        turnLeft();

        break;

    case 0xE:  // NEC Button 4

        turnRight();

        break;

    case 0xD:  // NEC Button 5 (Stop)

        stopMotors();

        break;

    default:

        Serial.print(F("Unknown command: 0x"));

        Serial.println(command, HEX);

}

}

// Obstacle Detection

bool checkObstacle() {

digitalWrite(TRIG_PIN, LOW);

delayMicroseconds(2);

digitalWrite(TRIG_PIN, HIGH);

delayMicroseconds(10);

digitalWrite(TRIG_PIN, LOW);



uint32_t duration = pulseIn(ECHO_PIN, HIGH);

uint16_t distance = duration \* 0.034 / 2;



if (distance > 0 && distance < OBSTACLE_DISTANCE) {

    Serial.print(F("Obstacle detected: "));

    Serial.print(distance);

    Serial.println(F(" cm"));

    return true;

}

return false;

}

void emergencyStop() {

stopMotors();

delay(500);

moveBackward();

delay(1000);

stopMotors();

}

// Motor Control Functions

void moveForward() {

digitalWrite(IN1_PIN, HIGH);

digitalWrite(IN2_PIN, LOW);

digitalWrite(IN3_PIN, HIGH);

digitalWrite(IN4_PIN, LOW);

analogWrite(ENA_PIN, 200);

analogWrite(ENB_PIN, 200);

Serial.println(F("Moving Forward"));

}

void moveBackward() {

digitalWrite(IN1_PIN, LOW);

digitalWrite(IN2_PIN, HIGH);

digitalWrite(IN3_PIN, LOW);

digitalWrite(IN4_PIN, HIGH);

analogWrite(ENA_PIN, 150);

analogWrite(ENB_PIN, 150);

Serial.println(F("Moving Backward"));

}

void turnLeft() {

digitalWrite(IN1_PIN, LOW);

digitalWrite(IN2_PIN, HIGH);

digitalWrite(IN3_PIN, HIGH);

digitalWrite(IN4_PIN, LOW);

analogWrite(ENA_PIN, 180);

analogWrite(ENB_PIN, 180);

Serial.println(F("Turning Left"));

}

void turnRight() {

digitalWrite(IN1_PIN, HIGH);

digitalWrite(IN2_PIN, LOW);

digitalWrite(IN3_PIN, LOW);

digitalWrite(IN4_PIN, HIGH);

analogWrite(ENA_PIN, 180);

analogWrite(ENB_PIN, 180);

Serial.println(F("Turning Right"));

}

void stopMotors() {

digitalWrite(IN1_PIN, LOW);

digitalWrite(IN2_PIN, LOW);

digitalWrite(IN3_PIN, LOW);

digitalWrite(IN4_PIN, LOW);

analogWrite(ENA_PIN, 0);

analogWrite(ENB_PIN, 0);

Serial.println(F("Stopped"));

}

bool isMovingForward() {

return (digitalRead(IN1_PIN) && !digitalRead(IN2_PIN) && 

        digitalRead(IN3_PIN) && !digitalRead(IN4_PIN));

}

Welcome to the forum

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the < CODE/ > icon above the compose window) to make it easier to read and copy for examination

Please post your full sketch, using code tags when you do

Posting your code using code tags prevents parts of it being interpreted as HTML coding and makes it easier to copy for examination

In my experience the easiest way to tidy up the code and add the code tags is as follows

Start by tidying up your code by using Tools/Auto Format in the IDE to make it easier to read. Then use Edit/Copy for Forum and paste what was copied in a new reply. Code tags will have been added to the code to make it easy to read in the forum thus making it easier to provide help.

It is also helpful to post error messages in code tags as it makes it easier to scroll through them and copy them for examination

Check battery voltage.

Please, format and paste your code in a "code" block.

RTFM. Arduino-IRremote/Arduino-IRremote