from machine import Pin ,
import utime
# constants
BUTTON_COUNT = 3
LED_COUNT = 9
INPUT_COUNT = 4
# MUX Pins
s0 = machine.Pin(27, machine.Pin.OUT)
s1 = machine.Pin(28, machine.Pin.OUT)
muxIn = machine.Pin(26, machine.Pin.IN)
# Initialize global variables
last_button_time_stamp = 0
button_pressed = False
key_presses = []
BUTTON_START_ID = 16 # Assuming the first button starts at pin 16
LED_GPIO_START = 7
# Define the pins for the buttons
b1 = Pin(16, Pin.IN, Pin.PULL_DOWN)
b2 = Pin(17, Pin.IN, Pin.PULL_DOWN)
b3 = Pin(18, Pin.IN, Pin.PULL_DOWN)
# Define the pins for the leds
led0=pin(15.Pin.OUT)
led1=pin(14.Pin.OUT)
led3=pin(13.Pin.OUT)
led4=pin(12.Pin.OUT)
led5=pin(11.Pin.OUT)
led6=pin(10.Pin.OUT)
led7=pin(9.Pin.OUT)
led8=pin(8.Pin.OUT)
led9=pin(7.Pin.OUT)
# Define the passcode
PASSCODE = [0, 2, 1]
entered = []
# Function to get the numeric ID of the pin
def PinId(pin):
# Correctly slice the string to get the pin number
return int(str(pin)[8:11].rstrip(","))
# Interrupt callback function for button press events
def interrupt_callback(pin):
global last_button_time_stamp
global button_pressed
global key_presses
# Debouncing
cur_button_ts = utime.ticks_ms() # Get the current time in milliseconds
button_press_delta = cur_button_ts - last_button_time_stamp # Calculate the time difference since the last button press
if button_press_delta > 200:
last_button_time_stamp = cur_button_ts # Update the last button time stamp
key_presses.append(pin) # Add the pin to the list of key presses
print(f'key press: {PinId(pin) - BUTTON_START_ID}') # Print the button ID
def button_callback(pin):
led.toggel()
def clearEntered(pin):
global entered
if entered:
print("Clearing entered sequence due to inactivity...")
timeoutLed.value(1)
utime.sleep(0.75)
timeoutLed.value(0)
entered = []
utime.sleep(0.2)
def main():
global last_button_time_stamp
global button_pressed
button_in = machine.Pin(BUTTON_PIN, machine.Pin.IN, machine.Pin.PULL_DOWN)
button_in.irq(trigger=machine.Pin.IRQ_FALLING, handler=interrupt_callback)