from machine import Pin
from utime import sleep_ms, ticks_ms

# Define button pins
button_pin1 = 16
button_pin2 = 17
button_pin3 = 18

# Define LED pins
led_pins = [7, 8, 9, 10, 11, 12, 13, 14, 15]

# Define multiplexer pins
mux_pins = [27, 28, 26]

# Create LED objects
leds = [Pin(pin, Pin.OUT) for pin in led_pins]

# Create multiplexer objects
mux_pins = [Pin(pin, Pin.OUT) for pin in mux_pins]

# Function to select the channel on the multiplexer
def select_channel(channel):
    mux_pins[0].value(channel & 0x01)
    mux_pins[1].value((channel >> 1) & 0x01)

# Function to read from the multiplexer
def read_mux(channel):
    select_channel(channel)
    sleep_ms(10)  # Delay to allow mux to settle
    return mux_pins[2].value()

button_order = []
last_interrupt = 0
debounce_delay = 400  # milliseconds

# Button interrupt callback with debounce
def interrupt_callback(pin):
    global value_1, value_2, value_3, button_order
    global last_interrupt
    current_time = ticks_ms()
    if current_time - last_interrupt > debounce_delay:
        last_interrupt = current_time
        global button_order
        if pin == button_pin1:
            print("Button 0 pressed")
            value_1 = 0
            button_order.append(0)
        elif pin == button_pin2:
            print("Button 1 pressed")
            value_2 = 1
            button_order.append(1)
        elif pin == button_pin3:
            print("Button 2 pressed")
            value_3 = 2
            button_order.append(2)
        if len(button_order) == 3:
            action_for_value(decimal_value)

# Function to perform actions based on the value read from the multiplexer
def action_for_value(decimal_value):
    global button_order
    if button_order == [2, 0, 1]:
        if 0 <= decimal_value <= 8:
            print("LED {} is toggled".format(decimal_value))
            leds[decimal_value].toggle()
        else:
            print("Correct passcode entered, but no action for this value \nchoose another value")
    else:
        print("Invalid passcode")
    sleep_ms(200)
    value_1,value_2,value_3,button_order = 0,0,0,[]

def main():
    global value_1, value_2, value_3, button_order
    global button_pin1, button_pin2, button_pin3
    button_pin1 = Pin(button_pin1, Pin.IN, Pin.PULL_DOWN)
    button_pin2 = Pin(button_pin2, Pin.IN, Pin.PULL_DOWN)
    button_pin3 = Pin(button_pin3, Pin.IN, Pin.PULL_DOWN)
    button_pin1.irq(trigger=Pin.IRQ_FALLING, handler=lambda pin: interrupt_callback(button_pin1))
    button_pin2.irq(trigger=Pin.IRQ_FALLING, handler=lambda pin: interrupt_callback(button_pin2))
    button_pin3.irq(trigger=Pin.IRQ_FALLING, handler=lambda pin: interrupt_callback(button_pin3))

    global previous_decimal_value
    previous_decimal_value = None

    while True:
        global decimal_value
        decimal_value = 0
        for channel in range(4):
            value = read_mux(channel)
            decimal_value |= value << channel
        
        if decimal_value != previous_decimal_value:
            previous_decimal_value = decimal_value
            value_1,value_2,value_3,button_order = 0,0,0,[]
            if 0 <= decimal_value <= 8:
                print("LED D{} is chosen\nEnter the passcode".format(decimal_value))
            else:
                print("No action defined for value", decimal_value)
        
        # Introduce a small delay to avoid high CPU usage
        sleep_ms(100)

if __name__ == "__main__":
    main()
$abcdeabcde151015202530354045505560fghijfghij
$abcdeabcde151015202530fghijfghij
BOOTSELLED1239USBRaspberryPiPico©2020RP2-8020/21P64M15.00TTT