from machine import Pin, PWM
from time import sleep
# Set up the servo and button pins
servo_pin = Pin(1)
button_pin = Pin(4, Pin.IN, Pin.PULL_UP)
# Initialize PWM for the servo
pwm = PWM(servo_pin)
pwm.freq(50)
# Initialize servo position (0 degrees)
pwm.duty_u16(1000)
# Track button state
button_pressed = False
while True:
# Check if the button is pressed
if not button_pin.value():
# Toggle servo position
if button_pressed:
# Move to 0 degrees
pwm.duty_u16(1500)
else:
# Move to 90 degrees
pwm.duty_u16(8000)
# Update button state
button_pressed = not button_pressed
# Debounce delay
sleep(0.2)