I’m making a smart car using an arduino uno, sensor shield v5, motor control with a L298N and a servo motor + HC-SR04 ultrasound sensor. I want to add a IR receiver on the board so I can also control the car with a remote.
The problem is when I add in the code for the IR Receiver and make the 3 additional connections, the right motor doesn’t respond anymore, it only moves at the beginning, though slower than the left one during the initial setup, then only the left motor functions properly.
Here is the whole code with comments next to the IR-Receiver specific code:
#include <Servo.h>
#include <IRremote.h> //IR Receiver
Servo servo ;
const int trigPin = 13;
const int echoPin = 12;
const int servoPin = 11;
const int enAPin = 6;
const int in1Pin = 7;
const int in2Pin = 5;
const int in3Pin = 4;
const int in4Pin = 2;
const int enBPin = 3;
const int receiverPin = 9; //IR Receiver
IRrecv irrecv(receiverPin); //IR Receiver
decode_results signals; //IR Receiver
enum Motor { LEFT, RIGHT };
void go(enum Motor m, int speed)
{
digitalWrite (m == LEFT ? in1Pin : in3Pin , speed > 0 ? HIGH : LOW );
digitalWrite (m == LEFT ? in2Pin : in4Pin , speed <= 0 ? HIGH : LOW );
analogWrite(m == LEFT ? enAPin : enBPin, speed < 0 ? -speed : speed );
}
void testMotors ()
{
static int speed[8] = { 128, 255, 128, 0 , -128, -255, -128, 0};
go(RIGHT, 0);
for (unsigned char i = 0 ; i < 8 ; i++)
go(LEFT, -speed[i]), delay(200);
for (unsigned char i = 0 ; i < 8 ; i++)
go(RIGHT, speed[i]), delay(200);
}
unsigned int readDistance()
{
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
unsigned long period = pulseIn(echoPin, HIGH);
return period * 343 / 2000;
}
#define NUM_ANGLES 7
unsigned char sensorAngle[NUM_ANGLES] = {60, 70, 80, 90, 100, 110, 120};
unsigned int distance [NUM_ANGLES];
void readNextDistance()
{
static unsigned char angleIndex = 0;
static signed char step = 1;
distance[angleIndex] = readDistance();
angleIndex += step ;
if (angleIndex == NUM_ANGLES - 1) step = -1;
else if (angleIndex == 0) step = 1;
servo.write(sensorAngle[angleIndex]);
}
void setup () {
Serial.begin(9600); //IR Receiver
irrecv.enableIRIn(); //IR Receiver
pinMode(trigPin , OUTPUT);
pinMode(echoPin, INPUT);
digitalWrite(trigPin, LOW);
pinMode(enAPin, OUTPUT);
pinMode(in1Pin, OUTPUT);
pinMode(in2Pin, OUTPUT);
pinMode(in3Pin, OUTPUT);
pinMode(in4Pin, OUTPUT);
pinMode(enBPin, OUTPUT);
servo.attach(servoPin);
servo.write(90);
go(LEFT, 0);
go(RIGHT, 0);
testMotors();
servo.write(sensorAngle[0]);
delay(200);
for (unsigned char i = 0 ; i < NUM_ANGLES ; i ++)
readNextDistance(), delay (200);
}
void loop () {
//IR Receiver:
if (irrecv.decode(&signals)) {
Serial.println(signals.value);
irrecv.resume();
if (signals.value == 0xFFA25D) {
Serial.println("yeees");
}
}
readNextDistance ();
unsigned char tooClose = 0;
for (unsigned char i = 0 ; i < NUM_ANGLES ; i++)
if (distance[i] < 200)
tooClose = 1;
if (tooClose) {
go(LEFT, 180);
go(RIGHT, -80);
} else {
go(LEFT, -180);
go(RIGHT, 180);
}
delay (50);
}
1 Answer 1
Finally found the problem: it is due to the IRremote library using the same timers than the Servo.
Solutions are to change the specified timer that the IRremote library uses in the library files (didn't work for me though) or to not use a library for either the IR receiver or the Servo or to change the physical connection of the component plugged in a pin also used by a timer.
Link to an Arduino Forum post suggesting an alternative for controlling the Servo without a library
Explore related questions
See similar questions with these tags.
//IR Receiver
? Maybe you should also post the working code (that doesn't have the IR receiver code included), so that we may compare the two versions ourselves?//IR Receiver
, then you have the working code.