from machine import Pin, Timer
from stepper import Stepper

motor = Stepper([9,8,7,6])

greenL = Pin(15, Pin.OUT)
redL = Pin(14, Pin.OUT)

toggleB = Pin(19, Pin.IN, Pin.PULL_DOWN)
closedB = Pin(20, Pin.IN, Pin.PULL_DOWN)
openedB = Pin(21, Pin.IN, Pin.PULL_DOWN)

isOpen = False
isChanging = False

if (openedB.value()):
  isOpen = True

def onToggle(pin):
  global isChanging
  if (isChanging == False):
    motor.start(not isOpen)
    isChanging = True

def onOpen(pin):
  global isOpen
  global isChanging
  if (isOpen == False):
    motor.stop()
    isChanging = False
    isOpen = True

def onClose(pin):
  global isOpen
  global isChanging
  if (isOpen):
    motor.stop()
    isChanging = False
    isOpen = False

toggleB.irq(trigger=Pin.IRQ_RISING, handler=onToggle)
openedB.irq(trigger=Pin.IRQ_RISING, handler=onOpen)
closedB.irq(trigger=Pin.IRQ_RISING, handler=onClose)

def leds(timer):
  if (isChanging):
    if (isOpen):
      redL.toggle()
      greenL.off()
    else:
      greenL.toggle()
      redL.off()
  else:
    if (isOpen):
      greenL.on()
      redL.off()
    else:
      redL.on()
      greenL.off()

ledT = Timer()
ledT.init(freq=2.5, mode=Timer.PERIODIC, callback=leds)
$abcdeabcde151015202530354045505560fghijfghij
BOOTSELLED1239USBRaspberryPiPico©2020RP2-8020/21P64M15.00TTT