-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest.py
52 lines (41 loc) · 1.31 KB
/
test.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
import RPi.GPIO as GPIO
import time
servo_pin = 17
GPIO.setmode(GPIO.BCM)
GPIO.setup(servo_pin, GPIO.OUT)
servo_pwm = GPIO.PWM(servo_pin, 50) # 50 Hz PWM frequency
servo_pwm.start(0) # Starting PWM signal with duty cycle of 0 (corresponds to 0 degrees)
#Go up
print("Going up")
for angle in range(90, -1, -1):
duty_cycle = angle / 18.0 + 2.5
servo_pwm.ChangeDutyCycle(duty_cycle)
time.sleep(5)
print("Going down")
for angle in range(0, 90):
duty_cycle = angle / 18.0 + 2.5
servo_pwm.ChangeDutyCycle(duty_cycle)
time.sleep(0.05) # Wait for 50ms before changing angle again
"""
try:
while True:
# Setting servo angle between 0 and 90 degrees
print("Going down?")
for angle in range(0, 90):
duty_cycle = angle / 18.0 + 2.5
servo_pwm.ChangeDutyCycle(duty_cycle)
time.sleep(0.05) # Wait for 50ms before changing angle again
time.sleep(2)
print("Down?")
servo_pwm.ChangeDutyCycle(0)
time.sleep(2)
# Setting servo angle between 90 and 0 degrees
print("Going up?")
for angle in range(90, -1, -1):
duty_cycle = angle / 18.0 + 2.5
servo_pwm.ChangeDutyCycle(duty_cycle)
time.sleep(2)
except KeyboardInterrupt:
servo_pwm.stop()
GPIO.cleanup()
"""