import machine
import time
BUTTON_COUNT = 3
LED_COUNT = 9
INPUT_COUNT = 4
BUTTON_START_ID = 16
LED_GPIO_START = 7
last_button_time_stamp = 0
key_presses = []
# Constants for pin numbers
BUTTON_START_ID = 16
# Update the LED_GPIO_START to the first LED pin number
LED_GPIO_START = [10, 11, 12, 13, 14] # Example GPIO pin numbers for LEDs
# Initialize last button timestamp
last_button_time_stamp = 0
# Initialize key presses list
key_presses = []
# Function to extract the numeric pin id from the Pin instance
def PinId(pin):
return int(str(pin)[8:11].rstrip(","))
# Interrupt callback function
def interrupt_callback(pin):
global last_button_time_stamp
cur_button_ts = time.ticks_ms()
button_press_delta = cur_button_ts - last_button_time_stamp
if button_press_delta > 200:
last_button_time_stamp = cur_button_ts
key_presses.append(PinId(pin))
# Report the key press
print(f'Key pressed: {PinId(pin) - BUTTON_START_ID}')
# Main function
def main():
global key_presses
# Define the passcode sequence
PASS_CODE = [0, 2, 1] # Assuming buttons are numbered 0, 1, 2
# Setup buttons with interrupts
buttons = []
for btn_idx in range(0, BUTTON_COUNT):
buttons.append(machine.Pin(BUTTON_START_ID + btn_idx, machine.Pin.IN, machine.Pin.PULL_DOWN))
buttons[-1].irq(trigger=machine.Pin.IRQ_FALLING, handler=interrupt_callback)
# Setup LEDs
leds = []
# Initialize each LED using the pin numbers from the LED_GPIO_START list
for led_pin in LED_GPIO_START:
pin = machine.Pin(led_pin, machine.Pin.OUT)
leds.append(pin)
# Main loop
while True:
# Check if the passcode is entered
if len(key_presses) >= len(PASS_CODE):
# Compare the entered sequence with the passcode
if key_presses[:len(PASS_CODE)] == PASS_CODE:
print('Correct passcode entered')
# Toggle all LEDs
for led in leds:
led.toggle()
else:
print('Incorrect passcode')
# Reset key presses
key_presses = key_presses[len(PASS_CODE):]
time.sleep(0.1)
# Run the main function
if __name__ == "__main__":
main()