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
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);
}
}
}