Function not returning to switch /case loop

Good day all, this code “should” call 4 functions forward, left right etc… My problem is that whichever function I call first is the only function that runs. I have tried putting return at the end of each function to no avail. it seems too simple to go wrong, but there you have it.

Any insights greatly appreciated. This is running on an Arduino uno and DFRrobotshop rovershield v1


#include <IRremote.h> // Include the IRremote library

const int IR_RECEIVER_PIN = 9; // Define the pin connected to the IR receiver
IRrecv irrecv(IR_RECEIVER_PIN);  // Create an IRrecv object
decode_results results; // Create a decode_results variable to store IR data

// Define motor control pins (adjust based on your Rover Shield connections)
const int E1 = 6; // Motor 1 speed control
const int E2 = 5; // Motor 2 speed control 
const int M1 = 8; // Motor 1 direction control
const int M2 = 7; // Motor 2 direction control 

void setup() {
  Serial.begin(9600); // Initialize serial communication
  irrecv.enableIRIn(); // Enable IR receiver
  
  // Set motor control pins as outputs
  pinMode(E1, OUTPUT);
  pinMode(E2, OUTPUT);
  pinMode(M1, OUTPUT);
  pinMode(M2, OUTPUT);
}

void loop() {
  if (irrecv.decode(&results)) { // Check if IR signal received
    switch (results.value) { // Perform actions based on IR code 
      case 0xE0E006F9: // IR code for "Forward"
        moveForward();
        break;
      case 0xE0E08679: // IR code for "Backward"
        moveBackward();
        break;
      case 0xE0E0A659: // IR code for "Left"
        turnLeft();
        break;
      case 0xE0E046B9: // IR code for "Right"
        turnRight();
        break;
      // Add more cases for other IR commands as needed
    }
    irrecv.resume(); // Prepare to receive the next IR signal
  }
}

// Motor control functions
void moveForward() {
  digitalWrite(M1, HIGH); // Set motor 1 direction
  digitalWrite(M2, HIGH); // Set motor 2 direction
  analogWrite(E1, 255); // Set motor 1 speed
  analogWrite(E2, 255); // Set motor 2 speed
}

void moveBackward() {
  digitalWrite(M1, LOW); // Set motor 1 direction
  digitalWrite(M2, LOW); // Set motor 2 direction
  analogWrite(E1, 255); // Set motor 1 speed
  analogWrite(E2, 255); // Set motor 2 speed
}

void turnLeft() {
  digitalWrite(M1, HIGH); // Set motor 1 direction
  digitalWrite(M2, LOW); // Set motor 2 direction
  analogWrite(E1, 255); // Set motor 1 speed
  analogWrite(E2, 255); // Set motor 2 speed
}

void turnRight() {
  digitalWrite(M1, LOW); // Set motor 1 direction
  digitalWrite(M2, HIGH); // Set motor 2 direction
  analogWrite(E1, 255); // Set motor 1 speed
  analogWrite(E2, 255); // Set motor 2 speed
}

It’s never easy to troubleshoot code, so used ChatGPT for some insights (untested):

Your issue likely comes from the fact that once a movement function (e.g., moveForward()) is called, it keeps the motors running indefinitely because there’s no stop condition.

Fix: Add a delay and stop function

Once a movement function is executed, the motors stay in that state. You need to:

  1. Add a delay so the movement happens for a brief moment.
  2. Stop the motors afterward.
#include <IRremote.h> // Include the IRremote library

const int IR_RECEIVER_PIN = 9; // Define the pin connected to the IR receiver
IRrecv irrecv(IR_RECEIVER_PIN);  // Create an IRrecv object
decode_results results; // Create a decode_results variable to store IR data

// Define motor control pins (adjust based on your Rover Shield connections)
const int E1 = 6; // Motor 1 speed control
const int E2 = 5; // Motor 2 speed control 
const int M1 = 8; // Motor 1 direction control
const int M2 = 7; // Motor 2 direction control 

void setup() {
  Serial.begin(9600); // Initialize serial communication
  irrecv.enableIRIn(); // Enable IR receiver
  
  // Set motor control pins as outputs
  pinMode(E1, OUTPUT);
  pinMode(E2, OUTPUT);
  pinMode(M1, OUTPUT);
  pinMode(M2, OUTPUT);
}

void loop() {
  if (irrecv.decode(&results)) { // Check if IR signal received
    Serial.println(results.value, HEX); // Print received IR code for debugging

    switch (results.value) { // Perform actions based on IR code 
      case 0xE0E006F9: // IR code for "Forward"
        moveForward();
        break;
      case 0xE0E08679: // IR code for "Backward"
        moveBackward();
        break;
      case 0xE0E0A659: // IR code for "Left"
        turnLeft();
        break;
      case 0xE0E046B9: // IR code for "Right"
        turnRight();
        break;
    }
    
    irrecv.resume(); // Prepare to receive the next IR signal
  }
}

// Motor control functions
void moveForward() {
  digitalWrite(M1, HIGH); 
  digitalWrite(M2, HIGH); 
  analogWrite(E1, 255); 
  analogWrite(E2, 255);
  delay(500);  // Move for 500ms
  stopMotors();
}

void moveBackward() {
  digitalWrite(M1, LOW);
  digitalWrite(M2, LOW);
  analogWrite(E1, 255);
  analogWrite(E2, 255);
  delay(500);
  stopMotors();
}

void turnLeft() {
  digitalWrite(M1, HIGH);
  digitalWrite(M2, LOW);
  analogWrite(E1, 255);
  analogWrite(E2, 255);
  delay(500);
  stopMotors();
}

void turnRight() {
  digitalWrite(M1, LOW);
  digitalWrite(M2, HIGH);
  analogWrite(E1, 255);
  analogWrite(E2, 255);
  delay(500);
  stopMotors();
}

// Stop motors function
void stopMotors() {
  analogWrite(E1, 0);
  analogWrite(E2, 0);
}