Just to be on the same page, the sketch is not measuring every pulse.
Please refer to the picture above, which is the one you posted, with names on the transitions and pulses.
Suppose the loop() first starts executing during 0LOW
This is what hapens:
- pulseIn(sensorPin,HIGH) is called and it waits for next low-to-high transition to occur. 1LH is detected. It then starts counting microseconds elapsed.
- pulseIn waits for next high-to-low transition (1HL). We are now in 1LOW
- pulseHi receives the duration of 1HIGH
- pulseIn(sensorPin,LOW) is called and it now waits for next high-to-low transition. 1HL is water under the bridge, and pulseIn patiently waits for 2HL to happen and then it starts counting microseconds during 2LOW.
- pulseIn waits for next low-to-high transition to occur (3LH)
- pulseLo receives the duration of 2LOW. 1LOW and 2HIGH are not measured.
- Output (is sent to the Serial engine during 3HIGH.
- loop() restarts. Per the figure, 3HIGH lasts more than 6 milliseconds, so we should still be in 3HIGH.
- pulseHi receives the duration of 4HIGH
- pulseLo receives the duration of 5LOW
Etcetera
So in the monitor you see the durations of
1HIGH__2LOW
4HIGH__5LOW
and so forth.
To test this, I loaded your sketch in an Arduino Mega ("A") and the following sketch in a second Arduino Mega ("B"):
// pulse generator to test pulseIn()
// port 142401
//https://forum.arduino.cc/t/uno-pulsein-problem/965774
const int N = 15;
const int outputPin = 3;
const unsigned long tLow = 67; //milliseconds
void setup() {
pinMode(outputPin, OUTPUT);
}
void loop() {
for (int i = 0; i < N; i++) {
digitalWrite(outputPin, HIGH);
delayMicroseconds(1000*(i+1));
digitalWrite(outputPin, LOW);
delay(tLow);
}
}
I had to use varying durations for the HIGH pulse because otherwise the output would not give any clues. In this case, the high pulse durations are a repeating sequence of 1000, 2000, thru, 15000 microseconds.
This is the output I get:
The first few lines occur during the reset and setup of Arduino "B".
You can see that it measures one HIGH pulse and it skips the next two. In this run it gets pulses "numbered" 1,4,7,10,13,1,4,7,10,... and misses 2,3,5,6,8,9,11,12,14,15,...
The same happens with the LOW pulses, but the output does not reveal this because all LOW pulses have the same duration (approx).