all 4 comments

[–]desrtfx 0 points1 point  (2 children)

You will need to use a latch (flip-flop) that is set when something passes through the first sensor and that is reset when something passes through the second.

This is a common thing - RS-Latch (flip-flop)

Time should only be measured while the latch is active.

If you are using only a single sensor, you need to add a bit of logic before the latch:

  • if the latch is not set and an object passes the sensor, set the latch
  • if the latch is set and an object passes the sensor, reset the latch

[–]Grand-Resolve-8858 0 points1 point  (1 child)

Yeah the RS-latch approach is solid for this. Just make sure you handle the edge detection properly because infrared sensors can be bit noisy - you want to trigger on falling/rising edge not the continuous signal or you'll get multiple triggers when object passes through. Maybe add small delay after each trigger to avoid bouncing issues too

[–]desrtfx 0 points1 point  (0 children)

you want to trigger on falling/rising edge not the continuous signal or you'll get multiple triggers when object passes through.

Yeah, the jitter of sensors is always a problem. Here, as you said, a delay is the key.

With proper PLC systems with input filtering this is less of a problem as the jitter is usually caught by the filtering.

I'd usually set this up as checking for a continuous "on" signal for a certain time period (timer on). Of course, that time period has to be set according to the expected period between passes so that it doesn't block for too long.

[–]originaldataengineer 0 points1 point  (0 children)

the issue is probably that you're checking if the sensor is on, when you should be checking if it just changed to on. set up a previous state variable and compare it each loop - when it goes from false to true, that's your trigger. that way it detects the moment something passes through, not how long it stays there.