from machine import Pin, I2C, PWM
import utime
from time import sleep
from pico_i2c_lcd import I2cLcd
from picozero import Speaker
speaker = Speaker(15)
buzz = 1
PIR = Pin(13,Pin.IN, Pin.PULL_UP)
i2c = I2C(0, sda=Pin(0), scl=Pin(17), freq=400000)
I2C_ADDR = i2c.scan()[0]
lcd = I2cLcd(i2c, I2C_ADDR, 2, 16)
col_list=[1,2,3,4]
row_list=[5,6,7,8]
PASSWORD = "1234" # change me
user_password = ""
for x in range(0,4):
row_list[x]=Pin(row_list[x], Pin.OUT)
row_list[x].value(1)
for x in range(0,4):
col_list[x] = Pin(col_list[x], Pin.IN, Pin.PULL_UP)
key_map=[["D","#","0","*"],\
["C","9","8","7"],\
["B","6","5","4"],\
["A","3","2","1"]]
def Keypad4x4Read(cols,rows):
for r in rows:
r.value(0)
result=[cols[0].value(),cols[1].value(),cols[2].value(),cols[3].value()]
if min(result)==0:
key=key_map[int(rows.index(r))][int(result.index(0))]
r.value(1)
return(key)
r.value(1)
while True:
key = Keypad4x4Read(col_list, row_list)
motion = PIR.value()
if key != None:
if key != "D":
lcd.putstr(key)
user_password+=key
utime.sleep(0.3)
else:
if user_password == PASSWORD:
lcd.clear()
lcd.putstr("CORRECT PASSWORD")
utime.sleep(2)
lcd.clear()
buzz = 0
else:
lcd.clear()
lcd.putstr("INCORRECT PASSWORD")
speaker.on()
utime.sleep(2)
speaker.off()
lcd.clear()
if motion == 1 and buzz == 1:
speaker.on()