DigitalWrite interfering with IRremote

Why would digitalWrite cause IRremote to stop responding?

I'm using and arduino mega with a 2A motor controller from DFrobot on top.

Here's my code with the problematic sections commented out.

// Code for IRremote

#include <IRremote.h>

int receiver = 4;
IRrecv irrecv(receiver); // create instance of 'irrecv'
decode_results decodedSignal;

///


// Motor code
//Arduino PWM Speed Control?
int E1 = 6;   
int M1 = 7;
int E2 = 5;                         
int M2 = 4;                           

///



void setup() 
{ 
    Serial.begin(9600);
    pinMode(M1, OUTPUT);   
    pinMode(M2, OUTPUT); 
    
    irrecv.enableIRIn(); // Start the receiver
}
void loop() 
{ 
 
  
  int value=255;
  
   if (irrecv.decode(&decodedSignal)) // have we received an IR signal?
  {
    if (decodedSignal.value == 0xFF4AB5) {  // 8 backwards   
      Serial.println("backwards");
      /* digitalWrite(M1,LOW);   
      digitalWrite(M2, LOW);       
      analogWrite(E1, value);   //PWM Speed Control
      analogWrite(E2, value);   //PWM Speed Control
      */
      
    }
    else if (decodedSignal.value == 0xFF18E7) {   //2 forward
      Serial.println("forwards");
     /* digitalWrite(M1,HIGH);   
      digitalWrite(M2, HIGH);       
      analogWrite(E1, value);   //PWM Speed Control
      analogWrite(E2, value);   //PWM Speed Control
      */

      
    }
    else if (decodedSignal.value == 0xFF38C7) {  //5 stop
      Serial.println("stop");
      

    }
    else if (decodedSignal.value == 0xFF10EF) {  //4 left
      Serial.println("left");
     /* digitalWrite(M1,LOW);   
      digitalWrite(M2, HIGH);       
      analogWrite(E1, value);   //PWM Speed Control
      analogWrite(E2, value);   //PWM Speed Control
      */
    }
    else if (decodedSignal.value == 0xFF5AA5) {  //6 right
      Serial.println("right");
      /*    digitalWrite(M1,HIGH);   
      digitalWrite(M2, LOW);       
      analogWrite(E1, value);   //PWM Speed Control
      analogWrite(E2, value);   //PWM Speed Control
     */  
   }
     
     Serial.println(decodedSignal.value, HEX);
    irrecv.resume(); // Receive the next value
    
   } 
}
int receiver = 4;
IRrecv irrecv(receiver); // create instance of 'irrecv'

int M2 = 4;

Give us a clue. What is really connected to pin 4? The IR receiver or the motor?

Thanks for your reply. Ok. That's a mistake in the code. I originally had the ir receiver on pin 2 and was getting the same problem. The serial input gives

backwards
FF4AB5
FFFFFFFF

Then for subsequent presses of the remote I get

0
FFFFFFFF

So I guess it doesn't understand the code. This still only happens when the motor is driven and the motor shield is powered off separate battery power to the ardunio mega (which is powered of USB). If the code for the motor drive is greyed out I get perfect response on the serial monitor.

Why would digitalWrite cause IRremote to stop responding?

It's unlikely that the digitalWrite() is the problem. The analogWrite() that follows starts PWM which means timers and interrupts. The IRremote code also uses timers and interrupts, and they are most likely interfering with each other.

You may be able to find a combination of analogWrite() pins and IR pin that do not interfere with each other. Or not, I don't know.

So another iteration of my code just used digitalWrites so just LOW or HIGH on the M1 E1 M2 and E2 pins. This had the same problem, so it would seem that the problem is related to the motor or power. Could this issue be noise from the DC motors. I've been reading that a 0.1 microF capacitor across the motor terminals of each motor may solve this. Does this make any sense?