in loop function the "for" loop is running 2 times but for the second time "if" is not being executed i.e., the value of d is always zero. i feel the problem is with irrecv.resume(); in line 33. can anyone please help me with this
#include <IRremote.h>
IRrecv irrecv(9);
decode_results results;
int n=0,t=0,d=0,c=0;
void setup() {
Serial.begin (9600);
Serial.println("Setup");
irrecv.enableIRIn(); // Starts the receiver
}
void loop(){
if (irrecv.decode(&results))
{for(int i=1;i<3;i++)
{ if (irrecv.decode(&results)) // have we received an IR signal?
{long int decCode = results.value;
switch(decCode){
case 719194275: t=1; break;
case 2137078331: t=2; break;
case 440841023: t=3; break;
case 2671644771: t=4; break;
case 800219743: t=5; break;
case 1290492415: t=6; break;
case 3889222403: t=7; break;
case 3380893819: t=8; break;
case 590459007: t=9; break;
case 3509629087: t=0; break;
}
if(i==1)
c=t;
if(i==2)
d=t;
Serial.println(c, DEC);
Serial.println(d, DEC);
irrecv.resume();
}
}
n=d+c*10;
Serial.println(n, DEC);
irrecv.resume();
}
}
Without any comments, code formatting or description of your application, it is very hard to 'guess' what you are trying to achieve.
However, you seem to have 2 nested
if (irrecv.decode(&results))
statements, and the second one may never work without issueing an
irrecv.resume();
before it.
...not 100% sure, without more info.
my intention is to print n value as two digit number, i.e., as 25 when inputs are 2,5. i've assigned 2 to c and 5 to d. and you can see
{ if (irrecv.decode(&results)) // have we received an IR signal?
{long int decCode = results.value;
switch(decCode){
case 719194275: t=1; break;
case 2137078331: t=2; break;
case 440841023: t=3; break;
case 2671644771: t=4; break;
case 800219743: t=5; break;
case 1290492415: t=6; break;
case 3889222403: t=7; break;
case 3380893819: t=8; break;
case 590459007: t=9; break;
case 3509629087: t=0; break;
}
if(i==1)
c=t;
if(i==2)
d=t;
Serial.println(c, DEC);
Serial.println(d, DEC);
irrecv.resume();
}
at the end of if there is irrecv.resume(); but still it is not working.
Try inserting Serial.println("replace with some useful info here") at various points to verify the code is behaving the way you expect.
That 'debugging' approach will help you track down the isuues.
i did it before,
for(int i=1;i<3;i++)
{Serial.println(i, DEC);
if (irrecv.decode(&results)) // have we received an IR signal?
{long int decCode = results.value;
switch(decCode){
i've tried to print the value of i and 1,2 are the otputs but the values of c,d are printed only once. that means for loop is executing twice but if inside it is not executed for the second time
By adding more print statements you will see the path been taken thru your code, which should inform you about what is wrong with the code.
Alternatively, just start again from a blank sketch and get it working for 1 digit. When that is OK try to add in support for the second digit.
You should try to rewrite the code with only one 'decode' and one 'resume' statement.
one digit is completely successful, problem with two irrecv.resume();