import time
time.sleep(0.1) # Wait for USB to become ready
print("Hello, Pi Pico!")
import machine # Import the machine module to interact with hardware components
import time # Import the time module for time-related functions
from utime import sleep # Import sleep from utime for delays
# Define the number of buttons and LEDs
BUTTON_COUNT = 3
LED_COUNT = 9
# Define the starting GPIO numbers for buttons and LEDs
BUTTON_START_ID = 16
LED_START_ID = 7
# Initialize variables to keep track of button presses and passcode status
last_button_time_stamp = 0
key_presses = []
passcode_entered = False
# Function to get the number from a pin object
def PinId(pin):
# Convert the pin object to a string, extract the pin number, and convert it back to an integer
return int(str(pin)[8:11].rstrip(","))
# Function to handle button press interrupts
def interrupt_callback(pin):
global last_button_time_stamp # Access the global variable for the last button timestamp
global passcode_entered # Access the global variable for passcode entry status
cur_button_ts = time.ticks_ms() # Get the current time in milliseconds
button_press_delta = cur_button_ts - last_button_time_stamp # Calculate the time since the last button press
# If more than 200 ms have passed since the last press, it's not a bounce
if button_press_delta > 200:
last_button_time_stamp = cur_button_ts # Update the last button timestamp
key_presses.append(pin) # Add the pressed button to the list of key presses
print(f'Button {PinId(pin) - BUTTON_START_ID} pressed') # Print which button was pressed
if not passcode_entered:
start_timer() # Start the passcode timer if it's not already started
# Function to start the passcode timer
def start_timer():
global passcode_entered
global last_button_time_stamp
last_button_time_stamp = time.ticks_ms() # Reset the last button timestamp
passcode_entered = True # Set the passcode entry status to True
# The main function where the setup and loop logic will run
def main():
global key_presses
global last_button_time_stamp
global passcode_entered
# Initialize the multiplexer control pins
s0 = machine.Pin(27, machine.Pin.OUT)
s1 = machine.Pin(28, machine.Pin.OUT)
mux_in = machine.Pin(26, machine.Pin.IN, machine.Pin.PULL_DOWN)
# Set up the buttons and attach interrupt handlers
buttons = []
for btn_idx in range(BUTTON_COUNT):
button_pin = machine.Pin(BUTTON_START_ID + btn_idx, machine.Pin.IN, machine.Pin.PULL_DOWN)
button_pin.irq(trigger=machine.Pin.IRQ_FALLING, handler=interrupt_callback)
buttons.append(button_pin)
# Define the correct sequence of buttons for the passcode
PASS_CODE = [buttons[0], buttons[2], buttons[1]]
# Initialize the LED pins
LED_pins = [machine.Pin(LED_START_ID + out_id, machine.Pin.OUT) for out_id in range(LED_COUNT)]
last_dev = -1 # Variable to keep track of the last device (multiplexer output)
# Main loop
while True:
binary_code = 0 # Variable to store the binary code from the multiplexer
# Read the binary code from the multiplexer
for selector_val in range(4):
s0.value(selector_val % 2)
s1.value(selector_val // 2)
sleep(0.02) # Wait a bit for the value to stabilize
binary_code += (pow(2, selector_val) * mux_in.value()) # Calculate the binary code
# If the binary code has changed, print it
if last_dev != binary_code:
last_dev = binary_code
print(f'Multiplexer output is now: {last_dev}')
sleep(0.1) # Short delay before the next loop iteration
# Check if the passcode has been entered
if passcode_entered:
# If 3 seconds have passed since the last button press, reset the passcode entry
if time.ticks_diff(time.ticks_ms(), last_button_time_stamp) >= 3000:
print("Time's up! Please enter the passcode again.")
key_presses.clear() # Clear the list of key presses
passcode_entered = False # Reset the passcode entry status
# If the passcode length matches the number of key presses, check if it's correct
elif len(key_presses) == len(PASS_CODE):
if key_presses == PASS_CODE:
print('Passcode is correct!')
# If the binary code is within the range of LEDs, toggle the corresponding LED
if binary_code < LED_COUNT:
print(f'Toggling LED number: {binary_code}')
LED_pins[binary_code].toggle()
else:
print(f'Invalid output: {binary_code}. No action taken.')
else:
print('Passcode is incorrect. Try again.')
key_presses.clear() # Clear the list of key presses
passcode_entered = False # Reset the passcode entry status
# Run the main function if this script is executed
if __name__ == "__main__":
main()