IRremote.h caussing issues with AFMotor.h

Hi, When I include irrecv.enableIRIn(); in my setup, motor 1 and 2 stops working.
when I comment it, the motor works as usual. any work around to make all 4 motors work?

My plan is to use a remote to send signals to my robot to move in different directions.

My code :

#include <IRremote.h>
#include <AFMotor.h>

int RECV_PIN = A3;
IRrecv irrecv(RECV_PIN);
decode_results results;

unsigned long previousCommand = 0; // To store the previous valid command
// Define the motor shield object
AF_DCMotor motor1(1); // Motor 1
AF_DCMotor motor2(2); // Motor 2
AF_DCMotor motor3(3); // Motor 3
AF_DCMotor motor4(4); // Motor 4

void setup() {
  Serial.begin(9600);
  motor1.setSpeed(255);
  motor2.setSpeed(255);
  motor3.setSpeed(255);
  motor4.setSpeed(255);
  //test();
  irrecv.enableIRIn(); // Start the receiver
  //test();
}

void loop() {
 
  if (irrecv.decode(&results)) {
    Serial.print("Received IR signal: 0x");
    Serial.println(results.value, HEX);

    if (results.value == 0xFF48B7) {
      Serial.println("fwd");
      previousCommand = results.value;
      moveForward();
      
    } else if (results.value == 0xFF7887) {
      Serial.println("bck");
      previousCommand = results.value;
      moveBackward();
    } else if (results.value == 0xFF02FD) {
      Serial.println("lft");
      previousCommand = results.value;
      turnLeft();
     } else if (results.value == 0xFF32CD) {
      Serial.println("stp");
      previousCommand = results.value;
      stopMotors();
    } else if (results.value == 0xFF20DF) {
      Serial.println("rgt");
      previousCommand = results.value;
      turnRight();
    } else if (results.value == 0xFFFFFFFF && previousCommand != 0) {
      // Continue the previous task
      if (previousCommand == 0xFF48B7) {
        Serial.println("fwd (continue)");
      } else if (previousCommand == 0xFF7887) {
        Serial.println("bck (continue)");
        moveBackward();
      } else if (previousCommand == 0xFF02FD) {
        Serial.println("lft (continue)");
        turnLeft();
      } else if (previousCommand == 0xFF32CD) {
        Serial.println("stp (continue)");
        stopMotors();
      } else if (previousCommand == 0xFF20DF) {
        Serial.println("rgt (continue)");
        turnRight();
      }
    }
    irrecv.resume(); // Receive the next value
  }
}


void moveForward() {
  //unsigned long startTime = millis(); // Record the start time

  motor1.run(FORWARD);
  motor2.run(FORWARD);
  motor3.run(FORWARD);
  motor4.run(FORWARD);
  Serial.println("running");
}

void moveBackward() {
 // unsigned long startTime = millis(); // Record the start time

  motor1.run(BACKWARD);
  motor2.run(BACKWARD);
  motor3.run(BACKWARD);
  motor4.run(BACKWARD);
  Serial.println("running");
}

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

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

void stopMotors() {
 
  motor1.run(RELEASE);
  motor2.run(RELEASE);
  motor3.run(RELEASE);
  motor4.run(RELEASE);
  
  Serial.println("stopped");
}

void test(){
  testMotor(motor1, "Motor 1");
  delay(500); // Wait for 2 seconds before testing the next motor
  testMotor(motor2, "Motor 2");
  delay(500);
  testMotor(motor3, "Motor 3");
  delay(500);
  testMotor(motor4, "Motor 4");
}
void testMotor(AF_DCMotor &motor, const char *motorName) {
  Serial.print("Testing ");
  Serial.println(motorName);

  // Run the motor forward for 2 seconds
 
  motor.run(FORWARD);
  delay(2000);
  
  // Stop the motor for 1 second
 
  motor.run(RELEASE);
  delay(1000);

  // Run the motor backward for 2 seconds
  
  motor.run(BACKWARD);
  delay(2000);

  // Stop the motor for 1 second
  
  motor.run(RELEASE);
  delay(1000);
}

The IRremote library and the AFMotor library are both trying to use the same timers. That won't work. I'd suggest using a more modern motor shield that generates its own PWM signals rather than depending on the Arduino to do it.

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