from machine import Pin
import time
import keypad
# Set up GPIO pins for the 4x4 Keypad
rows = [Pin(i, Pin.OUT) for i in (2, 3, 4, 5)]
cols = [Pin(i, Pin.IN, Pin.PULL_UP) for i in (6, 7, 8, 9)]
# Create a keypad object
keypad = keypad.Keypad(rows, cols)
# Define the correct password sequence (progressive password)
# User must enter the password one digit at a time
correct_password = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '0'] # Example progressive password
# This will store the user's input
user_input = []
# Function to show the input on a simple LED indicator (optional)
def led_feedback(state):
led = Pin(25, Pin.OUT) # Built-in LED on pin 25 of Raspberry Pi Pico
led.value(state)
def check_password():
global user_input
if user_input == correct_password:
led_feedback(1) # Turn on LED if correct password
print("Password Correct! Access Granted.")
user_input = [] # Clear input after success
else:
led_feedback(0) # Turn off LED if wrong password
print("Incorrect Password! Try Again.")
user_input = [] # Clear input after failure
def main():
global user_input
print("Enter password:")
while True:
key = keypad.pressed()
if key:
print("Key pressed:", key)
user_input.append(key) # Add the pressed key to the input list
if len(user_input) > len(correct_password):
print("Too many digits, reset.")
user_input = [] # Reset input if too long
# Check if the password is correct
check_password()
time.sleep(0.2) # Debounce delay to avoid multiple detections
if __name__ == '__main__':
main()