Now, I am trying to connect two IR receivers and I have the following code, but it does only detect from one IR detector, can anyone please provide me with clue on why this only read from one sensor? It only reads from pin 11. I would very much appreciate your help, thank you */
#include <IRremote.h> // Include the IRremote library
const int buzzPin = 6;
int RECV_PIN_1 = 11;
int RECV_PIN_2 = 10;
/* Initialize the irrecv part of the IRremote library */
IRrecv irrecv1(RECV_PIN_1);
IRrecv irrecv2(RECV_PIN_2);
decode_results results1; // This will store our IR received codes
decode_results results2; // This will store our IR received codes
void setup()
{
Serial.begin(9600); // Use serial to debug.
irrecv1.enableIRIn(); // Start the receiver
irrecv2.enableIRIn(); // Start the receiver
pinMode(13, OUTPUT); //declare pin 13 as an output
}
void loop()
{
digitalWrite(13, HIGH); // Turn on the LED
delay(50); // Wait for one second
digitalWrite(13, LOW); // Turn off the LED
delay(50); // Wait for one second
if (irrecv1.decode(&results1))
{
// read the RX'd IR into a 16-bit variable:
uint16_t resultCode1 = (results1.value & 0xFFFF);
switch (resultCode1)
{
/*case 1:
;
case 2:
;
case 3:
*/
default:
Serial.print("IR detected_1: ");
Serial.println(results1.value, HEX);
buzz(50);
break;
}
irrecv1.resume(); // Receive the next value
}
if (irrecv2.decode(&results2))
{
// read the RX'd IR into a 16-bit variable:
uint16_t resultCode2 = (results2.value & 0xFFFF);
switch (resultCode2)
{
/*case 1:
;
case 2:
;
case 3:
*/
default:
Serial.print("IR detected_2: ");
Serial.println(results2.value, HEX);
buzz(50);
break;
}
irrecv2.resume(); // Receive the next value
}
} //endloop
void buzz(byte time)
{
analogWrite(buzzPin,170);
delay(time);
analogWrite(buzzPin,0);
delay(time);
}
Hello LarryD, Thanks for your help:
What happens when I switch the detectors around in hardware, in software?
The only pin that reads info from the IR receiver signal is the pin that is listed last in the code above the setup loop.
Why do you recommend to stop using delay()? What do you suggest to use instead to accomplish the same as a delay()?
Hello Wawa, Thanks for your help too.
My ultimate goal is to try using this IR receivers as proximity sensors.
//Simple BWD BlinkWithoutDelay examples
//Timer variables used
unsigned long currentMillis;
unsigned long pin13Millis;
unsigned long pin12Millis;
unsigned long SwitchMillis;
//if these are not changed in the sketch, they can be const
unsigned long debounceMillis = 50UL; //50ms
unsigned long ledOnTime = 500UL; //500ms seconds
byte lastSwitchState = HIGH;
byte buttonState = HIGH;
//enable/disable flags
boolean flag13 = true;
boolean flag12 = false;
const byte Switch = 2; //pushed = LOW
//**********************************************************************
void setup()
{
Serial.begin(9600);
digitalWrite(13,LOW);
pinMode(13, OUTPUT);
digitalWrite(12,LOW);
pinMode(12, OUTPUT);
pinMode(Switch, INPUT_PULLUP); //pushed = LOW
} // >>>>>>>>>>>>>> E N D O F s e t u p ( ) <<<<<<<<<<<<<<<<<
void loop()
{
//save the current time
currentMillis = millis();
//*************************************
//Heartbeat LED
//Toggle LED on and off. Helps show if there is blocking code
if (flag13 == true && currentMillis - pin13Millis >= ledOnTime)
{
pin13Millis = millis(); //re-initialize Timer
digitalWrite(13,!digitalRead(13)); //toggle LED condition
}
//*************************************
if (flag12 == true && currentMillis - pin12Millis >= 5*1000UL)
{
//Turn off pin 12
digitalWrite(12,LOW); //Turn off LED
flag12 = false; //disable timing
}
//*************************************
//is it time to check the switches?
if (currentMillis - SwitchMillis >= debounceMillis)
{
//code here runs every debounceMillis ms
SwitchMillis = millis(); //re-initilize Timer
//go and check the switches
checkSwitches();
}
//*********************************
//put other non-blocking stuff here
//*********************************
} // >>>>>>>>>>>>>> E N D O F l o o p ( ) <<<<<<<<<<<<<<<<<
//======================================================================
// F U N C T I O N S
//======================================================================
//****************** c h e c k S w i t c h e s ( ) *********************
//switches are checked every debounceValue milli seconds
//no minimum switch press time is validated with this code (i.e. No glitch filter)
void checkSwitches()
{
//re-usable for all the switches
boolean thisState;
//check if this switch has changed state
thisState = digitalRead(Switch);
if (thisState != lastSwitchState)
{
//update the switch state
lastSwitchState = thisState;
//this switch position has changed so do some stuff
//"LOW condition code"
//has switch gone from HIGH to LOW?
if(thisState == LOW)
{
//Do some LOW switch stuff here
flag12 = true; //allow timing
digitalWrite(12, HIGH); //turn on LED
pin12Millis = millis(); //initialize Timer
}
} //END of Switch code
//*****************************************
// similar code for other switches goes here
//*****************************************
} //END of checkSwitches()
//**********************************************************************
//======================================================================
// E N D O F C O D E
//======================================================================