I am facing the same issue with my arduino uno using 4 IR sensors .even though i have created the four objects for them they are all sending the output when only one is supposed to receive the signal at a time . and even my arduino uno has its own ir transmitter along with the ir sensors so i am having trouble in running both at the same time. here is my code suggest me any improvement for 4 ir sensor data reading.
#include <IRremote.h>
int val = 0,DeviceId;
char str1[20];
String x;
IRsend irsend;
int RECV_PIN_T = 11;
int RECV_PIN_L = 9;
int RECV_PIN_B = 10;
int RECV_PIN_R = 8;
const int analogPin_T = A0;
const int analogPin_L=A1;
const int analogPin_B=A2;
const int analogPin_R=A3;
int analogValue=0;
IRrecv irrecv_T(RECV_PIN_T);
IRrecv irrecv_L(RECV_PIN_L);
IRrecv irrecv_B(RECV_PIN_B);
IRrecv irrecv_R(RECV_PIN_R);
decode_results results_T;
decode_results results_L;
decode_results results_B;
decode_results results_R;
void setup()
{
Serial.begin(9600);
pinMode(13, OUTPUT);
pinMode(analogPin_T, INPUT);
pinMode(RECV_PIN_T,OUTPUT);
pinMode(RECV_PIN_L,OUTPUT);
pinMode(RECV_PIN_B,OUTPUT);
pinMode(RECV_PIN_R,OUTPUT);
Serial.println("Enabling IRin");
irrecv_T.enableIRIn();
irrecv_L.enableIRIn();
irrecv_B.enableIRIn();
irrecv_R.enableIRIn();
Serial.println("Enabled IRin");
delay(100);
}
void loop ()
{
//condition for serial input data ,if available then only execute this step
if (Serial.available() ) {
int Counter = 0;
while((val = Serial.read()) != ':')
{
if(val!= -1)
{
str1[Counter] = val;
++Counter;
}
}
processData();
str1[Counter] = ':';
str1[Counter+1] = '\0';
Serial.println(str1);
}
//send();
int i=0;
for(i=0;i<=3;++i)
{
if (irrecv_T.decode(&results_T)) {
Serial.println(results_T.value, HEX);
dump_T(&results_T);
Serial.println("TOP");
}
}
for(i=4;i<=7;++i)
{
if (irrecv_L.decode(&results_L)) {
Serial.println(results_L.value, HEX);
dump_L(&results_L);
Serial.println(" LEFT");
}
//irrecv.resume(); // Receive the next value
}
for(i=8;i<=11;++i)
{
if (irrecv_L.decode(&results_B)) {
Serial.println(results_B.value, HEX);
dump_B(&results_B);
Serial.println(" BOTTOM");
}
}
for(i=12;i<=15;++i)
{
if (irrecv_R.decode(&results_R)) {
Serial.println(results_R.value, HEX);
dump_R(&results_R);
Serial.println(" RIGHT");
}
}
if(i>15)
i=0;
}
void processData() {
if(str1[0]=='D'&&str1[1]=='E'||str1[2]=='V'||str1[3]=='I'||str1[4]=='C'||str1[5]=='E'||str1[6]=='I'||str1[7]=='D')
{
// Serial.println(str1[8]);
//Serial.println(str1[9]);
Serial.println(str1);
digitalWrite(13,HIGH);
Serial.println("Enter the desired device id");
// while(Serial.available()>0){Serial.read();} //trying to flush the serial read
//if (Serial.available() ) {
x=Serial.readString();
//Serial.println("the value of x is ");
//Serial.print(x);
DeviceId=atoi(x.c_str());
Serial.println("The set Device Id is ");
Serial.print(DeviceId);
}
//}
else
{
Serial.println("off");
digitalWrite(13,LOW);
}
}
void send()
{
int khz = 38; // set the frequency as per the protocol 38 khz for NEC
irsend.sendRaw(DeviceId, sizeof(DeviceId), khz); //Note the approach used to automatically calculate the size of the array.
delay(500);
}
void dump_T(decode_results *results_T) {
// Dumps out the decode_results structure.
// Call this after IRrecv::decode()
int count = results_T->rawlen;
if (results_T->decode_type == UNKNOWN) {
Serial.print("Unknown encoding: ");
}
else if (results_T->decode_type == NEC) {
Serial.print("Decoded NEC: ");
}
if (results_T->decode_type == LG) {
Serial.print("Decoded LG: ");
}
Serial.print(results_T->value, HEX);
Serial.print(" (");
Serial.print(results_T->bits, DEC);
Serial.println(" bits)");
delay(1000);
}
void dump_L(decode_results *results_L) {
int count = results_L->rawlen;
if (results_L->decode_type == UNKNOWN) {
Serial.print("Unknown encoding: ");
}
else if (results_L->decode_type == NEC) {
Serial.print("Decoded NEC: ");
}
if (results_L->decode_type == LG) {
Serial.print("Decoded LG: ");
}
Serial.print(results_L->value, HEX);
Serial.print(" (");
Serial.print(results_L->bits, DEC);
Serial.println(" bits)");
delay(1000);
}
void dump_B(decode_results *results_B) {
int count = results_B->rawlen;
if (results_B->decode_type == UNKNOWN) {
Serial.print("Unknown encoding: ");
}
else if (results_B->decode_type == NEC) {
Serial.print("Decoded NEC: ");
}
if (results_B->decode_type == LG) {
Serial.print("Decoded LG: ");
}
Serial.print(results_B->value, HEX);
Serial.print(" (");
Serial.print(results_B->bits, DEC);
Serial.println(" bits)");
delay(1000);
}
void dump_R(decode_results *results_R) {
int count = results_R->rawlen;
if (results_R->decode_type == UNKNOWN) {
Serial.print("Unknown encoding: ");
}
else if (results_R->decode_type == NEC) {
Serial.print("Decoded NEC: ");
}
if (results_R->decode_type == LG) {
Serial.print("Decoded LG: ");
}
Serial.print(results_R->value, HEX);
Serial.print(" (");
Serial.print(results_R->bits, DEC);
Serial.println(" bits)");
delay(1000);
}