-
Notifications
You must be signed in to change notification settings - Fork 0
/
Motionsensor.py
54 lines (47 loc) · 1.08 KB
/
Motionsensor.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
54
from machine import Pin, PWM
import utime
buzz = PWM(Pin(15))
buzz.freq(50)
red = Pin(13, Pin.OUT)
trigger = Pin(3, Pin.OUT)
echo = Pin(2, Pin.IN)
led = Pin(25, Pin.OUT)
global t, diff, distance
distance = 0
t = 0
diff = 0
def ultra():
global t, diff, distance
trigger.low()
utime.sleep_us(2)
trigger.high()
utime.sleep_us(5)
trigger.low()
while echo.value() == 0:
signaloff = utime.ticks_us()
while echo.value() == 1:
signalon = utime.ticks_us()
timepassed = signalon - signaloff
distance = (timepassed * 0.0343) / 2
diff = (t - distance) ** 2
t = distance
print('difference is: ',diff)
if distance<200:
if diff<30:
led.value(1)
buzz.duty_u16(0)
red.value(0)
else:
led.value(0)
buzz.duty_u16(5000)
red.value(1)
print('DANGER')
utime.sleep(0.5)
else:
led.value(1)
buzz.duty_u16(0)
red.value(0)
print("The distance from object is ",distance,"cm")
while True:
ultra()
utime.sleep(1)