from machine import PWM,ADC,Pin,I2C
import utime
from time import sleep
from pico_i2c_lcd import I2cLcd
i2c = I2C(0, sda=Pin(0), scl=Pin(1), freq=400000)

I2C_ADDR = i2c.scan()[0]
lcd = I2cLcd(i2c, I2C_ADDR, 2, 16)

servo=PWM(Pin(15))
servo.freq(50)
adc = ADC(Pin(28))

Btns=Pin(22,Pin.IN,Pin.PULL_UP);
Bt1=Pin(6,Pin.IN,Pin.PULL_UP);
Bt2=Pin(10,Pin.IN,Pin.PULL_UP);
Bt3=Pin(14,Pin.IN,Pin.PULL_UP);
pot=0; #valor en grados del servo
lcd.clear()
while True:
    if Btns.value()==0:#lectura del potenciometro
        pot= int(adc.read_u16()*180/65535)
        utime.sleep_ms(100)
        lcd.clear()
    if Bt1.value()==0: # 0 grados
        pot=0
        utime.sleep_ms(100)
        lcd.clear()
    if Bt2.value()==0: # 90 grados
        pot=90
        utime.sleep_ms(100)
    if Bt3.value()==0: # 180 grados
        pot=180
        utime.sleep_ms(100)
        lcd.clear()
    
    
    print(pot)
    lcd.move_to(0,0)
    lcd.putstr("Servomotor A:")
    lcd.move_to(0,1)
    lcd.putstr(str(pot)+"   Grados")

    ton=(pot+42)*100000/9 #conversion a ms de tiempo servo
    servo.duty_ns(int(ton)) #activacion servo
    utime.sleep_ms(10) #delay


BOOTSELLED1239USBRaspberryPiPico©2020RP2-8020/21P64M15.00TTT