from machine import I2C, Pin, Timer
import time
from pico_i2c_lcd import I2cLcd
PINCode = str("1234")
# Initialize I2C Bus
i2c = I2C(0, sda=Pin(0), scl=Pin(1), freq=400000)
# Scan I2C bus and initialize I2C LCD
I2C_ADDR = i2c.scan()[0]
I2C_LCD = I2cLcd(i2c, I2C_ADDR, 4, 20)
# Write Line one string
#I2C_LCD.putstr('Raspberry Pi\n')
# Write Line two string
#I2C_LCD.putstr('Pico 16x2 LCD')
#time.sleep(5)
from machine import Pin
import utime
# Connect Keypad pins as below
col_list=[12,13,15,16]
row_list=[8,9,10,11]
#Set rows pin as output
for x in range(0,4):
row_list[x]=Pin(row_list[x], Pin.OUT)
row_list[x].value(1)
#Set column as input
for x in range(0,4):
col_list[x] = Pin(col_list[x], Pin.IN, Pin.PULL_UP)
# Create a map between keypad buttons and chars
key_map=[["D","C","B","A"],\
["#","9","6","3"],\
["0","8","5","2"],\
["*","7","4","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) # manages key keept pressed
return(key)
r.value(1)
# Start the main loop
step=1
inputedPIN=str("")
invalidPIN=str("")
#print("--- Ready to get user inputs ---")
def waitForInputPIN():
global inputedPIN, invalidPIN
key = None
if invalidPIN=="True":
I2C_LCD.clear()
I2C_LCD.putstr('Wrong PIN, try again and press # after\nyour PIN: ')
elif invalidPIN=="":
I2C_LCD.putstr("Enter your PIN and\npress # after your\nPIN: ")
while key != '#':
key=Keypad4x4Read(col_list, row_list)
if key != None and key !="#":
I2C_LCD.putstr("*")
if step==1:
inputedPIN=inputedPIN+str(key)
utime.sleep(0.3)
if inputedPIN==PINCode:
I2C_LCD.clear()
I2C_LCD.putstr("Welcome!")
invalidPIN="False"
else:
invalidPIN="True"
inputedPIN=""
waitForInputPIN()
waitForInputPIN()
print("END!")