-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrelayServo.py
52 lines (39 loc) · 1.18 KB
/
relayServo.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
from gpiozero import Servo
from RPi import GPIO
from time import sleep
# Setup for servo motor
servo_pin = 13 # Change to your GPIO pin
servo = Servo(servo_pin)
# Setup for relay
relay_pin = 13 # Change to your GPIO pin
GPIO.setmode(GPIO.BCM)
GPIO.setup(relay_pin, GPIO.OUT)
# Function to move servo
def move_servo(angle):
if angle == 45:
servo.mid() # This assumes that mid position corresponds to 45 degrees
elif angle == 0:
servo.min() # This assumes that min position corresponds to 0 degrees
try:
# Turn on the relay
GPIO.output(relay_pin, GPIO.HIGH)
print("Relay is ON")
# Wait for 35 seconds
sleep(2)
# Move servo from 0 to 45 degrees
move_servo(0)
print("Servo moved to 70 degrees")
# Wait for 5 seconds
sleep(2)
# Move servo back to 0 degrees
print("Servo moved back to 0 degrees")
# Turn off the relay
GPIO.output(relay_pin, GPIO.LOW)
print("Relay is OFF")
# Wait for 30 seconds before ending the script or doing anything else
sleep(2)
except KeyboardInterrupt:
# Clean up GPIO on CTRL+C exit
GPIO.cleanup()
# Clean up GPIO on normal exit
GPIO.cleanup()