from machine import Pin, PWM
import time

# Define pins
SERVO_PIN = 16  # Change as needed
BUTTON_PIN = 17  # Change as needed

# Initialize servo (50 Hz PWM)
servo = PWM(Pin(SERVO_PIN))
servo.freq(50)

# Initialize button with pull-up resistor
button = Pin(BUTTON_PIN, Pin.IN, Pin.PULL_UP)

# Function to move servo
def set_servo_angle(angle):
    min_duty = 1500  # Adjusted for most servos (~0.5ms pulse for 0°)
    max_duty = 8000  # Adjusted (~2.5ms pulse for 180°)
    
    duty = int((angle / 180) * (max_duty - min_duty) + min_duty)
    servo.duty_u16(duty)
    time.sleep(0.5)  # Allow time for movement

# Initial position
direction = False  # False = left, True = right
set_servo_angle(90)  # Start at center position

while True:
    if button.value() == 0:  # If button is pressed
        direction = not direction  # Toggle direction

        if direction:
            set_servo_angle(180)  # Move right
            print("RIGHT")
        else:
            set_servo_angle(0)  # Move left
            print("LEFT")

        time.sleep(0.3)  # Debounce delay
        while button.value() == 0:  # Wait for button release
            pass
BOOTSELLED1239USBRaspberryPiPico©2020RP2-8020/21P64M15.00TTT
pico:GP0
pico:GP1
pico:GND.1
pico:GP2
pico:GP3
pico:GP4
pico:GP5
pico:GND.2
pico:GP6
pico:GP7
pico:GP8
pico:GP9
pico:GND.3
pico:GP10
pico:GP11
pico:GP12
pico:GP13
pico:GND.4
pico:GP14
pico:GP15
pico:GP16
pico:GP17
pico:GND.5
pico:GP18
pico:GP19
pico:GP20
pico:GP21
pico:GND.6
pico:GP22
pico:RUN
pico:GP26
pico:GP27
pico:GND.7
pico:GP28
pico:ADC_VREF
pico:3V3
pico:3V3_EN
pico:GND.8
pico:VSYS
pico:VBUS
btn1:1.l
btn1:2.l
btn1:1.r
btn1:2.r
r1:1
r1:2
servo1:GND
servo1:V+
servo1:PWM