from machine import Pin
import utime
from machine import I2C, Pin
from time import sleep
from pico_i2c_lcd import I2cLcd
import random
i2c = I2C(0, sda=Pin(0), scl=Pin(1), freq=400000)
I2C_ADDR = i2c.scan()[0]
lcd = I2cLcd(i2c, I2C_ADDR, 4, 20)
# Configure Row and Column Pins
row_pins = [2, 3, 4, 5]
col_pins = [6, 7, 8, 9]
# Initialize rows as outputs, cols as inputs with pull-up
rows = [Pin(pin, Pin.OUT) for pin in row_pins]
cols = [Pin(pin, Pin.IN, Pin.PULL_UP) for pin in col_pins]
# Key map
keys = [["1", "2", "3", "A"], ["4", "5", "6", "B"], ["7", "8", "9", "C"], ["*", "0", "#", "D"]]
def get_key():
for r in range(4):
rows[r].value(0) # Set row low
for c in range(3):
if cols[c].value() == 0: # If column is low, key pressed
key = keys[r][c]
while cols[c].value() == 0: pass # Wait for release
rows[r].value(1) # Reset row high
return key
rows[r].value(1) # Reset row high
return None
score = 0
i = 0
def addition():
for i in range(1):
num1 = random.randint(1, 1000)
print("This is num1: " + str(random.randint(1, 100)))
num2 = random.randint(1, 100)
answer = num1 + num2
answerstr = str(answer)
num1 = str(num1)
num2 = str(num2)
lcd.putstr(num1 + "+" + num2 + " = " + answerstr)
key = get_key()
while key != "#":
key = str(key) + str(get_key())
if key == answer:
lcd.putstr("Correct")
score = score+ 1
else:
lcd.putstr("Incorrect")
lcd.putstr("Your score is")
lcd.putstr(score)
# Main Loop
addition()
while True:
key = get_key()
if key:
lcd.putstr(key)
utime.sleep(0.1)