import time, random
from machine import Pin, I2C, SoftI2C
from pico_i2c_lcd import I2cLcd
time.sleep(0.1)
i2c = SoftI2C(sda = Pin(0), scl = Pin(1), freq = 400000)
addr = i2c.scan()[0]
lcd = I2cLcd(i2c, addr, 2, 16)
rows = [16, 17, 18, 19]
cols = [20, 21, 22, 26]
row_pins = [Pin(pin, Pin.OUT) for pin in rows]
col_pins = [Pin(pin, Pin.IN, Pin.PULL_DOWN) for pin in cols]
keypad = [
['1', '2', '3', 'A'],
['4', '5', '6', 'B'],
['7', '8', '9', 'C'],
['*', '0', '#', 'D']
]
def scan():
entered_password = ""
while True:
for i, row_pin in enumerate(row_pins):
row_pin.value(1)
for j, col_pin in enumerate(col_pins):
if col_pin.value() == 1:
entered_password += keypad[i][j]
print("Key pressed:", keypad[i][j])
time.sleep(0.2)
row_pin.value(0)
if '#' in entered_password:
entered_password = entered_password[:len(entered_password)-1]
return entered_password
x = random.randint(1,21)
y = random.randint(1,21)
z = x + y
lcd.putstr(str(x))
time.sleep(2)
lcd.move_to(4,0)
lcd.putstr(str(y))
value = int(scan())
lcd.move_to(0,1)
if value==z:
lcd.putstr("Correct Value")
else:
lcd.putstr("Incorrect Value")