import machine
import time
from utime import sleep
# Define the pins for buttons and LEDs
BUTTON_PINS = [machine.Pin(i, machine.Pin.IN, machine.Pin.PULL_UP) for i in range(2, 5)] # B0, B1, B2
LED_PINS = [machine.Pin(i, machine.Pin.OUT) for i in range(10, 19)] # D0-D8
SWITCH_PINS = [machine.Pin(i, machine.Pin.IN, machine.Pin.PULL_UP) for i in range(5, 9)] # SW0-SW3
# Passcode storage (example: 012)
PASSCODE = [0, 1, 2]
input_code = []
# Interrupt handler for button presses
def interrupt_callback(pin):
global input_code
button_index = BUTTON_PINS.index(pin)
input_code.append(button_index + 1) # Append button number to input_code
print(f"Button {button_index + 1} pressed!")
# Attach interrupts to buttons
for button in BUTTON_PINS:
button.irq(trigger=machine.Pin.IRQ_FALLING, handler=interrupt_callback)
def check_passcode():
if input_code == PASSCODE:
print("Access Granted")
# Logic to turn on/off devices
for led in LED_PINS:
led.on() # Example: Turn on all LEDs if passcode is correct
else:
print("Access Denied")
input_code.clear() # Clear the input code for next attempt
def main():
# Initial sleep to allow for setup
sleep(0.01)
print('Program starting')
while True:
# Polling for switch states if needed
for switch in SWITCH_PINS:
if not switch.value(): # If switch is pressed
print(f"Switch pressed: {SWITCH_PINS.index(switch)}")
# Check passcode after some time or condition
if len(input_code) == len(PASSCODE): # Check passcode when enough inputs are received
check_passcode()
sleep(2) # Delay for a short time to avoid multiple checks
if __name__ == "__main__":
main()