Hi,
I am using iRremote.h library.
I want to receive ir signal and based on ir signal I have used switch to diect it to three different cases. In one of the case I am using switch again after receiving the signal. Second time when I am using the switch statement I have used the if statement
if (irrecv.decode(&results)).
It seems that the problem is not reading this statement. I think the problem is in using the statement
irrecv.resume();
I have tried it to place it in various places but nothing seems to work.
Could you please see my code and tell me where the problem is
My code is as follows
#include <Wire.h>
#include <IRremote.h>
int RECV_PIN = 11;
IRrecv irrecv(RECV_PIN);
decode_results results;
void setup()
{
Serial.begin(9600);
irrecv.enableIRIn();
}
void loop()
{
if (irrecv.decode(&results)){ // have we received an IR signal?
Serial.println(results.value, HEX);
switch(results.value)
{
case 0x7:
Serial.println(“q”);
break;
case 0x8:
Serial.println(“w”);
break;
case 0x9:
Serial.println(“e”);
unsigned long sec;
unsigned long B;
B=millis();
sec=millis()-B;
while(sec < 120000){
if (irrecv.decode(&results)){ // have we received an IR signal?
Serial.println(results.value, HEX);
switch(results.value){
case 0x21:
Serial.println(“A”);
break;
case 0x20:
Serial.println(“B”);
break;
case 0x11:
Serial.println(“c”);
break;
case 0x10:
Serial.println(“d”);
break;
}
irrecv.resume();
}
sec=millis()-B;
Serial.println(sec);
}
Serial.println(“done”);
break;
}
irrecv.resume();
}
}
If you use the code tags your code looks like this …
#include <Wire.h>
#include <IRremote.h>
int RECV_PIN = 11;
IRrecv irrecv(RECV_PIN);
decode_results results;
void setup()
{
Serial.begin(9600);
irrecv.enableIRIn();
}
void loop()
{
if (irrecv.decode(&results)) { // have we received an IR signal?
Serial.println(results.value, HEX);
switch (results.value)
{
case 0x7:
Serial.println("q");
break;
case 0x8:
Serial.println("w");
break;
case 0x9:
Serial.println("e");
unsigned long sec;
unsigned long B;
B = millis();
sec = millis() - B;
while (sec < 120000) {
if (irrecv.decode(&results)) { // have we received an IR signal?
Serial.println(results.value, HEX);
switch (results.value) {
case 0x21:
Serial.println("A");
break;
case 0x20:
Serial.println("B");
break;
case 0x11:
Serial.println("c");
break;
case 0x10:
Serial.println("d");
break;
}
irrecv.resume();
}
sec = millis() - B;
Serial.println(sec);
}
Serial.println("done");
break;
}
irrecv.resume();
}
}
See how much easier it is to read ? I’m not sure, since you didn’t say, how this “system” is intended to work. I infer from the code above that you press the a button on the remote (IR) control and it’s received by some Arduino based receiver. Then after a 0x09, an “e” per your code, it’s supposed to wait up to 120 secs for another IR transmission. If that 2’nd IR transmission/byte = 0x10 or 0x11 or 0x20 or 0x21, then there’s supposed to be another printout (of A, B, C or D) depending on that 2’nd button press.
I’m not sure I agree w/all the code but I think a
irrecv.resume();
belongs after/below the 1’st receipt of the case = 0x9 message and just before/above the while() looping.