from machine import Pin, PWM, ADC, Timer
import time
led1= Pin(25, Pin.OUT, value=0)
led2= Pin(27, Pin.OUT, value=0)
led3= Pin(26, Pin.OUT, value=0)
valor=0
valorPot=0
def lectura(x):
global valor
global valorPot
valor=joystickY.read()
valorPot=potenciometer.read()
valorPot=((valorPot-0)/4095)*180
print(valor)
print(valorPot)
def angle_servo(angle):
maximum=128
minimum=26 #CONVERSION A BITS DEL PUNTO MINIMO Y MAXIMO
dutyC=int(minimum+(angle/180)*(maximum-minimum))
pwm0.duty(dutyC)
jY= Pin(32, Pin.IN)
joystickY=ADC(jY)
joystickY.atten(ADC.ATTN_11DB)
pot=Pin(35, Pin.IN)
potenciometer=ADC(pot)
value=0
pwm0 = PWM(Pin(14), duty=0) # create PWM object from a pin
pwm0.freq(50) # get current frequency
lecturaJY=Timer(0)
lecturaJY.init(mode=Timer.PERIODIC, callback=lectura, freq=10)
angle_act=0
while True:
if valor>3000:
angle_act=angle_act+1
if angle_act>4095: angle_act=4095
if valor<1000:
angle_act=angle_act-1
if angle_act<0: angle_act=0
angle_servo(angle_act)
time.sleep(0.1)
if (valorPot>=angle_act-10 and valorPot<=angle_act+10):
led2.on()
else:
led2.off()