import machine
import time
from utime import sleep
# Pin interrupt handler
def interrupt_callback(pin):
pass
def setup():
# Configure LEDs as outputs
leds = [machine.Pin(i, machine.Pin.OUT) for i in range(7, 16)]
# Configure buttons as inputs with pull-down resistors
buttons = [machine.Pin(i, machine.Pin.IN, machine.Pin.PULL_DOWN) for i in range(16, 19)]
return leds, buttons
def read_binary(mux_pins):
# Poll the mux inputs and convert the 4-digit binary number to decimal
binary_value = sum([pin.value() << idx for idx, pin in enumerate(mux_pins)])
return binary_value
def main():
sleep(0.01)
print('Program starting')
leds, buttons = setup()
mux_pins = [machine.Pin(i, machine.Pin.IN) for i in range(26, 29)] # GPIO26, GPIO27, GPIO28 for mux
passcode = [0, 2, 1]
entered_code = []
while True:
for idx, button in enumerate(buttons):
if button.value() == 1: # Button pressed
print(f'Button {idx} pressed')
entered_code.append(idx)
sleep(0.2) # Debounce for 200 ms
if len(entered_code) == 3:
if entered_code == passcode:
binary_value = read_binary(mux_pins)
if 0 <= binary_value <= 8:
leds[binary_value].toggle()
print(f'Toggling LED {binary_value}')
else:
print('Binary value out of range (0-8)')
else:
print('Incorrect passcode')
entered_code.clear() # Clear the entered code
if __name__ == "__main__":
main()