Ir remote control 4wd obstacle avoidant car

Hello folks,

I'm trying to use an IR remote control to activate and deactivate my robot car, but for some reason after hitting the buttons, only the servo for ultrasonic sensor responds, the DC motors don't move at all, no matter on or off. Much appreciated if somebody can help me out with my code. Thanks!

#include <AFMotor.h>  // Library for Adafruit Motor Shield v1
#include <NewPing.h>
#include <Servo.h> 
#include <IRremote.h>  // Include the IR remote library

#define TRIG_PIN A0 
#define ECHO_PIN A1 
#define IR_RECEIVE 16  // Define the IR receiver pin (change to your actual connection pin)
#define MAX_DISTANCE 200 
#define MAX_SPEED 190  // sets speed of DC motors

// Button codes for the remote (replace with actual values if different)
#define START_BUTTON 0xCE32D5BB  // Example code for "Start" (button "5")
#define STOP_BUTTON  0x4625FB32  // Example code for "Stop" (button "2")

NewPing sonar(TRIG_PIN, ECHO_PIN, MAX_DISTANCE); 

AF_DCMotor motor1(1, MOTOR12_1KHZ);  // Connect motor1 to port M1
AF_DCMotor motor2(2, MOTOR12_1KHZ);  // Connect motor2 to port M2
AF_DCMotor motor3(3, MOTOR34_1KHZ);  // Connect motor3 to port M3
AF_DCMotor motor4(4, MOTOR34_1KHZ);  // Connect motor4 to port M4
Servo myservo;   

boolean goesForward = false;
boolean isActive = false;  // Variable to track if the robot is active or not
int distance = 100;
int speedSet = 0;

IRrecv irrecv(IR_RECEIVE);  // Create an IR receiver object
decode_results results;      // Variable to store IR results

void setup() {
  pinMode(IR_RECEIVE, INPUT);
  Serial.begin(9600);  // Start serial communication for debugging
  myservo.attach(10);  
  myservo.write(115); 
  delay(2000);

  distance = readPing();
  delay(100);
  distance = readPing();
  delay(100);
  distance = readPing();
  delay(100);
  distance = readPing();
  delay(100);

  irrecv.enableIRIn();  // Start the IR receiver
}

void loop() {
  if (irrecv.decode(&results)) {  // Check if there's an IR signal
    switch(results.value) {
      case START_BUTTON:  // If the "Start" button is pressed
        isActive = true;
        Serial.println(results.value, HEX);
        break;
      case STOP_BUTTON:  // If the "Stop" button is pressed
        isActive = false;
        Serial.println(results.value, HEX);
        moveStop();  // Stop all motors when deactivated
        break;
    }
    irrecv.resume();  // Receive the next value
  }

  if (isActive) {  // Only run the algorithm if isActive is true
    int distanceR = 0;
    int distanceL = 0;
    delay(40);

    if (distance <= 15) {
      moveStop();
      delay(100);

      distanceR = lookRight();
      delay(200);
      distanceL = lookLeft();
      delay(200);

      // Determine the clearest path
      if (distanceR >= distanceL && distanceR > 15) {
        turnRight();
      } else if (distanceL > distanceR && distanceL > 15) {
        turnLeft();
      } else {
        // Both sides are blocked, perform a short backward movement
        moveBackward();
        delay(200);  // Minimal backward movement
      }
    } else {
      moveForward();
    }
    distance = readPing();
  }
}

int lookRight() {
  myservo.write(50); 
  delay(500);
  int distance = readPing();
  delay(100);
  myservo.write(115); 
  return distance;
}

int lookLeft() {
  myservo.write(170); 
  delay(500);
  int distance = readPing();
  delay(100);
  myservo.write(115); 
  return distance;
}

int readPing() { 
  delay(70);
  int cm = sonar.ping_cm();
  if (cm == 0) {
    cm = 250;
  }
  return cm;
}

void moveStop() {
  motor1.run(RELEASE); 
  motor2.run(RELEASE);
  motor3.run(RELEASE);
  motor4.run(RELEASE);
} 
  
void moveForward() {
  if (!goesForward) {
    goesForward = true;
    motor1.run(FORWARD);      
    motor2.run(FORWARD);
    motor3.run(FORWARD); 
    motor4.run(FORWARD);     
    for (speedSet = 0; speedSet < MAX_SPEED; speedSet += 2) { // slowly bring the speed up to avoid loading down the batteries too quickly
      motor1.setSpeed(speedSet);
      motor2.setSpeed(speedSet);
      motor3.setSpeed(speedSet);
      motor4.setSpeed(speedSet);
      delay(5);
    }
  }
}

void moveBackward() {
  goesForward = false;
  motor1.run(BACKWARD);      
  motor2.run(BACKWARD);
  motor3.run(BACKWARD);
  motor4.run(BACKWARD);  
  for (speedSet = 0; speedSet < MAX_SPEED / 2; speedSet += 2) { // limit speed for backward movement
    motor1.setSpeed(speedSet);
    motor2.setSpeed(speedSet);
    motor3.setSpeed(speedSet);
    motor4.setSpeed(speedSet);
    delay(5);
  }
}  

void turnRight() {
  motor1.run(FORWARD);
  motor2.run(FORWARD);
  motor3.run(BACKWARD);
  motor4.run(BACKWARD);     
  delay(500);
  motor1.run(FORWARD);      
  motor2.run(FORWARD);
  motor3.run(FORWARD);
  motor4.run(FORWARD);      
} 
 
void turnLeft() {
  motor1.run(BACKWARD);     
  motor2.run(BACKWARD);  
  motor3.run(FORWARD);
  motor4.run(FORWARD);   
  delay(500);
  motor1.run(FORWARD);     
  motor2.run(FORWARD);
  motor3.run(FORWARD);
  motor4.run(FORWARD);
}

My guess is your power supply is not sufficient to energize four motors and a servo, without having problems. This is often the case. Provide a circuit diagram with pin labels and descriptions of the devices, and a photo of how you have wired it.

[edit] Do you know which Arduino pins you are using? 11, 3, 5, and 6 or 4, 7, 8 and 12?

(reference: https://cdn-learn.adafruit.com/downloads/pdf/adafruit-motor-shield.pdf )

I'm using a 4WD robot platform and I can't get anything to work.
The motors used in the 4WD robot platforms from Maker Shed, DF Robotics,
Jameco and others have a lot of "brush noise". This feeds back into the Arduino
circuitry and causes unstable operation. This problem can be solved by soldering
3 noise suppression capacitors to the motor. 1 between the motor terminals, and
one from each terminal to the motor casing.

Hello, thank you for your reply, before incorporating the ir module everything works perfectly. here's a video of it working

I'm using 2 x 18650 batteries for the motors and the arduino is powered separately.

earlier, i've found out that the irremote library(4.4) was not working properly, then i installed an older version(2.7) and it decodes perfectly, however, this interferes with the timer from the newping library, then i commanded out the timer lines in the newping library(that i'm not using) it was still not working properly.

now i'm considering getting a bluetooth module or to add a relay to control the power on/off with the ir remote control

Would you try the video configuration on the floor/table? The no-load of elevated wheels might be hiding a lack of torque/current.

I would start with getting the base working, then plan from that point.

Problem solved, it was indeed the irremote library interfering with the newping library, the newer versions of newping library allow you to disable the timer in the .h file, another problem was the irremote decoding not being constant, after turning the circuit on, the code FF38C7 and FF6897 became C101E57B and 488F3CBB. my easy solution is to define those two hex code as well, and now everything works fine
#define START_BUTTON 0xFF38C7 #define STOP_BUTTON 0xFF6897 #define STP 0xC101E57B #define RSM 0x488F3CBB