from utime import sleep
from machine import Pin, I2C, PWM
from lcd_api import LcdApi
from pico_i2c_lcd import I2cLcd

red = Pin(16, Pin.OUT)
green = Pin(17, Pin.OUT)

servo = PWM(Pin(28))
# Set Duty Cycle for Different Angles
max_duty = 7864
min_duty = 1802
half_duty = int(max_duty/2)

#Set PWM frequency
frequency = 50
servo.freq (frequency)

servo.duty_u16(min_duty)
#Servo at 90 degrees
servo.duty_u16(min_duty)

I2C_ADDR     = 0x27
I2C_NUM_ROWS = 4
I2C_NUM_COLS = 20

i2c = I2C(0, sda=Pin(0), scl=Pin(1), freq=400000)
lcd = I2cLcd(i2c, I2C_ADDR, I2C_NUM_ROWS, I2C_NUM_COLS)    
sleep(1)
lcd.clear()
lcd.move_to(1,0)
lcd.putstr("Enter The Password")
lcd.move_to(4,1)
lcd.putstr("To be unlock")
lcd.move_to(0,2)

matrix_keys = [
    ["1", "2", "3"],
    ["4", "5", "6"],
    ["7", "8", "9"],
    ["*", "0", "#"],
]

rows = [Pin(15, Pin.OUT), Pin(14, Pin.OUT), Pin(13, Pin.OUT), Pin(12, Pin.OUT)]
cols = [Pin(11, Pin.OUT), Pin(10, Pin.OUT), Pin(9, Pin.OUT)]

password = "13122008"
user_input = ""

def synce_keys():
    global password
    global user_input
    for row in range(len(rows)):
        rows[row].high()
        for col in range(len(cols)):
            if cols[col].value() == 1:
                key = matrix_keys[row][col]
                if key != "#":
                    lcd.putchar(key)
                    user_input += key
                elif user_input == password:
                    green.value(1)
                    # sleep(2)
                    # green.value(0)
                    servo.duty_u16(int(((max_duty - min_duty) / 2) + min_duty))
                    lcd.clear()
                    lcd.move_to(6, 1)
                    lcd.putstr("Unlocked")
                    lcd.move_to(1, 2)
                    lcd.putstr("Wellcome With You.")
                    return 1
                else:
                    red.value(1)
                    user_input = ""
                    lcd.clear()
                    lcd.move_to(3, 1)
                    lcd.putstr("Wrong Password")
                    lcd.move_to(6, 2)
                    lcd.putstr("Try Agin")
                    sleep(2)
                    red.value(0)
                    lcd.clear()
                    lcd.move_to(1,0)
                    lcd.putstr("Enter The Password")
                    lcd.move_to(4,1)
                    lcd.putstr("To be unlock")
                    lcd.move_to(0,2)
                sleep(0.5)
        rows[row].low()

while True:
    if synce_keys() == 1:
        break;
$abcdeabcde151015202530fghijfghij
BOOTSELLED1239USBRaspberryPiPico©2020RP2-8020/21P64M15.00TTT