import utime
from machine import Pin

# Define LED pins and create Pin objects
led_pins = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
leds = [Pin(pin, Pin.OUT) for pin in led_pins]

# Define button pins and create Pin objects with pull-down
bt_pin = [16, 17, 18]
bt_mode = [Pin(pin, Pin.IN, Pin.PULL_DOWN) for pin in bt_pin]

# Global variables for button states and modes
bt1_triggered = 0
bt2_triggered = 0
bt3_triggered = 0
bt1 = -1
bt1_use = False
bt2_use = False
bt3_use = False
mod2 = 0
mod3 = 0

# Variables for debounce
last_button_state = [0] * len(bt_mode)
debounce_delay = 50  # Adjust the debounce delay as needed (in milliseconds)

# Interrupt handler for button 1 with debounce
def handle_button1(pin):
    global bt1_triggered
    current_state = pin.value()

    if current_state == 1 and last_button_state[0] == 0:
        bt1_triggered = 1

    last_button_state[0] = current_state
    utime.sleep_ms(debounce_delay)

# Interrupt handler for button 2 with debounce
def handle_button2(pin):
    global bt2_triggered
    current_state = pin.value()

    if current_state == 1 and last_button_state[1] == 0:
        bt2_triggered = 1

    last_button_state[1] = current_state
    utime.sleep_ms(debounce_delay)

# Interrupt handler for button 3 with debounce
def handle_button3(pin):
    global bt3_triggered
    current_state = pin.value()

    if current_state == 1 and last_button_state[2] == 0:
        bt3_triggered = 1

    last_button_state[2] = current_state
    utime.sleep_ms(debounce_delay)

# Attach interrupt handlers to buttons
bt_mode[0].irq(trigger=Pin.IRQ_RISING, handler=handle_button1)
bt_mode[1].irq(trigger=Pin.IRQ_RISING, handler=handle_button2)
bt_mode[2].irq(trigger=Pin.IRQ_RISING, handler=handle_button3)

# Function to set LEDs based on the specified pattern
def bt_mod(led, flash_sw=True):
    for i in range(len(led_pins)):
        leds[i].value(0)
    if flash_sw == False:
        for i in led:
            leds[i - 1].value(1)
    else:
        for k in range(1, 3):
            for i in led:
                leds[i - 1].value(k % 2)
            utime.sleep(0.7)

# Function to flash LEDs based on a specific pattern
def led_flash(led):
    flash_led = [[1, 7, 10, 4], [2, 8, 11, 5], [10, 4, 2, 8], [11, 5, 1, 7]]
    bt_mod(flash_led[led], flash_sw=True)

# Function for mode 2 with specified lighting pattern
def mode2(light_num):
    global lighting
    lighting = [
        [12, 6, 1, 7],
        [10, 4, 3, 9]
    ]
    bt_mod(lighting[light_num], flash_sw=False)

# Function for mode 3 with specified version and lighting patterns
def mode3(ver):
    mode3_led = [
        [
            [1, 6, 7, 12], [1, 2, 5, 7, 8, 11], [3, 4, 9, 10], [2, 4, 5, 8, 10, 11],
        ],
        [
            [1, 6, 7, 12], [1, 7, 5, 11], [1, 4, 7, 10], [1, 4, 7, 10, 2, 8], [3, 9, 4, 10], [4, 10, 5, 11, 2, 8]
        ]
    ]

    global version
    version = [
        [5, 0.5, 5, 0.5],
        [2, 0.5, 5.5, 0.5, 2, 0.5]
    ]
    for i in range(len(version[ver])):
        # print(mode3_led[ver][i])
        bt_mod(mode3_led[ver][i], flash_sw=False)
        utime.sleep(version[ver][i])

if __name__ == "__main__":
    while True:
        
        if bt1_triggered == 1:
            bt1_triggered = 0
            if bt1_use == False:
                bt1 = -1
                bt1_use = True
                bt2_use = False
                bt3_use = False
            # print("mode1")
            bt1 += 1
            bt1 %= 4

        if bt2_triggered == 1:
            bt2_triggered = 0
            if bt2_use == False:
                bt1 = 5
                bt1_use = False
                bt3_use = False
                bt2_use = True
                mode2(0)
                continue
            # print("mode2")
            mod2 = (mod2 + 1) % 2
            mode2(mod2)

        if bt3_triggered == 1:
            bt3_triggered = 0
            if bt3_use == False:
                bt1 = 5
                bt1_use = False
                bt2_use = False
                bt3_use = True
                mode3(0)
                continue
            # print("mode3")
            mod3 = (mod3 + 1) % 2
            mode3(mod3)

        if bt1 > -1 and bt1 < 4:
            led_flash(bt1)
        elif bt2_use:
            mode2(mod2)
        elif bt3_use:
            mode3(mod3)
        else:
            pass

        utime.sleep(0.12)
Loading
pi-pico-w