I wrote a program which would put on or off my room lights and fan, but when I try it, sometimes my Arduino detects different codes from the same control button, I do not Know whats going on or why, could someone help me?
Here I add a part of the code because it is so long
Please, help me, Thank you
#include <IRremote.h>
#include <SoftwareSerial.h>
SoftwareSerial Bluetooth (11, 10); //Tx & Rx
const int infra = 3;
const int light = 4;
const int desktop = 5;
const int fan = 6;
//bool state = false;
bool li = false;
bool des = false;
bool fa = false;
int timer = 0;
String words;
char text[25];
int n = 0;
int modes = 1;
const int photo = 9;
const int sound = 8;
const int BT = 7;
IRrecv irrecv(infra);
decode_results code;
void setup() {
Serial.begin(9600);
irrecv.enableIRIn();
Bluetooth.flush();
delay(500);
Bluetooth.begin(9600);
pinMode (13, OUTPUT);
pinMode (light, OUTPUT);
pinMode (desktop, OUTPUT);
pinMode (fan, OUTPUT);
pinMode (BT, OUTPUT);
pinMode (12, INPUT);
}
void loop() {
digitalWrite(BT, HIGH);
while (modes == 1){
if (irrecv.decode(&code) || (Bluetooth.available() > 0)){
Serial.println(code.value, HEX);
while (Bluetooth.available() > 0){
text[n] = char(Bluetooth.read());
Serial.println (text);
n++;
}
words = text;
Serial.print(words);
//Serial.println(code.value, HEX);
if (code.value == 0xFF30CF || words == "li")
li = !li;
if (code.value == 0xFF18E7 || words == "des")
des = !des;
if (code.value == 0xFF7A85 || words == "fa")
fa = !fa;
if (code.value == 0xFFE21D || words == "all"){
li = false;
des = false;
fa = false;
}
if (code.value == 0xFFA25D || words == "none"){
li = true;
des = true;
fa = true;
}
if (code.value == 0xFF9867)
modes = 2;
if (code.value == 0xFFB04F)
modes = 3;
//timer = 1;
irrecv.resume();
delay(500);
}
if (li == false){
digitalWrite (light, LOW);
}else
digitalWrite (light, HIGH);
if (des == false){
digitalWrite (desktop, LOW);
}else
digitalWrite (desktop, HIGH);
if (fa == false){
digitalWrite (fan, LOW);
}else
digitalWrite (fan, HIGH);
words = "xyz";
while (n >= 0){
text[n] = '\0';
if (n == 0)
break;
n--;
}
}