import time
from machine import Pin,I2C
from lcd_api import LcdApi
from i2c_lcd import I2cLcd
i2c = I2C(0, sda=Pin(0), scl=Pin(1), freq=400000)
I2C_ADDR = 0x27
lcd= I2cLcd(i2c, I2C_ADDR, 2, 16)
clk = Pin(14, Pin.IN, Pin.PULL_UP)
dt = Pin(15, Pin.IN, Pin.PULL_UP)
btn = Pin(16, Pin.IN, Pin.PULL_UP)
dir_pin = Pin(2, Pin.OUT)
step_pin = Pin(3, Pin.OUT)
position = 0
last_clk = clk.value()
ROW = [6, 7, 8, 9]
COL = [10, 11, 12, 13]
KEYS = [
["1", "2", "3", "A"],
["4", "5", "6", "B"],
["7", "8", "9", "C"],
["*", "0", "#", "D"]
]
rows = [Pin(pin, Pin.OUT) for pin in ROW]
cols = [Pin(pin, Pin.IN, Pin.PULL_DOWN) for pin in COL]
def check_keypad():
for i, row in enumerate(rows):
for r in rows:
r.value(0)
row.value(1)
for j, col in enumerate(cols):
if col.value() == 1:
return KEYS[i][j]
return None
last_key = None
password = "123A#"
given_password=""
lcd.clear()
lcd.putstr("Password: ")
lcd.move_to(0,1)
lcd.putstr("Position: ")
lcd.move_to(10,0)
while True:
key = check_keypad()
if key != last_key:
if key is not None:
print("Key:", key)
given_password=given_password+key
lcd.putstr(str(key))
last_key = key
clk_state = clk.value()
dt_state = dt.value()
if given_password==password:
if clk_state != last_clk:
if dt_state != clk_state:
if position < 200:
position += 01
dir_pin.value(1)
else:
if position > -200:
position -= 1
dir_pin.value(0)
step_pin.on()
time.sleep(0.001)
step_pin.off()
time.sleep(0.001)
print(f"Position: {position}")
last_clk = clk_state
if btn.value() == 0:
position = 0
print("Reset to 0")
time.sleep(0.5)
lcd.move_to(12,1)
lcd.putstr(str(position))
time.sleep(0.001)