Some improvements. The code now uses exit code 1 when timeout occurs. Usually shell scripts assume exit code 0 for OK, else are error.
The code does some averaging on the values measured.
Not tested.
The code does some averaging on the values measured.
Not tested.
Code:
import RPi.GPIO as GPIOimport sysimport timeGPIO.setmode(GPIO.BCM)GPIO.setwarnings(False) resistorPin = 18TIMEOUT = 3.0sum = 0cnt = 0REPEAT = 4for sequence in range(REPEAT+1): GPIO.setup(resistorPin, GPIO.OUT) GPIO.output(resistorPin, GPIO.LOW) #time.sleep(0.1) time.sleep(1.5) # discharge time GPIO.setup(resistorPin, GPIO.IN) currentTime = time.time() diff = 0 while(GPIO.input(resistorPin) == GPIO.LOW): diff = time.time() - currentTime if diff > TIMEOUT: print("9999") GPIO.cleanup() sys.exit(1) #ignore first measurement if sequence > 0: sum += diff cnt += 1# print average time in millis, no decimalsprint( int(sum / cnt * 1000))GPIO.cleanup()sys.exit(0)
Statistics: Posted by ghp — Tue Dec 03, 2024 11:36 am