import machine
from time import sleep
# Define pin numbers for switches connected to the multiplexer
mux_select_pins = [26, 27] # Pins for selecting input on the multiplexer
# Define pin numbers for push buttons and LEDs
button_pins = [16, 17, 18] # Pins connected to push buttons
led_pins = [7, 8, 9, 10, 11, 12, 13, 14, 15] # Pins connected to LEDs
# Define correct passcode
correct_passcode = [0, 0, 2]
# Initialize variables
pressed_buttons = []
# Function to read the state of switches via multiplexer
def read_switches():
binary_value = 0
# Set the value of select pin for mux2 (GPIO 27) to 0
machine.Pin(mux_select_pins[1], machine.Pin.OUT).value(0)
# Set the value of select pin for mux1 (GPIO 26) to 1
machine.Pin(mux_select_pins[0], machine.Pin.OUT).value(1)
sleep(0.01) # Wait for multiplexer to settle
for i, pin in enumerate(button_pins):
binary_value |= machine.Pin(pin).value() << i
return binary_value
def toggle_output(passcode):
if passcode == correct_passcode:
print("Correct passcode entered.")
else:
print("Incorrect passcode.")
# Main loop
while True:
# Read switches
switches_value = read_switches()
# Add the value of switches to pressed_buttons list
pressed_buttons.append(switches_value)
# Sleep to debounce buttons and reduce CPU load
sleep(0.1)
for pin in button_pins:
button_pin = machine.Pin(pin, machine.Pin.IN, machine.Pin.PULL_DOWN)
print("Pin", pin, "initialized as", button_pin)