One of my motors doesn't seem to work whenever I try using an ir remote [solved]

Edit: the arduino I'm using is UNO and the motor that doesn't spin is the right motor.

Edit2: It seems I had the incorrect version of my sketch here is the current one.

#include <IRremote.h>

int RECV_PIN = 2;
IRrecv irrecv(RECV_PIN);
decode_results results;

int rightEnable = 3;
int in1Pin = 5;
int in2Pin = 4;

int leftEnable = 10;
int in3Pin = 8;
int in4Pin = 9;

byte speedVarOne = 5;
byte speedVarTwo = 5;

boolean foward = !true;
boolean backwards = true;

boolean reverseOne = foward;
boolean reverseTwo = foward;


void setup()
{

  Serial.begin(9600);

  pinMode(in1Pin, OUTPUT);
  pinMode(in2Pin, OUTPUT);
  pinMode(rightEnable, OUTPUT);

  pinMode(in3Pin, OUTPUT);
  pinMode(in4Pin, OUTPUT);
  pinMode(leftEnable, OUTPUT);
  
  irrecv.enableIRIn();

}

void loop()
{

  if (irrecv.decode(&results)) {
    translateIR();
    irrecv.resume(); // Receive the next value
  }

  int speedOne = round(speedVarOne*(255/9));
  int speedTwo = round(speedVarTwo*(255/9));

  setMotor(speedOne, reverseOne, leftEnable, in1Pin, in2Pin);
  setMotor(speedTwo, reverseTwo, rightEnable, in3Pin, in4Pin);

  
}

There is probably a timer conflict between the IRremote library and the PWM output, which depends on the particular Arduino you have.

Details on the library page: GitHub - Arduino-IRremote/Arduino-IRremote: Infrared remote library for Arduino: send and receive infrared signals with multiple protocols

What version of the IRremote library do you have installed? The posted code is written for an older version of the library. See the IRremote GitHub page to see the differences and how to fix older code to run under the newer versions.

I'm unsure which version I have because when I initially downloaded the newest version, I was receiving the same signal from every button (FFFFFFFF) I tried to fix that initially, but it didn't seem to work so I looked into the website from where I bought the ir remote and there was an Irremote library there. (Arduino Kits User Support – ELEGOO Official this would be the website and the kit I got was the UNO r3 super starter kit as I just needed extra parts since I already had some and this was the one that provided the most for the most reasonable value)

Not sure your code should always set reverseOne and reverseTwo false? Did you mean to do that. Surely they should be conditional on something.

Yes they'll be conditional, but at the moment I decided to focus more on the IR remote part so I made them have a set value.

Unfortunately your code in post #1 does not work; there is a line "* List item" and also the variables rightEnable and leftEnable are not declared. Not a big deal of course, but maybe you could check if you have posted the correct sketch?

I found this with the Arduino IRremote Lib:

Source: https://github.com/Arduino-IRremote/Arduino-IRremote#receiving-ir-codes

which might be interesting also. It does not explain the motor issue but probably why you got "FFFFFF" for all received codes ...:

and why you may have to rearrange the data in the switch cases to fit to the new order if you use the new(er) library.

However, it seems to apply only if one uses #include <IRremote.hpp>

Did you connect the rightEnable or leftEnable to pin 11? I found that does not work.

You can use e.g. pins 5 and 6 ... at least Wokwi works with this:

#include <IRremote.h>

int RECV_PIN = A0;
IRrecv irrecv(RECV_PIN);
decode_results results;

//* List item

int rightMotor= 3;
int in1Pin = 11;
int in2Pin = 4;

int rightEnable = 5; 

int leftMotor = 10;
int in3Pin = 8;
int in4Pin = 9;

int leftEnable = 6; 



byte speedVarOne = 5;
byte speedVarTwo = 5;

void setup()
{

  Serial.begin(9600);

  pinMode(in1Pin, OUTPUT);
  pinMode(in2Pin, OUTPUT);
  pinMode(rightEnable, OUTPUT);

  pinMode(in3Pin, OUTPUT);
  pinMode(in4Pin, OUTPUT);
  pinMode(leftEnable, OUTPUT);
  
  irrecv.enableIRIn();

}

void loop()
{

  if (irrecv.decode()) {
    translateIR();
    irrecv.resume(); // Receive the next value
  }

  int speedOne = round(speedVarOne*((1023/4)/9));
  int speedTwo = round(speedVarTwo*((1023/4)/9));

  boolean reverseOne = !true;
  boolean reverseTwo = !true;


  setMotor(speedTwo, reverseTwo, rightEnable, in3Pin, in4Pin);
  setMotor(speedOne, reverseOne, leftEnable, in1Pin, in2Pin);

  
}

void setMotor(int speed, boolean reverse, int enablePin, int pinOne, int pinTwo)
{
  analogWrite(enablePin, speed);
  digitalWrite(pinOne, ! reverse);
  digitalWrite(pinTwo, reverse);
  delay(100);
}

void translateIR() // takes action based on IR code received describing Car MP3 IR codes 
{
  unsigned long key = irrecv.decodedIRData.decodedRawData >> 8;
  unsigned long oldKey = ((key & 0xFF) << 16) + ((key & 0xFF0000) >> 16) + (key & 0x00FF00);
  switch(oldKey){
  case 0xFFA25D:  
    Serial.println(" power            "); 
    break;
  case 0xFF629D:  
    Serial.println(" VOL +             "); 
    break;
  case 0xFFE21D:  
    Serial.println(" FUN/STOP            "); 
    break;
  case 0xFF22DD:  
    Serial.println(" SKIP BACK          "); 
    break;
  case 0xFF02FD:  
    Serial.println(" PAUSE/PLAY        "); 
    break;
  case 0xFFC23D:  
    Serial.println(" SKIP FOWARD     "); 
    break;
  case 0xFFE01F:  
    Serial.println(" DOWN           "); 
    break;
  case 0xFFA857:  
    Serial.println(" VOL-           "); 
    break;
  case 0xFF906F:  
    Serial.println(" UP            "); 
    break;
  case 0xFF6897:  
    Serial.println(" 0              "); 
    speedVarOne = 0;
    speedVarTwo = 0;
    break;
  case 0xFF9867:  
    Serial.println(" EQ           "); 
    break;
  case 0xFFB04F:  
    Serial.println(" ST/REPT           "); 
    break;
  case 0xFF30CF:  
    Serial.println(" 1              "); 
    speedVarOne = 1;
    speedVarTwo = 1;    
    break;

  case 0xFF18E7:  
    Serial.println(" 2              "); 
    speedVarOne = 2;
    speedVarTwo = 2;    
    break;

  case 0xFF7A85:  
    Serial.println(" 3              "); 
    speedVarOne = 3;
    speedVarTwo = 3;    
    break;

  case 0xFF10EF:  
    Serial.println(" 4              "); 
    speedVarOne = 4;
    speedVarTwo = 4;    
    break;

  case 0xFF38C7:  
    Serial.println(" 5              "); 
    speedVarOne = 5;
    speedVarTwo = 5;    
    break;

  case 0xFF5AA5:  
    Serial.println(" 6              "); 
    speedVarOne = 6;
    speedVarTwo = 6;    
    break;

  case 0xFF42BD:  
    Serial.println(" 7              "); 
    speedVarOne = 7;
    speedVarTwo = 7;  
    break;

  case 0xFF4AB5:  
    Serial.println(" 8              "); 
    speedVarOne = 8;
    speedVarTwo = 8;      
    break;

  case 0xFF52AD:  
    Serial.println(" 9              "); 
    speedVarOne = 9;
    speedVarTwo = 9;      
    break;

  default: 
    Serial.println(" unknown button   ");
    Serial.println(key,HEX);
    Serial.println(oldKey,HEX);

  }

  delay(50);


}

Feel free to check it out... Good luck!

I ended up finding my solution, apparently one of the pin I was using for one of the motor was being used by the ir remote library.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.