forked from Baymax94/children-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest02.py
More file actions
51 lines (41 loc) · 1.15 KB
/
test02.py
File metadata and controls
51 lines (41 loc) · 1.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from pymata_aio.pymata3 import PyMata3
from pymata_aio.constants import Constants
Trig = 7
Echo = 8
# board = PyMata3(com_port="/dev/ttyACM0")
board = PyMata3(com_port="COM6")
'''
def checkdistance():
board.digital_write(Trig,0)
board.sleep(0.002)
board.digital_write(Trig,1)
board.sleep(0.01)
board.digital_write(Trig,0)
distance=board.int_read(0)/58.00
board.sleep(0.01)
return distance
'''
def setup():
board.set_pin_mode(Trig, Constants.OUTPUT) # 声明引脚为输出
# board.set_pin_mode(Echo, Constants.INPUT) # 声明引脚为输入
board.set_pin_mode(Echo, Constants.ANALOG)
def loop():
try:
board.digital_write(Trig, 0)
board.sleep(0.002)
board.digital_write(Trig, 1)
board.sleep(0.01)
board.digital_write(Trig, 0)
# distance = board.int_read(Echo)/58.00 # 对应arduino的pulseIn(),缺失
distance = board.analog_read(Echo)/58.00
board.sleep(0.01)
print(distance)
except:
board.shutdown()
if __name__ == "__main__":
setup()
while True:
loop()
# 超声波