0
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

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

Ok so guys i found the ans (little a bit) When we set

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

....

It works :)

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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