Hi Terry,
> > The Sensor output is low when there is no train in front of it and
> > rises when there is.
By polling for a high input, there's the chance it might be missed
depending on timings. Have you checked:
- How long the train takes to pass the sensor, i.e. how long the sensor
remains high.
- How often your while loop runs given it's part of a larger body of
code which may be busy, or even tied up, elsewhere.
- The value of sensor_pause_time, and that it is in seconds as
time.sleep() wants.
Also double-check more simple things:
- GPIO configured as input.
- Any Pi-configured pull-up/pull-down is correct.
I'd debug with print(). So taking your last version, make it:
print('sensor b: test start')
for i in range(sensor_test_loops_out):
if GPIO.input(sensor_b):
print('sensor b: train', i)
break
print('z ', end='', flush=True)
time.sleep(sensor_pause_time)
else:
print('sensor b: test end, no train')
- The old code went around the while-loop sensor_test_loops_out+1 times.
range() makes that easy to avoid.
- The first code was correct: stop waiting for the train when
GPIO.input() returns true meaning the GPIO is high.
- Observe the speed of polling by the stream of ‘z z z...’ showing the
time.sleep()s.
Can you slow the train so the sensor should be high for longer on each
passing?
Polling is often not the ideal way to do it. There are better ways,
like event_detected(). But don't switch to those without getting the
current method working; that would just be adding another layer of murk
to obscure. Fully understand the current behaviour first.
https://sourceforge.net/p/raspberry-gpio-python/wiki/Inputs/
--
Cheers, Ralph.
--
Next meeting: Online, Jitsi, Tuesday, 2026-05-05 20:00
Check to whom you are replying
Meetings, mailing list, IRC, ... https://dorset.lug.org.uk
New thread, don't hijack: mailto:[email protected]