import machine
import time
from utime import sleep

BUTTON_COUNT = 3
LED_COUNT = 9

BUTTON_START_ID = 16
LED_START_ID = 7
last_button_time_stamp = 0
key_presses = []
passcode_entered = False

def PinId(pin):
    return int(str(pin)[8:11].rstrip(","))

def interrupt_callback(pin):
    global last_button_time_stamp
    global passcode_entered
    cur_button_ts = time.ticks_ms()
    button_press_delta = cur_button_ts - last_button_time_stamp
    if button_press_delta > 200:
        last_button_time_stamp = cur_button_ts
        key_presses.append(pin)
        print(f'key press: {PinId(pin) - BUTTON_START_ID}')
        if not passcode_entered:
            start_timer()

def start_timer():
    global passcode_entered
    global last_button_time_stamp
    last_button_time_stamp = time.ticks_ms()  # Reset the button press timestamp
    passcode_entered = True

def main():
    global key_presses
    global last_button_time_stamp
    global passcode_entered
    PASSCODE_LENGTH = 0

    s0 = machine.Pin(27, machine.Pin.OUT)
    s1 = machine.Pin(28, machine.Pin.OUT)
    mux_in = machine.Pin(26, machine.Pin.IN, machine.Pin.PULL_DOWN)

    buttons = []
    for btn_idx in range(0, BUTTON_COUNT):
        buttons.append(machine.Pin(BUTTON_START_ID + btn_idx, machine.Pin.IN, machine.Pin.PULL_DOWN))
        buttons[-1].irq(trigger=machine.Pin.IRQ_FALLING, handler=interrupt_callback)

    PASS_CODE = [buttons[0], buttons[2], buttons[1]]
    PASSCODE_LENGTH = len(PASS_CODE)

    LED_pins = []
    for out_id in range(0, LED_COUNT):
       LED_pins.append(machine.Pin(LED_START_ID + out_id, machine.Pin.OUT))

    last_dev = -1
    while True:
        binary_code = 0
        for selector_val in range(4):
            s0.value(selector_val % 2)
            s1.value(selector_val // 2)
            sleep(0.02)
            binary_code += (pow(2, selector_val) * mux_in.value())

        if last_dev != binary_code:
            last_dev = binary_code
            print(f'selected output: {last_dev}')
        sleep(0.1)

        if passcode_entered:
            # Check if 3 seconds have elapsed since the first button press
            if time.ticks_diff(time.ticks_ms(), last_button_time_stamp) >= 3000:
                print("The 3 Seconds Have Been Finished , Type The Passeord Again.")
                print('')
                key_presses.clear()
                passcode_entered = False
            elif len(key_presses) >= PASSCODE_LENGTH:
                if key_presses == PASS_CODE:
                    print('correct passcode')
                    if binary_code < LED_COUNT:
                        print(f'toggling: {binary_code}')
                        LED_pins[binary_code].toggle()
                    else:
                        print(f'invalid output: {binary_code}, ' +
                              f'valid range: 0-{len(LED_pins) - 1}, doing nothing')
                else:
                    print('wrong passcode')
                print('')
                key_presses.clear()
                passcode_entered = False

if __name__ == "__main__":
    main()


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