初始化提交
This commit is contained in:
23
boards/default_src/micropython/origin/build/lib/sonar.py
Normal file
23
boards/default_src/micropython/origin/build/lib/sonar.py
Normal file
@@ -0,0 +1,23 @@
|
||||
from machine import Pin
|
||||
import time
|
||||
|
||||
# Sonar
|
||||
class Sonar:
|
||||
def __init__(self, trig, echo):
|
||||
self.trig=Pin(trig, Pin.OUT)
|
||||
self.echo=Pin(echo, Pin.IN)
|
||||
|
||||
def checkdist(self):
|
||||
#trig, echo = Pin(trig, Pin.OUT), Pin(echo, Pin.IN)
|
||||
self.trig.value(0)
|
||||
self.echo.value(0)
|
||||
self.trig.value(1)
|
||||
time.sleep_us(10)
|
||||
self.trig.value(0)
|
||||
while(self.echo.value()==0):
|
||||
pass
|
||||
t1 = time.ticks_us()
|
||||
while(self.echo.value()==1):
|
||||
pass
|
||||
t2 = time.ticks_us()
|
||||
return round(time.ticks_diff(t2, t1) / 10000 * 340 / 2, 2)
|
||||
Reference in New Issue
Block a user