IRremote messing things up

I am new to Arduino and wanted to start a little project to practice my Arduino abilities.
I wanted to use the IR remote library to switch between modes in my project, but for some reason when I added the IR remote feature It started messing things up, and things that weren't supposed to happen happened.

I know that all my coding is correct, because I have used the same IR concept and code in another project, and it worked completely fine. And when I remove the IR feature, everything works fine.

I did some testing to find the problem and try to fix it, after some time I finally found the issue, but I have no idea why it happens and how to fix it. When I remove ''IR.enableIRIn();'' it works fine (without being able to use remote), but when I add it, it messes things up.

If anyone knows what's wrong it would be a huge help if you could tell me or if you know any alternate way I could try. Thanks :slight_smile:

Edit:
I also did a test which you can see in the code, where I printed the variable ''mode'' and it was working as intended (Printing 1 when I press 1 and printing 2 when I press 2).

//Remote
#include <IRremote.h>
#define one 0xFFA25D
#define two 0xFF629D


int RECV_PIN=5;
int IRcode;
IRrecv IR(RECV_PIN);
decode_results results;

int mode;

//motor
int M10=10;
int M11=11;
int M9=9;
int M3=3;

//line follower
int IRLFHpin=12;
int IRLFVpin=8;
int IRLFHstate;
int IRLFVstate;

//obstacle avoider
int IROAHpin=7;
int IROAVpin=6;
int IROAHstate;
int IROAVstate;


void setup() {
//Remote
IR.enableIRIn(); //When I remove this the code works, but when I add it, it messes things up. Everything else works fine.
IR.blink13(true);

//motor
pinMode(M10, OUTPUT);
pinMode(M11, OUTPUT);
pinMode(M9, OUTPUT);
pinMode(M3, OUTPUT);

//line follower
pinMode(IRLFHpin, INPUT);
pinMode(IRLFVpin, INPUT);

//obstacle avoider
pinMode(IROAHpin, INPUT);
pinMode(IROAVpin, INPUT);

Serial.begin(9600);
}

void loop() {

//Remote
if(IR.decode(&results)){
  
  switch(results.value){

    case one: 
    mode=1; 
    break;

    case two:
    mode=2; 
    break;

  }
  Serial.print("Code & Mode: ");
  Serial.print(results.value, HEX );
  Serial.println(mode);
  IR.resume();
  delay(500);
  }

//obstacle avoider

if(mode==1){
 IROAHstate=digitalRead(IROAHpin);
 IROAVstate=digitalRead(IROAVpin);

 if(IROAVstate==false && IROAHstate==true){
 analogWrite(M10, 125);
 analogWrite(M11, 0);

 analogWrite(M9, 50);
 analogWrite(M3, 0);
 delay(500);
 }

 if(IROAVstate==true && IROAHstate==false){
 analogWrite(M10, 0);
 analogWrite(M11, 50);

 analogWrite(M9, 0);
 analogWrite(M3, 125);
 delay(500);
 }

 if(IROAVstate==true && IROAHstate==true){
 analogWrite(M10, 50);
 analogWrite(M11, 0);

 analogWrite(M9, 0);
 analogWrite(M3, 50);
 delay(500);
 }

 if(IROAVstate==false && IROAHstate==false){
 analogWrite(M10, 0);
 analogWrite(M11, 255);

 analogWrite(M9, 100);
 analogWrite(M3, 0);
 delay(500);
 }

}

//line follower

if(mode==2){

IRLFVstate=digitalRead(IRLFVpin);
IRLFHstate=digitalRead(IRLFHpin);

if(IRLFVstate==false && IRLFHstate==true){
analogWrite(M10, 255);
analogWrite(M11, 0);

analogWrite(M9, 0);
analogWrite(M3, 0);
}

if(IRLFVstate==true && IRLFHstate==false){
analogWrite(M10, 0);
analogWrite(M11, 0);

analogWrite(M9, 0);
analogWrite(M3, 255);
}

if(IRLFVstate==false && IRLFHstate==false){
analogWrite(M10, 30);
analogWrite(M11, 0);

analogWrite(M9, 0);
analogWrite(M3, 30);
}

if(IRLFVstate==true && IRLFHstate==true){
analogWrite(M10, 0);
analogWrite(M11, 0);

analogWrite(M9, 0);
analogWrite(M3, 0);
}

}

}

I think the problem is that IRRemote uses Timer1 so you can't use PWM (analogWrite()) on pins 9 or 10 (assuming Arduino UNO). Pick other pins for your motor control.

What version of the IRremote library are you using? The newest version (>3.0) does not use the decode_results struct. See the IRremote GitHub page.

Since the decoded values are now in IrReceiver.decodedIRData and not in results any more, remove the line decode_results results or similar.

As I said, I've used the exact same IR code in another project (with decode_results), and it worked completely fine. I also did a test which you can see in the code, where I printed the variable ''mode'' and it was working as intended (Printing 1 when I press 1 and printing 2 when I press 2)

It is pretty important for it to use analog pins.

I was merely pointing out what the documentation says.

Ignore the warnings if you want.

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