I wrote that from memory and made one mistake. The first capture in the loop is the space following the start pulse. The bits of the code are in pulses[1:32].

The second loop should be changed to:
Code:
        For i = 0 To 31
          If (pulses[i+1] < 40) Or (pulses[i+1] > 200) Then 
            Goto loop
          EndIf
          b = i >> 3                      	'0,1,2,3
          If pulses[i+1] > 99 Then  
            IR[b] = IR[b] | BitMask       	'set bit 
          EndIf 
          BitMask = BitMask << 1        	'$01,$02,$04,$08,$10,$20,$40,$80
          If BitMask = 0 Then BitMask = $01 
        Next
I've used that basic algorithm for several years to capture X-10 RF codes which also use the NEC protocol. You can find a description of the protocol at http://www.cpcares.com/pdf/UPD6121G-001.PDF.

If you're having a problem with consistency, it is most likely hardware related.

With IR, you should also define a maximum value for PulsIn. That's not always needed with RF where there are constant noise pulses in the absence of a signal.