I want to build a simple IR controlled power plug which should be able to learn a single code from some remote and than toggle a relay if it sees this code.
For this I use the IRremote library.
Everything works so far but sometimes the receiver sees the latest code again after a few seconds.
This toggles my relay in error.
I have already added a HOLDOFF delay of 2000ms after I see the matching IR code but still sometimes the IR receiver sees the matching code again even after these 2s.
The transmitter isn't the problem as I covered the sending IR LED after sending a signal but the problem still occurred.
Below is the IR check routines, the whole sketch is attached.
I use SERIALOUT as a debugging flag. The learned IR code is stored in irMATCHcode.
irCheckInterval is 500ms.
void checkIRcmd()
{ if( SERIALOUT > 1 ) { Serial.println( "checkIRcmd" ) ; }
if( (millis() - lastIRcheck) < irCheckInterval )
return ; // don't check too frequently
lastIRcheck = millis() ;
long irCode = readIRinput() ; // read IR signal
if( irCode < 0 )
return ; // do nothing, if no IR received
if( irCode == irMATCHcode )
{ toggleOutput() ; // toggle output if match code received
delay( IRHOLDOFF ) ; // HOLDOFF to prevent multi toggle
if( SERIALOUT ) Serial.println( "ir HOLDOFF ended" ) ;
}
}
long readIRinput()
{ if( SERIALOUT > 1 ) Serial.println( "readIRinput" ) ;
long irCode = -1 ;
if( irrecv.decode( &irResults ) )
{ if( irResults.decode_type == UNKNOWN )
{ if( SERIALOUT ) { Serial.println( "unknown IR code" ) ; }
irrecv.resume() ; // receive next value
return( irCode ) ;
}
irCode = irResults.value ;
if( irResults.decode_type == RC5 )
{ irCode &= ~0x800 ; // mask RC5 toggle bit
if( SERIALOUT ) { Serial.print( " RC5 ") ; }
}
if( irResults.decode_type == RC6 )
{ irCode &= ~0x8000 ; // mask RC6 toggle bit
if( SERIALOUT ) { Serial.print( " RC6 ") ; }
}
if( SERIALOUT ) { Serial.print( " irCode=" ) ; Serial.println( irCode, HEX ) ; }
}
irrecv.resume() ; // receive next value
return( irCode ) ;
}
x.ino (8.6 KB)