Using multiple IR sensors on same board

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

}

Hi,
Welcome to the forum.

Please read the post at the start of any forum , entitled "How to use this Forum".
OR
http://forum.arduino.cc/index.php/topic,148850.0.html.
Then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Thanks.. Tom... :slight_smile:
PS I have told the moderator that you need to be given your own thread.

In future do not hijack 4 year old topics

Follow the advice in the link given in post #1 about posting code

pidurkar_paras:
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);

}

This post belongs in programming questions.
[Mods!!]

Anyhow, light can spread as much as it wants, unless you use a laser or something focused.
Depending on the construction, the ir light is very possibly reaching multiple sensors. So all the sensors are sending data.
Also, why do you need 4 sensors? Is it for 4 sides? I believe there exists tricks to use 1 sensor for all directions.

Hi,
Here is an example code for that library;

/*
 * IRremote: IRrecvDemo - demonstrates receiving IR codes with IRrecv
 * An IR detector/demodulator must be connected to the input RECV_PIN.
 * Version 0.1 July, 2009
 * Copyright 2009 Ken Shirriff
 * http://arcfn.com
 */
#include <IRremote.h>
int RECV_PIN = 11;
IRrecv irrecv(RECV_PIN);
decode_results results;

void setup()
{
  Serial.begin(9600);
  // In case the interrupt driver crashes on setup, give a clue
  // to the user what's going on.
  Serial.println("Enabling IRin");
  irrecv.enableIRIn(); // Start the receiver
  Serial.println("Enabled IRin");
}

void loop() {
  if (irrecv.decode(&results)) {
    Serial.println(results.value, HEX);
    irrecv.resume(); // Receive the next value
  }
  delay(100);
}

Why are you making your receive pins OUTPUTs?

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

Tom... :slight_smile:

Your duplicate post has been deleted

Cross-posting is against the rules of the forum. The reason is that duplicate posts can waste the time of the people trying to help. Someone might spend 15 minutes (or more) writing a detailed answer on this topic, without knowing that someone else already did the same in the other topic.

Repeated cross-posting will result in a suspension from the forum.

In the future, please take some time to pick the forum board that best suits the topic of your question and then only post once to that forum board. This is basic forum etiquette, as explained in the sticky "How to use this forum - please read." post you will find at the top of every forum board. It contains a lot of other useful information. Please read it.

Thanks in advance for your cooperation.