IR reviever continuously running after turning on motor

When I press a button on my IR remote and it makes my motors run, the Reciever keeps on flashing the red light and won't let me input any other button. What I currently want to make happen is the motors to run foward on a button press, and then stop on a different button press. Here is my code below:

#include <IRremote.h>

// Motor Pins
const int motorPin1 = 5;
const int motorPin2 = 6;
const int motorPin3 = 10;
const int motorPin4 = 9;

// Sensor Pins
const int RECV_PIN = 11;
const int trigPin = 12;
const int echoPin = 8;
const int buzzerPin = 13;
const int soundSensorPin = 7;

// IR Receiver
IRrecv irrecv(RECV_PIN);
decode_results results;

// Threshold for obstacle detection
const int DISTANCE_THRESHOLD = 20; // Distance in cm to stop from obstacle

void setup() {
pinMode(motorPin1, OUTPUT);
pinMode(motorPin2, OUTPUT);
pinMode(motorPin3, OUTPUT);
pinMode(motorPin4, OUTPUT);
pinMode(buzzerPin, OUTPUT);
pinMode(soundSensorPin, INPUT);

irrecv.enableIRIn(); // Start the IR receiver
Serial.begin(9600);  // Start the serial monitor for debugging

}

void loop() {
long distance = getDistance(); // Measure distance in cm using pulseIn()

Serial.print("Distance: ");
Serial.println(distance); // Print distance in the Serial Monitor

// Stop motors if an obstacle is within the threshold
if (distance > 0 && distance < DISTANCE_THRESHOLD) {
    Serial.println("Obstacle detected, stopping motors.");
    stopMotors();
    delay(1000);  // Short delay to avoid continuous stopping
} else if (irrecv.decode(&results)) {
    Serial.print("IR Code: ");
    Serial.println(results.value, HEX);  // Print the IR code received

    // Handle the different remote button presses
    switch (results.value) {
        case 0x1B103CA82: // Forward code
            Serial.println("Moving forward");
            moveForward();
            break;
        case 0x2F807FF: // Reverse code
            Serial.println("Moving backward");
            moveBackward();
            break;
        case 0x3BB44FF: // Left code
            Serial.println("Turning left");
            turnLeft();
            break;
        case 0x4BC43FF: // Right code
            Serial.println("Turning right");
            turnRight();
            break;
        case 0x25AE7EE0: // Stop code
            Serial.println("Stop button pressed");
            stopMotors();
            break;
        case 0x6F609FF: // Beep code (example, replace with your buzzer button's code)
            Serial.println("Beep button pressed");
            buzzerBeep();  // Only beeps when this button is pressed
            break;
        default:
            Serial.println("Unknown IR code, not beeping");
            // Optionally stop the buzzer if it's on (in case of incorrect signal)
            digitalWrite(buzzerPin, LOW);
            break;
    }
    irrecv.resume(); // Receive the next value
}

}

// Motor Control Functions
void moveForward() {
Serial.println("Moving Forward");
digitalWrite(motorPin1, HIGH);
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin3, HIGH);
digitalWrite(motorPin4, LOW);
}

void moveBackward() {
Serial.println("Moving Backward");
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, HIGH);
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin4, HIGH);
}

void turnLeft() {
Serial.println("Turning Left");
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, HIGH);
digitalWrite(motorPin3, HIGH);
digitalWrite(motorPin4, LOW);
}

void turnRight() {
Serial.println("Turning Right");
digitalWrite(motorPin1, HIGH);
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin4, HIGH);
}

void stopMotors() {
Serial.println("Stopping Motors");
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin4, LOW);
}

// Buzzer beep function
void buzzerBeep() {
digitalWrite(buzzerPin, HIGH); // Turn on the buzzer
delay(100); // Beep duration (100ms)
digitalWrite(buzzerPin, LOW); // Turn off the buzzer
}

// Function to calculate distance using pulseIn()
long getDistance() {
// Send a 10us pulse to trigger the sensor
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);

// Measure the pulse duration from the echo pin
long duration = pulseIn(echoPin, HIGH);
// Calculate the distance in centimeters
long distance = duration * 0.0344 / 2;
return distance;

}

Cross-posting is against the Arduino forum rules. The reason is that duplicate posts can waste the time of the people trying to help. Someone might spend a lot of time investigating and writing a detailed answer on one topic, without knowing that someone else already did the same in the other topic.

Please create one topic only for your question and choose the forum category carefully. If you have multiple questions about the same project then please ask your questions in the one topic as the answers to one question provide useful context for the others, and also you won’t have to keep explaining your project repeatedly.

Repeated duplicate posting could result in a temporary or permanent ban from the forum.

Could you take a few moments to Learn How To Use The Forum. It will help you get the best out of the forum in the future.

Thank you.

@ethanweekly ,

Your other topic on the same subject deleted.

Please do not duplicate your questions as doing so wastes the time and effort of the volunteers trying to help you as they are then answering the same thing in different places.

Please create one topic only for your question and choose the forum category carefully. If you have multiple questions about the same project then please ask your questions in the one topic as the answers to one question provide useful context for the others, and also you won’t have to keep explaining your project repeatedly.

Repeated duplicate posting could result in a temporary or permanent ban from the forum.

Could you take a few moments to Learn How To Use The Forum

It will help you get the best out of the forum in the future.

Thank you.

how does the code every reach the else if than checks for IR input once the robot is within the DISTANCE_THRESHOLD?

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