Solved : IRremote sees phantom codes (repeats of last key)

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)

The original sketch may be a bit complex so I tested a stripped down version without any interrupt and pushbuttons.
The issue remains though.
Every other time I get a double toggle.

IRremoteSwitch_2.ino (4.12 KB)

Solved!
I found that remotes send so fast that shorter codes like RC5 are received multiple times even on a short key press.
Thus I needed to resume only after a HOLDOFF time not directly after the reception of a code.

artisian:
Solved!
I found that remotes send so fast that shorter codes like RC5 are received multiple times even on a short key press.
Thus I needed to resume only after a HOLDOFF time not directly after the reception of a code.

That is indeed good for you. Some future forum searcher will benefit from your discovery. Please edit your post title to show (SOLVED).

Paul