라즈베리파이 Ultra 예제

Posted by PeEn
2019. 9. 5. 11:27 Programing/Raspberry Pi)
import RPi.GPIO as gpio
import time

TRIGER = 24
ECHO = 23

gpio.setmode(gpio.BCM)
gpio.setup(TRIGER, gpio.OUT)
gpio.setup(ECHO, gpio.IN)

startTime = time.time()

try:
    while True:
        gpio.output(TRIGER, gpio.LOW)
        time.sleep(0.1)
        gpio.output(TRIGER, gpio.HIGH)
        time.sleep(0.00002)
        gpio.output(TRIGER, gpio.LOW)

        while gpio.input(ECHO) == gpio.LOW:
            startTime = time.time()

        while gpio.input(ECHO) == gpio.HIGH:
            startTime = time.time()

        perio = endTime - startTime
        dist1 = round(period * 1000000 / 58, 2)
        dist2 = round(period * 17241, 2)

        print ('Dist1', dist1, 'cm', ', Dist2', dist2, 'cn')
except:
    gpio.cleanup()