Hello,
I am experiencing a strange issue when I use the IR Remote library in the same code as a motor controller. The command to enable the IR receiver (irrecv.enableIRIn();) inexplicably prevents the left relay from working at all. I have tried moving all inputs to different pins, using different Arduino boards, and removing components, but the irrecv.enableIRIn(); command always disables the left relay on the motor controller. Will you please help me solve this issue?
The IR Remote library I am using is included. The code below is simply a test of the relay's functions. When the command irrecv.enableIRIn(); is commented out, both relays work. When it is active, the left relay does not work at all.
IRremote.zip (33.8 KB)
// Motors: 2x motors are used to drive the wheels and move the car.
#define enL 11 // Define the ports for each pin of the motor drive module. (enL = enA on the module.)
#define inL1 10 // in1
#define inL2 9 // in2
#define enR 6 // enB
#define inR1 8 // in3
#define inR2 7 // in4
int randfreeroam; // Randomly generated variable that determines the response to an obstacle.
// IR Remote: Unit can receive signals from an IR remote to control movement.
//www.elegoo.com
//2016.12.9
#include "IRremote.h"
int receiver = A0; // Signal Pin of IR receiver to Arduino Analog Pin 0
//-----( Declare objects )-----
IRrecv irrecv(receiver); // create instance of 'irrecv'
decode_results results; // create instance of 'decode_results'
void setup() { // Setup all inputs and outputs
Serial.begin(9600); // Enables Arduino IDE to read Serial messages from the Arduino
pinMode(enL, OUTPUT); // Set all motor functions as outputs
pinMode(enR, OUTPUT);
pinMode(inL1, OUTPUT);
pinMode(inL2, OUTPUT);
pinMode(inR1, OUTPUT);
pinMode(inR2, OUTPUT);
irrecv.enableIRIn(); // Start the IR receiver. <-- THIS COMMAND CAUSES THE PROBLEM
delay(1000); // Delay before beginnning
}
void loop() { // Main program
forward();
Serial.println("forward");
delay(1000);
left();
Serial.println("left");
delay(1000);
right();
Serial.println("right");
delay(1000);
back();
Serial.println("back");
delay(1000);
stop();
Serial.println("stop");
delay(1000);
}
void right() { // Spin the left wheel forward and the right wheel back.
Serial.println("right");
analogWrite(enL, 175);
analogWrite(enR, 175);
digitalWrite(inL1, LOW);
digitalWrite(inL2, HIGH);
digitalWrite(inR1, HIGH);
digitalWrite(inR2, LOW);
}
void forward() { // Spin both wheels forward.
Serial.println("forward");
analogWrite(enL, 175);
analogWrite(enR, 175);
digitalWrite(inL1, LOW);
digitalWrite(inL2, HIGH);
digitalWrite(inR1, LOW);
digitalWrite(inR2, HIGH);
}
void left() { // Spin the right wheel forward and the left wheel back.
Serial.println("left");
analogWrite(enL, 175);
analogWrite(enR, 175);
digitalWrite(inL1, HIGH);
digitalWrite(inL2, LOW);
digitalWrite(inR1, LOW);
digitalWrite(inR2, HIGH);
}
void back() { // Spin both wheels backward.
Serial.println("back");
analogWrite(enL, 175);
analogWrite(enR, 175);
digitalWrite(inL1, HIGH);
digitalWrite(inL2, LOW);
digitalWrite(inR1, HIGH);
digitalWrite(inR2, LOW);
}
void stop() { // Stop both wheels.
Serial.println("stop");
digitalWrite(enL, LOW);
digitalWrite(enR, LOW);
}