IR Remote codes change when changing speed using Arduino Motor Shield.

I have built a simple robot that goes forward, reverse, left, right and stop, controlled by an old DVD remote. Everything works fine if I leave my analogWrite pin speed at either 0 or 255. Once I change the speed in analogWrite, the IR codes received from the remote seem to change. Example - Forward code is 510 as listed in the Serial Monitor. If I change the speed to a value between 0 and 255, the serial monitor shows 510 for the first button push, but then all of the codes change to something else. Forward changes to 33689 for example, reverse changes to 48356, etc. so the robot no longer moves.
Here is my code. I have tried changing the recv_pin, but that didn't help.

Any Ideas?
#include <IRremote.h>

int RECV_PIN = 5; // connect IR receiver
#define forward 510 // Forward Button Code
#define reverse 33150 // Reverse Button Code
#define stopcar 33915 // Stop Button Code
#define left 35445 // left Button Code
#define right 45645 // right Button Code

IRrecv irrecv(RECV_PIN);

decode_results results;

void setup()
{
Serial.begin(9600); // send codes to serial monitor
irrecv.enableIRIn(); // Start the receiver
//Setup Motor A

  • pinMode(12, OUTPUT); //Motor direction A pin*

  • pinMode(9, OUTPUT); //Brake Channel A pin*

  • //Setup Motor B*

  • pinMode(13, OUTPUT); //Motor direction B pin*

  • pinMode(8, OUTPUT); //Brake B pin*

  • digitalWrite(9,HIGH); //Turns brakes on Motor A*

  • digitalWrite(8,HIGH); //Turns brakes on Motor B*
    }

void loop() {
if (irrecv.decode(&results)) {

  • unsigned int value = results.value;*
  • switch(value)*
    {
  • case forward:*
  • digitalWrite(9,LOW); //Brake Motor A - off*
  • digitalWrite(12, HIGH); //Motor A Forward*
  • analogWrite(3, 255); //Adjust speed of motor A*
  • digitalWrite(8,LOW); //Brake Motor B - off*
  • digitalWrite(13, HIGH); //Motor B Forward*
  • analogWrite(11, 255); //adjust speed of motor B*
  • break;*
  • case reverse:*
  • digitalWrite(9,LOW); //Brake Motor A*
  • digitalWrite(12, LOW); //Motor A Forward*
  • analogWrite(3, 255); //Adjust speed of motor A*
  • digitalWrite(8,LOW); //Brake Motor B - off*
  • digitalWrite(13, LOW); //Motor B Forward*
  • analogWrite(11, 255); //adjust speed of motor B*
  • break;*
  • case stopcar:*
  • digitalWrite(9,HIGH); //Brake Motor A*
  • digitalWrite(12, HIGH); //Motor A Forward*
  • analogWrite(3, 0); //Adjust speed of motor A*
  • digitalWrite(8,HIGH); //Brake Motor B*
  • digitalWrite(13, HIGH); //Motor B Forward *
  • analogWrite(11, 0); //adjust speed of motor B*
  • break; *
  • case left:*
  • digitalWrite(9,LOW); //Brake Motor A*
  • digitalWrite(12, HIGH); //Motor A Forward*
  • analogWrite(3, 0); //Adjust speed of motor A*
  • digitalWrite(8,LOW); //Brake Motor B*
  • digitalWrite(13, HIGH); //Motor B Forward *
  • analogWrite(11, 255); //adjust speed of motor B*
  • break; *
  • case right:*
  • digitalWrite(9,LOW); //Brake Motor A*
  • digitalWrite(12, HIGH); //Motor A Forward*
  • analogWrite(3, 255); //Adjust speed of motor A*
  • digitalWrite(8,LOW); //Brake Motor B*
  • digitalWrite(13, HIGH); //Motor B Forward *
  • analogWrite(11, 0); //adjust speed of motor B*
  • break; *
  • }*
  • Serial.println(value); // prints codes to serial monitor*
  • irrecv.resume(); // Receive the next value*
    }
    }

So the codes received "change".. are they random, changing values?

Sounds like electrical interference from the PWM switching.. Maybe.

Try shielding the wiring to the IR receiver and separating it from the motor wiring. Try adding a capacitor (maybe .1uF) across the Vcc and Gnd pins right at the IR receiver.

See the IR info on the ArduinoInfo.Info WIKI HERE:

Let us know what happens; I'd like to add info to the WIKI if possible.

I will check that out when I get home, but it makes sense. I had the same issue with cross feed when using a ping sensor on a servo. I tried to use ribbon cable to make it all pretty like and separating the wires fixed the problem.

Separating the wiring and adding the capacitor didn't work.
The codes change to the same values every time, but when I put the "new" codes in the sketch, the serial monitor gives me the original codes. When I change the speed on a different section of the sketch, the IR codes change to a different value again.
Also, switching to a different remote didn't help either.
I am going to see if going to an H-bridge will help but that may take a couple of days before I have the time.

Switching to an H-Bridge fixed the speed/PWM issue. Don't know why, but it works now.

Hi horde111,
I came across the same problem as you.
IR codes change because of using pin 11 for analog write. Use pin 11 as digital out and use pin 9 or 10 as PWM analogwrite. Then IR codes do not change. I think as pin11 is a MOSI pin codes change though i do not know why. It has been a long time since the last post. I know u would have moved on. But this may be of some help to others making the same mistake as us.

Came across this aswell. I don't think it gets too old..

Arduino Mega2560 with custom shield and 3.5" touch screen attached.
In addition, Si4703 radio and DT22 sensor.
The little black remote works fine to start with, but after a period, all remote codes change to, as described in first msg. in this topic.

Original code for forwards is:
18:39:55.862 -> 0xFFC23D

Changing to:
18:39:54.198 -> 0x6E4E627B
18:39:54.639 -> 0x4CB0FADD

The Shield for the Touch Screen is permanent. Will it help switching a timer in the library? (Not all of those will work, but possibly one)