-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
53 lines (42 loc) · 1.43 KB
/
main.py
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
52
53
import utime
import uos
from sonar import Ultrasonic
from buzzer import Buzzer
from temperature import celsius
# our Reset/s Time Clock at boot
with open('main.txt', 'w') as f:
f.write("{} at main.py said Hello World on {}".format(uos.uname().machine, utime.localtime()))
# take ur time
#utime.sleep(5)
# surpise! boot.py globals are visible in main (too)
# reconfigure the timer, hence led to flash a bit less
timer.init(freq=1, mode=Timer.PERIODIC, callback=tick)
# instantiate all (3) distance sensors
uLeft = Ultrasonic(triggerPin=3, echoPin=2)
uRight = Ultrasonic(triggerPin=7, echoPin=6)
uCenter = Ultrasonic(triggerPin=11, echoPin=10)
hLeft = Buzzer(signalPin=18)
hRight = Buzzer(signalPin=13)
while True:
# start measuring
utime.sleep(0.100)
uCenter.measure()
utime.sleep(0.100)
uLeft.measure()
utime.sleep(0.100)
uRight.measure()
utime.sleep(0.100)
# sonars are ready
ucm = min(uLeft.cm, uCenter.cm, uRight.cm)
if ucm == uLeft.cm: hLeft.beep(ucm)
if ucm == uRight.cm: hRight.beep(ucm)
if ucm == uCenter.cm:
hLeft.alert(ucm)
hRight.alert(ucm)
# a bit longer beep
utime.sleep(0.200)
hLeft.stop()
hRight.stop()
print("nearest obstacle detected at", ucm, "cm, air temperature cca", celsius(), "C")
print("measurements [in cm] from all existing sonars were", uLeft.cm, uCenter.cm, uRight.cm)
#utime.sleep(0.2)