How this smal infrared work I have a code that sends 384 pulses at 38kHz with an infrared LED. It's very easy to understand it. At the beggining the code sends the pulses using the LED and in the middle of the code I check the sensor if it has any signal coming. So far so good, yes? Please, take a look:

for(int i=0;i&lt;384;i++) {
  digitalWrite(IRledPin,HIGH);
  delayMicroseconds(13);
  digitalWrite(IRledPin,LOW);
  delayMicroseconds(13);
}

if (digitalRead(IRsensorPin) == LOW){
  digitalWrite(D13ledPin,HIGH);
} else {
  digitalWrite(D13ledPin,LOW);
}


My question is this: I first send the pulses, so when I reach the line if (digitalRead(IRsensorPin) == LOW){ the pulse should already have passed the sensor. So why does it work? It should not work cause I first send the pulse and then I check for the pulse but as I understand the 384 pulses should already have passed the sensor.

I have no idea why this code works. I would be glad to listen smarter people to help me understand this.

Thank you.
The IR receiver does some filtering on the signal. It doesn't signal a LOW immediately if it lost a single pulse of light. A speck of dust, hanging in the air, might just be blocking the IR phototransitor. Or some brief flash of light, making two pulses seem like one or more.

These IR receivers a specifically build to filter out the 'noise' coming from other light sources and light obstructions.

Identically, you would want this receiver to give you a HIGH if it detect only 2 pulses of light 13uS apart. I could just be some sunlight reflected from a puddle, accidentally matching 38kHz.