from machine import Pin, I2C, ADC, PWM
from time import sleep
from i2c_lcd import I2cLcd
from keypad import Keypad
i2c = I2C(0, scl=Pin(17), sda=Pin(16), freq=400000)
lcd = I2cLcd(i2c, 0x27, 2, 16)
adc = ADC(26) # Potentiometer connected to GP26
servo = PWM(Pin(15))
servo.freq(50)
ROWS = [2, 3, 4, 5]
COLS = [6, 7, 8, 9]
KEYS = [
['1', '2', '3', 'A'],
['4', '5', '6', 'B'],
['7', '8', '9', 'C'],
['*', '0', '#', 'D']
]
row_pins = [Pin(pin_name, Pin.OUT) for pin_name in ROWS]
col_pins = [Pin(pin_name, Pin.IN, Pin.PULL_DOWN) for pin_name in COLS]
keypad = Keypad(KEYS, row_pins, col_pins)
CORRECT_PIN = "1234"
def set_servo(angle):
duty = int(1638 + (angle / 180) * 4912)
servo.duty_u16(duty)
def read_potentiometer():
return adc.read_u16() * 180 // 65535
def get_pin():
lcd.clear()
lcd.putstr("Enter PIN:")
pin = ""
while len(pin) < 4:
key = keypad.get_key()
if key and key.isdigit():
pin += key
lcd.putstr("*")
sleep(0.3)
return pin
while True:
lcd.clear()
lcd.putstr("Welcome Driver")
sleep(1)
entered_pin = get_pin()
if entered_pin == CORRECT_PIN:
lcd.clear()
lcd.putstr("Access Granted")
sleep(1)
lcd.clear()
lcd.putstr("Opening Door...")
for _ in range(20):
angle = read_potentiometer()
set_servo(angle)
lcd.move_to(0, 1)
lcd.putstr("Angle: {:>3}".format(angle))
sleep(0.1)
lcd.clear()
lcd.putstr("Door Opened")
sleep(2)
else:
lcd.clear()
lcd.putstr("Wrong PIN")
sleep(2)