Watch 2 Star 2 Fork 0

RCPI / pi4bPythonMulanPSL-2.0

Join us
Explore and code with more than 5 million developers,Free private repositories !:)
Sign up
这里是针对树莓派4B的远程控制类python程序代码 spread retract

https://gitee.com/rcpi/pi4b

Clone or download
23_ultrasonic_ranging.py 821 Bytes
Copy Edit Web IDE Raw Blame History
RCPI authored 2020-03-28 03:24 . 超声波传感器测距
#!/usr/bin/python
# -*- coding:utf-8 -*-
import RPi.GPIO as GPIO
import time
TRIG = 11
ECHO = 12
def setup():
GPIO.setmode(GPIO.BOARD)
GPIO.setup(TRIG, GPIO.OUT)
GPIO.setup(ECHO, GPIO.IN)
def distance():
GPIO.output(TRIG, 0)
time.sleep(0.000002)
GPIO.output(TRIG, 1)
time.sleep(0.00001)
GPIO.output(TRIG, 0)
while GPIO.input(ECHO) == 0:
a = 0
time1 = time.time()
while GPIO.input(ECHO) == 1:
a = 1
time2 = time.time()
during = time2 - time1
return during * 340 / 2 * 100
def loop():
while True:
dis = distance()
print dis, 'cm'
print ''
time.sleep(0.3)
def destroy():
GPIO.cleanup()
if __name__ == "__main__":
setup()
try:
loop()
except KeyboardInterrupt:
destroy()

Comment ( 0 )

Sign in for post a comment

Python
1
https://gitee.com/rcpi/pi4b.git
git@gitee.com:rcpi/pi4b.git
rcpi
pi4b
pi4b
master

Help Search

205735 778617b6 1899542 205747 347fc4a1 1899542