import time
time.sleep(0.1) # Wait for USB to become ready

print("Hello, Pi Pico!")


from machine import Pin,PWM
import utime


SERVO_PIN = 4 
ROW_PINS = [0, 1, 2, 3]  
COL_PINS = [16,17,18,19]  
BUZZER_PIN = 5

# Initialize servo motor
servo = PWM(Pin(SERVO_PIN))
servo.freq(50)


# Initialize keypad pins
row_pins = [Pin(pin, Pin.OUT) for pin in ROW_PINS]
col_pins = [Pin(pin, Pin.IN, Pin.PULL_DOWN) for pin in COL_PINS]
print(f'row pin {row_pins}')
print(f'col pin {col_pins}')

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

# Initialize buzzer
buzzer = Pin(BUZZER_PIN, Pin.OUT)


password = '1234'



def scan_keypad():
    for i in range(len(ROW_PINS)):
        row_pins[i].value(1)  
        for j in range(len(COL_PINS)):
            if col_pins[j].value() == 1:  
                return keypad[i][j]  
        row_pins[i].value(0)  
    return None  


def activate_servo():
    
    print("Servo activated")
    for i in range(1000,9000,50):
        servo.duty_u16(i)
        time.sleep(0.1)

    time.sleep(5)

    for i in range(9000,1000,-50):
        servo.duty_u16(i)
        time.sleep(0.1)


entered_password = ''
while True:
    key = scan_keypad()
    if key:
        if key == '#':
            print(entered_password)
            if entered_password == password:
                activate_servo()
            else:
                print("Wrong password!")
                buzzer.on()
                utime.sleep(1)
                buzzer.off()
            entered_password = ''  
        else:
            entered_password += key
    utime.sleep(0.1)  
BOOTSELLED1239USBRaspberryPiPico©2020RP2-8020/21P64M15.00TTT