from machine import Pin, PWM
from utime import sleep_ms

servo1 = PWM(Pin(12, Pin.OUT), freq=50)
servo2 = PWM(Pin(33, Pin.OUT), freq=50)
BotonAbierto_compuerta1 = Pin(14, Pin.IN, Pin.PULL_UP)
BotonCerrado_compuerta1 = Pin(12, Pin.IN, Pin.PULL_UP) 
BotonAbierto_compuerta2 = Pin(0, Pin.IN, Pin.PULL_UP)
BotonCerrado_compuerta2 = Pin(4, Pin.IN, Pin.PULL_UP)


def Abierto_interrup(pin):
    servo1.value(not servo1.value())


def Cerrado_interrup(pin):
    servo1.value(not servo1.value())


def compuerta_abrir1():  
    servo1.off()
    servo1.on()
    while BotonAbierto_compuerta1.value():
        sleep_ms(500)


def compuerta_cerrada1():  
    servo1.off()
    servo1.on()
    while BotonCerrado_compuerta1.value():
        sleep_ms(500)


BotonAbierto_compuerta1.irq(trigger=Pin.IRQ_FALLING, handler=Abierto_interrup)
BotonCerrado_compuerta1.irq(trigger=Pin.IRQ_FALLING, handler=Cerrado_interrup)

while True:
    for i in range(1800, 8000):
        servo1.duty_u16(i)
        sleep_ms(1)