-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodule.py
59 lines (46 loc) · 1.24 KB
/
module.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
55
56
57
58
59
#!/usr/bin/python
import RPi.GPIO as GPIO
import time
def servo(angle):
GPIO.setmode(GPIO.BOARD)
GPIO.setup(11,GPIO.OUT)
servo1 = GPIO.PWM(11,50)
servo1.start(0)
duty = angle/18 + 2
GPIO.output(11,True)
servo1.ChangeDutyCycle(duty)
time.sleep(1)
GPIO.output(11,False)
servo1.ChangeDutyCycle(0)
servo1.stop()
GPIO.cleanup()
def distance(a):
GPIO.setmode(GPIO.BOARD)
if a == 1:
PIN_TRIGGER = 13
PIN_ECHO = 15
elif a == 2:
PIN_TRIGGER = 16
PIN_ECHO = 18
elif a == 3:
PIN_TRIGGER = 29
PIN_ECHO = 31
elif a == 4:
PIN_TRIGGER = 33
PIN_ECHO = 35
GPIO.setup(PIN_TRIGGER, GPIO.OUT)
GPIO.setup(PIN_ECHO, GPIO.IN)
GPIO.output(PIN_TRIGGER, GPIO.LOW)
#print("Waiting for sensor to settle")
time.sleep(2)
#print("Calculating distance")
GPIO.output(PIN_TRIGGER, GPIO.HIGH)
time.sleep(0.00001)
GPIO.output(PIN_TRIGGER, GPIO.LOW)
while GPIO.input(PIN_ECHO)==0:
pulse_start_time = time.time()
while GPIO.input(PIN_ECHO)==1:
pulse_end_time = time.time()
pulse_duration = pulse_end_time - pulse_start_time
dist = round(pulse_duration * 17150, 2)
return dist