from machine import Pin,PWM
import time
servo = PWM(Pin(10))
servo.freq(50) # standard servo frequency
button = Pin(14,Pin.IN,Pin.PULL_UP)
def move(angle):
# Comcert angle(0 to 180) to duty cycle (1000 to 9000)
min_duty = 1638
max_duty = 8192
duty = int(min_duty + (angle / 180) * (max_duty - min_duty))
servo.duty_u16(duty)
angles = [0,90,170]
index = 0
move(angles[index])
last_button = button.value()
while True:
current_button = button.value()
if last_button == 1 and current_button == 0:
index = (index + 1) % len(angles)
move(angles[index])
print("Moved to ", angles[index],"degrees")
time.sleep(0.3)
last_button = current_button
time.sleep(0.01)