import machine
import utime

#program "intRed_pico.py" toggles the pico's main green LED
#        every 2 seconds using an interrupt routine
#        (not using an loop with a counter).
#        press button 21 to
#           turn ON led GP5 and to "interrupt"
#           the green LED's blinking for 4 sec

# external set up of GP Pins
# GP15 to red (n.o.) button to 3v3
# GP10 to 330 ohm resistor to greenLED anode from cathode to GND Pin phys 23
# GP11 to 330 ohm resistor to redLED   anode from cathode to GND Pin phys 23

# tested OK on 2021FJun15 with MPA by D@CC

progName="intRed_pico.py"
print("progName:"+progName)
bRedGP=21        #test using the Cytron Maker Pi Pico
#bRedGP=18 to test my MAK Pi ADC board
#button_red = machine.Pin(bRedGP, machine.Pin.IN, machine.Pin.PULL_DOWN)
button_red = machine.Pin(bRedGP, machine.Pin.IN, machine.Pin.PULL_DOWN) #?no PULL_DOWN for MPA ADC0 to GP18
led_red  = machine.Pin(5, machine.Pin.OUT)
led_green  = machine.Pin(25, machine.Pin.OUT)

# inititialize red and green LEDs
led_red.value(0)
led_green.value(0)
print("waiting for press of button",bRedGP)

def int_handler(pin):
    button_red.irq(handler=None)
    print("Interrupt Detected! at time:",utime.time())
    led_red.value(1)
    led_green.value(0)
    print("sleeping for 4 secs")
    utime.sleep(4)     # seconds
    led_red.value(0)
    print("waiting for press of button",bRedGP)
    button_red.irq(handler=int_handler)
#end def

button_red.irq(trigger=machine.Pin.IRQ_RISING, handler=int_handler)

while True:
    led_green.toggle()
    utime.sleep(2)  # seconds
#while end

# source: https://www.youtube.com/watch?v=Zy64kZEM_bg
#/intRed.py     stored as intRed_pico.py
