1
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BOARD)
trig=7
echo=11
t=time.time()
GPIO.setup(7,GPIO.OUT)
GPIO.setup(11,GPIO.IN)
While (t+3)>time.time():
    start=0
    end=0
    GPIO.output(trig,True)


    time.sleep(0.000001)

    GPIO.output(trig,False)

    while GPIO.input(echo)==False:
        start=time.time()

    while GPIO.input(echo)==True:
        end=time.time()

    sig_time = end-start

    dist = sig_time * 17000


    print("dist: ",dist)

    time.sleep(0.5)

GPIO.cleanup()

Ultrasonic sensor. It works when while t+3 code is not included.but when it is it dosnt why. it just run but no output. After if i run it again it give runtime warning gpio already in use why? Plz give me suitable code to run it for 3s giving all distances

4
  • This appears to be a general Python question, the Pi is irrelevant.
    – joan
    Mar 21, 2020 at 13:26
  • While is not valid Python Mar 21, 2020 at 13:36
  • @Thomas Weller plz elaborate Mar 21, 2020 at 16:00
  • While with a captial W is not valid Python. It must be while. Mar 21, 2020 at 16:47

1 Answer 1

1

I found the answer (a little bit) When we set

while (t+3) >float(time.time()):

It works

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.