1

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 ReceiverServo 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 ReceiverIRrecv irrecv(receiverPin);  //IR Receiverdecode_results signals; //IR Receiverenum 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 7unsigned 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);}
askedFeb 25, 2024 at 19:12
AugustinS's user avatar
4
  • Are you saying that the code that works correctly isonly different because it is missing the lines commented with//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?CommentedFeb 25, 2024 at 19:37
  • @Greenonline Yes, exactly. If you remove the few lines commented with//IR Receiver, then you have the working code.CommentedFeb 25, 2024 at 19:44
  • It seems to be the motors conflicting with the IRremote library, due to both usage of same timers. Need to find another library or find a way to make it use another timerCommentedFeb 26, 2024 at 9:41
  • Please post that as an answerCommentedFeb 26, 2024 at 13:00

1 Answer1

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 Servoor 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

answeredFeb 27, 2024 at 9:39
AugustinS's user avatar

Your Answer

Sign up orlog in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

By clicking “Post Your Answer”, you agree to ourterms of service and acknowledge you have read ourprivacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.