Having trouble running two motors at a time when used IRremote.h library

Hey there!
I am using arduino nano along with IR receiving head VS1838B.
I am using library IRremote.h
I have functions in my code which runs the bot in various directions.
For a test I have code in which bot goes forward and it works very well.
When I tried merging separately coded working codes, one motor stops working, which works fine again when I remove IR receiver code.
I tried and pinned out the line which causes this thing which is

	irrecv.enableIRIn();

When I comment this line the code works totally fine!
Steps I followed:
Wrote code for running bot in various directions which worked perfectly.
Wrote code to get values of buttons when they're pressed using IRremote.h library which worked perfectly.
Merged code but one motor stopped working.
Here's my full code

#include<IRremote.h>

// Motor Driver
#define EN2 3           // Enable pin 2
#define RM1 4           // Front Right Motor
#define RM2 5           // Rear Right Motor
#define EN1 6           // Enable pin 1
#define LM1 7           // Front Left Motor
#define LM2 8           // Rear Left Motor

#define speedEN1 150
#define speedEN2 150

int RECV_PIN = 9;
IRrecv irrecv(RECV_PIN);
decode_results results;
unsigned long key_value = 0;

void forward()
{

	Serial.print("Forwards Triggered");
	digitalWrite(RM1,HIGH);
	digitalWrite(RM2,LOW);
	analogWrite(EN2,speedEN2);
	digitalWrite(LM1,HIGH);
	digitalWrite(LM2,LOW);
	analogWrite(EN1,speedEN1);
}

void right(){
	Serial.print("Right Triggered");
	digitalWrite(RM1,LOW);
	digitalWrite(RM2,LOW);
	analogWrite(EN2,0);
	digitalWrite(LM1,HIGH);
	digitalWrite(LM2,LOW);
	analogWrite(EN1,speedEN1);
}


void left(){
	Serial.print("Right Triggered");
	digitalWrite(RM1,HIGH);
	digitalWrite(RM2,LOW);
	analogWrite(EN2,speedEN2);
	digitalWrite(LM1,LOW);
	digitalWrite(LM2,LOW);
	analogWrite(EN1,0);
}


void spin_left(){
	Serial.print("Right Triggered");
	digitalWrite(RM1,HIGH);
	digitalWrite(RM2,LOW);
	analogWrite(EN2,speedEN2);
	digitalWrite(LM1,LOW);
	digitalWrite(LM2,HIGH);
	analogWrite(EN1,speedEN1);
}


void spin_right(){
	Serial.print("Right Triggered");
	digitalWrite(RM1,HIGH);
	digitalWrite(RM2,LOW);
	analogWrite(EN2,speedEN2);
	digitalWrite(LM1,LOW);
	digitalWrite(LM2,HIGH);
	analogWrite(EN1,speedEN1);
}


void backward()
{

	Serial.print("Reverse Triggered");
	digitalWrite(RM1,LOW);
	digitalWrite(RM2,HIGH);
	analogWrite(EN2,speedEN2);
	digitalWrite(LM1,LOW);
	digitalWrite(LM2,HIGH);
	analogWrite(EN1,speedEN1);
}


void stop_all()
{

// 	Serial.print("Right Triggered");
	digitalWrite(RM1,LOW);
	digitalWrite(RM2,LOW);
	digitalWrite(LM1,LOW);
	digitalWrite(LM2,LOW);
	analogWrite(EN1,0);
	analogWrite(EN2,0);
}

void deg360()
{
	Serial.print("Right Triggered");
	digitalWrite(RM1,HIGH);
	digitalWrite(RM2,LOW);
	analogWrite(EN2,speedEN2);
	digitalWrite(LM1,LOW);
	digitalWrite(LM2,HIGH);
	analogWrite(EN1,speedEN1);
}

void setup()
{
	Serial.begin(9600);
	// MOTOR AND ENABLES
	pinMode (RM1, OUTPUT);
	pinMode (RM2, OUTPUT);
	pinMode (LM1, OUTPUT);
	pinMode (LM2, OUTPUT);
	pinMode (EN1, OUTPUT);
	pinMode (EN2, OUTPUT);
	Serial.println("Starting IR");
	irrecv.enableIRIn(); // when I comment out this line and receiver code forward function works perfectly 
	Serial.println("Started IR");
}
void loop()
{
  if (irrecv.decode(&results)) {                     
    Serial.println(results.value,HEX);
    if (results.value == 0XFFFFFFFF)
      results.value = key_value;
    switch (results.value) {
      case 0xFF629D:
        forward();
        // Serial.println("Working forward");
        break;
    }
    key_value = results.value;
    irrecv.resume();
    delay(100);
  }
}

hard to make any comment without seeing the earlier programs where both motors work

using sub-functions to control direction and speed of each motor would make the code easier to read. don't understand why you treat code 0xFFFFFFFF as a special case a instead of just ignoring it.

This code works:

// Motor Driver
#define EN2 3           // Enable pin 2
#define RM1 4           // Front Right Motor
#define RM2 5           // Rear Right Motor
#define EN1 6           // Enable pin 1
#define LM1 7           // Front Left Motor
#define LM2 8           // Rear Left Motor

#define speedEN1 150
#define speedEN2 150

int RECV_PIN = 9;
IRrecv irrecv(RECV_PIN);
decode_results results;
unsigned long key_value = 0;

void forward()
{

	Serial.print("Forwards Triggered");
	digitalWrite(RM1,HIGH);
	digitalWrite(RM2,LOW);
	analogWrite(EN2,speedEN2);
	digitalWrite(LM1,HIGH);
	digitalWrite(LM2,LOW);
	analogWrite(EN1,speedEN1);
}

void right(){
	Serial.print("Right Triggered");
	digitalWrite(RM1,LOW);
	digitalWrite(RM2,LOW);
	analogWrite(EN2,0);
	digitalWrite(LM1,HIGH);
	digitalWrite(LM2,LOW);
	analogWrite(EN1,speedEN1);
}


void left(){
	Serial.print("Right Triggered");
	digitalWrite(RM1,HIGH);
	digitalWrite(RM2,LOW);
	analogWrite(EN2,speedEN2);
	digitalWrite(LM1,LOW);
	digitalWrite(LM2,LOW);
	analogWrite(EN1,0);
}


void spin_left(){
	Serial.print("Right Triggered");
	digitalWrite(RM1,HIGH);
	digitalWrite(RM2,LOW);
	analogWrite(EN2,speedEN2);
	digitalWrite(LM1,LOW);
	digitalWrite(LM2,HIGH);
	analogWrite(EN1,speedEN1);
}


void spin_right(){
	Serial.print("Right Triggered");
	digitalWrite(RM1,HIGH);
	digitalWrite(RM2,LOW);
	analogWrite(EN2,speedEN2);
	digitalWrite(LM1,LOW);
	digitalWrite(LM2,HIGH);
	analogWrite(EN1,speedEN1);
}


void backward()
{

	Serial.print("Reverse Triggered");
	digitalWrite(RM1,LOW);
	digitalWrite(RM2,HIGH);
	analogWrite(EN2,speedEN2);
	digitalWrite(LM1,LOW);
	digitalWrite(LM2,HIGH);
	analogWrite(EN1,speedEN1);
}


void stop_all()
{

// 	Serial.print("Right Triggered");
	digitalWrite(RM1,LOW);
	digitalWrite(RM2,LOW);
	digitalWrite(LM1,LOW);
	digitalWrite(LM2,LOW);
	analogWrite(EN1,0);
	analogWrite(EN2,0);
}

void deg360()
{
	Serial.print("Right Triggered");
	digitalWrite(RM1,HIGH);
	digitalWrite(RM2,LOW);
	analogWrite(EN2,speedEN2);
	digitalWrite(LM1,LOW);
	digitalWrite(LM2,HIGH);
	analogWrite(EN1,speedEN1);
}

void setup()
{
	Serial.begin(9600);
	// MOTOR AND ENABLES
	pinMode (RM1, OUTPUT);
	pinMode (RM2, OUTPUT);
	pinMode (LM1, OUTPUT);
	pinMode (LM2, OUTPUT);
	pinMode (EN1, OUTPUT);
	pinMode (EN2, OUTPUT);
	Serial.println("Starting IR");
	// irrecv.enableIRIn(); // when I comment out this line and receiver code forward function works perfectly 
	Serial.println("Started IR");
}
void loop()
{
  forward();
}

On repeated button press it gives reading as 0xFFFFFFFF. So to handle that case I have used the if statement.
After googling a bit, I found out that pin number 3 is used by that IR sensor for some timer purpose. I am literally unaware about concepts so please bear with me. That pin number 3 is exactly the pin where I have connected my enable pin of right motor which stops working when I merge the code. Do you know anything about this? Can you help me out of this? Simplest solution would be to change that pin 3 to some other pin, but is there any other solution?

if the IR library uses the pin, not sure what you can do except use a different pin.

you could try the following code to verify that your code without the IR commands functions properly using single character commands entered thru the serial monitor

#undef IR
#ifdef IR
# include<IRremote.h>
#endif

// Motor Driver
#define EN2 3           // Enable pin 2
#define RM1 4           // Front Right Motor
#define RM2 5           // Rear Right Motor
#define EN1 6           // Enable pin 1
#define LM1 7           // Front Left Motor
#define LM2 8           // Rear Left Motor
#define speedEN1 150
#define speedEN2 150

#ifdef IR
int RECV_PIN = 9;
IRrecv irrecv(RECV_PIN);

decode_results results;
unsigned long key_value = 0;
#endif

void forward()
{
    	Serial.print("Forwards Triggered");
    	digitalWrite(RM1,HIGH);
    	digitalWrite(RM2,LOW);
    	analogWrite(EN2,speedEN2);
    	digitalWrite(LM1,HIGH);
    	digitalWrite(LM2,LOW);
    	analogWrite(EN1,speedEN1);
}
void right(){
    	Serial.print("Right Triggered");
    	digitalWrite(RM1,LOW);
    	digitalWrite(RM2,LOW);
    	analogWrite(EN2,0);
    	digitalWrite(LM1,HIGH);
    	digitalWrite(LM2,LOW);
    	analogWrite(EN1,speedEN1);
}
void left(){
    	Serial.print("Right Triggered");
    	digitalWrite(RM1,HIGH);
    	digitalWrite(RM2,LOW);
    	analogWrite(EN2,speedEN2);
    	digitalWrite(LM1,LOW);
    	digitalWrite(LM2,LOW);
    	analogWrite(EN1,0);
}
void spin_left(){
    	Serial.print("Right Triggered");
    	digitalWrite(RM1,HIGH);
    	digitalWrite(RM2,LOW);
    	analogWrite(EN2,speedEN2);
    	digitalWrite(LM1,LOW);
    	digitalWrite(LM2,HIGH);
    	analogWrite(EN1,speedEN1);
}
void spin_right(){
    	Serial.print("Right Triggered");
    	digitalWrite(RM1,HIGH);
    	digitalWrite(RM2,LOW);
    	analogWrite(EN2,speedEN2);
    	digitalWrite(LM1,LOW);
    	digitalWrite(LM2,HIGH);
    	analogWrite(EN1,speedEN1);
}
void backward()
{
    	Serial.print("Reverse Triggered");
    	digitalWrite(RM1,LOW);
    	digitalWrite(RM2,HIGH);
    	analogWrite(EN2,speedEN2);
    	digitalWrite(LM1,LOW);
    	digitalWrite(LM2,HIGH);
    	analogWrite(EN1,speedEN1);
}
void stop_all()
{
    // 	Serial.print("Right Triggered");
    	digitalWrite(RM1,LOW);
    	digitalWrite(RM2,LOW);
    	digitalWrite(LM1,LOW);
    	digitalWrite(LM2,LOW);
    	analogWrite(EN1,0);
    	analogWrite(EN2,0);
}
void deg360()
{
    	Serial.print("Right Triggered");
    	digitalWrite(RM1,HIGH);
    	digitalWrite(RM2,LOW);
    	analogWrite(EN2,speedEN2);
    	digitalWrite(LM1,LOW);
    	digitalWrite(LM2,HIGH);
    	analogWrite(EN1,speedEN1);
}
void setup()
{
    	Serial.begin(9600);
    	// MOTOR AND ENABLES
    	pinMode (RM1, OUTPUT);
    	pinMode (RM2, OUTPUT);
    	pinMode (LM1, OUTPUT);
    	pinMode (LM2, OUTPUT);
    	pinMode (EN1, OUTPUT);
    	pinMode (EN2, OUTPUT);
    	Serial.println("Starting IR");
#ifdef IR
    irrecv.enableIRIn(); // when I comment out this line and receiver code forward function works perfectly
#endif
    	Serial.println("Started IR");
}

void loop()
{
    if (Serial.available ())
        switch (Serial.read ())  {
        case 'f':
            forward ();
            break;

        case 'r':
            right ();
            break;

        case 'l':
            left ();
            break;

        case 'b':
            backward ();
            break;

        case 'R':
            spin_right ();
            break;

        case 'L':
            spin_left ();
            break;

        case 'S':
            stop_all ();
            break;

        case '3':
            deg360 ();
            break;

        default:
            break;
    }

#ifdef IR
    if (irrecv.decode(&results)) {
        Serial.println(results.value,HEX);
        if (results.value == 0XFFFFFFFF)
            results.value = key_value;

        switch (results.value) {
            case 0xFF629D:
            forward();
            // Serial.println("Working forward");
            break;
        }
        key_value = results.value;
        irrecv.resume();
        delay(100);
    }
#endif
}

obviously you could use a similar switch statement for each IR code, simply ignoring 0xFFFFFFFF

1 Like

Hi,
I would like to see this information that talks about this use of pin 3 .
Can post the link

RV mineirin

This helps but the code does not work properly when merged.

here it is 解决Arduino的IRremote库与电机库冲突的问题
Translating the page would help.

can't comment w/o seeing the "merged" code

I meant the code you provided with works fine! The one I have clipped in question is merged code which does not work.