from machine import Pin, PWM
import time
# Configuración del pin para el servo motor
servo_pin = Pin(15, Pin.OUT)
servo_pwm = PWM(servo_pin, freq=50)
# Función para mover el servo a un ángulo específico
def move_servo(angle):
# El rango de duty cycle varía según el servo, aquí se usa un ejemplo común
min_duty = 40 # ángulo 0°
max_duty = 115 # ángulo 180°
# Convertir el ángulo a duty cycle
duty = int(min_duty + (max_duty - min_duty) * (angle / 180))
# Configurar el duty cycle
servo_pwm.duty(duty)
while True:
try:
# Leer el ángulo desde la terminal
angle = input("Ingresa el ángulo (0 a 180 grados): ")
angle = int(angle)
# Validar el rango del ángulo
if 0 <= angle <= 180:
move_servo(angle)
print(f"Moviendo el servo a {angle} grados")
else:
print("Por favor ingresa un valor entre 0 y 180")
except ValueError:
print("Por favor ingresa un número válido")
except KeyboardInterrupt:
print("Programa finalizado")
break