Create 360 deg with 2 servos

Hi, I am trying to create 360 deg using 2 180deg servos (stacking them on each other). I would like one arm to go 180deg and pause, then the other goes 180deg and pause. Then the first goes back to zero, then pauses, then the second goes back to zero. All this happens while a song is playing. Here is my code so far, I don't know where I am going wrong but its not working.

def spin():
    color_num = 0 
    for angle in range (0, 180, 10):
        servo_1.angle = angle
        if angle == 180:
            servo_1.angle = 180
            time.sleep(0.2)
        strip.fill(colors[color_num])
        time.sleep(0.05)
        color_num = color_num+1
        if color_num == len(colors):
            color_num = 0
    for angles in range (0,180,10):
        servo_2.angle = angles
        if angles == 180:
            servo_2.angle = 180
            time.sleep(0.2)
        strip.fill(colors[color_num])
        time.sleep(0.05)
        color_num = color_num+1
        if color_num == len(colors):
            color_num = 0
    for angle in range (180, -1, 10):
        servo_1.angle = angle
        servo_2.angle = angle
        if angle == 0:
            servo_1.angle = 0
        if angle == 0: 
            servo_2.angle = 0 
        time.sleep(0.2)

def play_mp3(filename):
    decoder.file = open(path + filename, "rb")
    audio.play(decoder)
    while audio.playing:
        spin()

Welcome to the Forum! You will get faster and better help if you post all your code as requested by the forum guidelines. Read the forum guidelines to see how to properly post code and some good information on making a good post. Use the IDE autoformat tool (ctrl-t or Tools, Auto format) before posting

Please ALWAYS show ALL your code when you asking about it.

"Not working" tells us nothing. Does the build go up in flames or is absolutely nothing happening? Please post scematics. Inadequate powering is a common issue.

Servo 1 is going to 180 then snapping back to zero before servo 2 runs to 180 then snaps back.

Did you think about using a small stepper motor rather than servos? As you have discovered the latter only give a limited range which means you have to "stack" them which leads to code and mechanical complexities, including repeated bending of the wires. A single cheap stepper can easily turn 360 degrees and back and carry on doing it for years. Simpler mechanics and simpler code I think.

So servo1 makes a complete move from 0 to 180 and back to 0 degrees, and then servo2 does the same manouver?

Yeah... Something like stepper motors would be a "more standard" solution. But it requires a completely different design approach. You'd need a stepper motor driver and a home-position sensor.

Or, you could use gears or pulleys to increase the rotation. Doubling the rotation ('gearing up") would result in half the torque and half of the position-resolution, and double the speed (unless the speed is limited by torque).

Currently, yes.

I'm not familiar with that way of coding. What makes the first for loop end before the second for loop? Colon(:slight_smile: looks like a sign for start of a block. What's the end? Just indentation?

That’s Python but not a complete program. OP needs to post the whole program or go to another forum.

Sounds like good advice.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.