So I am doing a little project with a Raspberry Pi that involves moving a servo motor. In the following code in Python 3, I begin by starting the servo at approximately 45 degrees. Later in the code, a different angle is determined based on the previous angle, and the the Duty Cycle is changed.
def main():
#Import functions
import measure, move
import time
import RPi.GPIO as GPIO
#Declare Variables
Servo_pin = 35
angle = 45
freq = 50
#Setup board
GPIO.setmode(GPIO.BOARD)
GPIO.setup(Servo_pin, GPIO.OUT)
servo = GPIO.PWM(Servo_pin,freq)
#Determine Duty Cycle
dc = 1/18 * (angle) + 2
print("Starting Duty Cycle: ",dc)
#Start servo
servo.start(dc)
i = 1
#Determine angle based on previous angle
while True:
if (i == 0):
angle = 45
elif (i == 1):
angle = 90
elif (i == 2):
angle = 180
elif (i > 2):
angle = 45
i = 0
i = i+1
#Change servo's position
#Convert angle to Duty Cycle
dc = 1/18 * (angle) + 2
print("Setting Duty Cycle: ",dc)
#Change position
servo.ChangeDutyCycle(dc)
#Give servo time to finish moving
time.sleep(0.3)
main()
I have the servo connected to a battery pack (4 AA batteries), yet the servo won't move with this code. Now, I'll admit that I'm a beginner, and it's probably something really easy and I apologize in advance if it that is the case.
Any help is appreciated!
Starting Duty Cycle:
? – chickity china chinese chicken