from machine import Pin, PWM
import utime

ROW_PINS = [0,1,2,3]
COL_PINS = [14, 12, 13, 15]

SERVO_PIN = 16
BUZZER_PIN = 17

KEYS = [
    ['1', '2', '3', 'A'],
    ['4', '5', '6', 'B'],
    ['7', '8', '9', 'C'],
    ['*', '0', '#', 'D']
]

PASSWORD = "1234"

row_pins = [Pin(pin, Pin.OUT, Pin.PULL_DOWN) for pin in ROW_PINS]
col_pins = [Pin(pin, Pin.IN, Pin.PULL_DOWN) for pin in COL_PINS]

servo = PWM(Pin(SERVO_PIN), freq=50)
buzzer = Pin(BUZZER_PIN, Pin.OUT)

def get_keypad_input():
    key = None
    for i, row_pin in enumerate(row_pins):
        row_pin.on()
        for j, col_pin in enumerate(col_pins):
            if col_pin.value() == 1:
                key = KEYS[i][j]
                break
        row_pin.off()
        if key:
            break
    return key

def setServoCycle(position):
    pwm.duty_u16(position)
    time.sleep(0.01)

def sound_buzzer():
    buzzer.on()
    utime.sleep(0.5)
    buzzer.off()

def open_door():
    servo.duty_u16(50)
    utime.sleep(2)
    servo.duty_u16(0)

def main():
    entered_password = ""
    while True:
        key = get_keypad_input()
        if key:
            if key == "#":
                if entered_password == PASSWORD:
                    print("Password correct. Opening door.")
                    open_door()
                else:
                    print("Password incorrect. Sounding buzzer.")
                    sound_buzzer()
                entered_password = ""
            else:
                entered_password += key
                print("Entered password:", entered_password)
            utime.sleep(0.2)

if __name__ == "__main__":
    main()
BOOTSELLED1239USBRaspberryPiPico©2020RP2-8020/21P64M15.00TTT