When you do a Serial.print you place characters into the serial buffer. If the buffer is full the Serial.print blocks (waits) until there is room in the buffer.
The buffer is emptied by an interrupt.
It takes a considerable amount of time to transmit one character out of the serial buffer (9600 symbols per second, 10 symbols per character) - that's 960 characters per second, or 1/960 or about 1ms per character.
Your doing a println, which adds 2 extra characters.
But that's not all.
First you're waiting for a pulse of at least 200µs. Fair enough. You're then adding to a counter. Ok. Then you're printing the results of another call to pulseIn which waits for a second pulse of indeterminate length to arrive and then prints the length of that pulse.
So, of your 10 pulses you have:
Read this pulse and check if it's > 200µS - add one to counter
Read this pulse and display the value
Read this pulse and check if it's > 200µS - add one to counter
Read this pulse and display the value
Read this pulse and check if it's > 200µS - add one to counter
Read this pulse and display the value
Read this pulse and check if it's > 200µS - add one to counter
Read this pulse and display the value
Read this pulse and check if it's > 200µS - add one to counter
Read this pulse and display the value
So you see, you are receiving all 10 pulses, but you're only counting 5, and you're displaying the pulse length of the pulses you're 'missing'.
thanks for that!
I hadn't considered the serial to be the issue.. (silly me) eventually all serial output will be removed - it's just here for now for testing/verifying.
I have reworked my code, it seems to be pretty consistent now...
If there is any simple improvements to this, please let me know as sometimes valid packets aren't received (distance from led to receiver = approx. 20CM currently)
The timeout option of pulseIn() seems to be a little weird...
If I set it below the expected length of the pulse, it ignores them - is this a change from the documentation in the reference?