from machine import Pin,ADC,PWM,I2C,Timer
import utime
from pico_i2c_lcd import I2cLcd
sda= Pin(0)
scl= Pin(1)
i2c = I2C(0, sda=sda, scl=scl, freq=400000)
devices=i2c.scan()
if len(devices)<=0:
print("NO Device found")
i2c_addr = devices[0]
lcd = I2cLcd(i2c, i2c_addr, 2, 16)
adc = ADC(26)
servo = PWM(Pin(5,Pin.OUT))
servo.freq(50)
rows = [Pin(i+6,Pin.OUT) for i in range(4)]
cols = [Pin(i+10,Pin.IN,Pin.PULL_UP) for i in range(4) ]
password = "123A"
lcd.putstr("Garage Locked.")
utime.sleep(1)
lcd.clear()
keys=[
['1','2','3','A'],
['4','5','6','B'],
['7','8','9','C'],
['*','0','#','D']
]
def keypad():
for row_index, row_pin in enumerate(rows):
for r in rows:
r.value(1)
row_pin.value(0)
for col_index, col_pin in enumerate(cols):
if col_pin.value() == 0:
return keys[row_index][ col_index]
row_pin.value(1)
return None
for row in rows:
row.value(1)
def check_pass():
lcd.putstr("Enter PIN : ")
global password
inp=""
for i in range(4):
while keypad()==None:
pass
inp+=keypad()
utime.sleep(0.3)
print(inp)
lcd.clear()
if inp==password:
return True
lcd.putstr("Wrong Pin entered!!")
utime.sleep(1)
lcd.clear()
check_pass()
if check_pass():
lcd.putstr("Correct PIN Entered")
utime.sleep(1)
lcd.clear()
lcd.putstr("Garage OPEN")
utime.sleep(1)
lcd.clear()
lcd.putstr("Use potentiometer to open")
def fn(timer):
utime.sleep(0.5)
raw = adc.read_u16()#0-65535
duty = raw/65535*6200+1800
servo.duty_u16(int(duty))
if(duty>=7000):
lcd.clear()
lcd.putstr("Door Opened!!")
t.deinit()
t = Timer(period=1000,mode=Timer.PERIODIC,callback=fn)