from machine import Pin
import time
PASSCODE = "021"
buttons = [Pin(16, Pin.IN, Pin.PULL_DOWN),
Pin(17, Pin.IN, Pin.PULL_DOWN),
Pin(18, Pin.IN, Pin.PULL_DOWN)]
switches = [Pin(28, Pin.IN, Pin.PULL_UP),
Pin(26, Pin.IN, Pin.PULL_UP),
Pin(27, Pin.IN, Pin.PULL_UP),
Pin(22, Pin.IN, Pin.PULL_UP)]
leds = [Pin(7, Pin.OUT), Pin(8, Pin.OUT), Pin(9, Pin.OUT), Pin(10, Pin.OUT),
Pin(11, Pin.OUT), Pin(12, Pin.OUT), Pin(13, Pin.OUT), Pin(14, Pin.OUT),
Pin(15, Pin.OUT)]
pressed_buttons = []
last_click= [0, 0, 0]
interrupt = 200
def get_decimal():
decimal_value = 0
for i, switch in enumerate(switches):
if switch.value() == 0:
decimal_value += 1 << i
return decimal_value
def button_pressed(pin):
button_index = buttons.index(pin)
current = time.ticks_ms()
if time.ticks_diff(current, last_click[button_index]) >= interrupt:
last_click[button_index] = current
pressed_buttons.append(str(button_index))
print("Button", button_index, "pressed.")
if len(pressed_buttons) == 3:
if ''.join(pressed_buttons) == PASSCODE:
decimal_value = get_decimal()
print("Passcode correct! Switch value:", decimal_value)
if 0 <= decimal_value <= 8:
leds[decimal_value].toggle()
print("Toggled LED at position:", decimal_value)
else:
print("Passcode correct, but", decimal_value, "is out of range [0-8].")
else:
print("Incorrect passcode.")
pressed_buttons.clear()
for button in buttons:
button.irq(trigger=Pin.IRQ_RISING, handler=button_pressed)
previous_decimal = get_decimal()
print("Initial switch value:", previous_decimal)
while True:
current_decimal = get_decimal()
if current_decimal != previous_decimal:
print("Switch changed, new decimal value:", current_decimal)
previous_decimal = current_decimal
time.sleep(0.1)