from machine import Pin
from time import sleep
# تعداد پینهای LED و سطر و ستون کیپد
LEDS = 12
ROWS = 4
COLS = 4
# تعریف کلیدهای کیپد
keys = [
['1', '2', '3', 'A'],
['4', '5', '6', 'B'],
['7', '8', '9', 'C'],
['*', '0', '#', 'D']
]
# پینهای متصل به LEDها
ledPins = [Pin(pin, Pin.OUT) for pin in [15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3]]
# پینهای متصل به سطرهای کیپد
rowPins = [Pin(pin, Pin.OUT) for pin in [26, 22, 21, 20]]
# پینهای متصل به ستونهای کیپد
colPins = [Pin(pin, Pin.IN, Pin.PULL_UP) for pin in [19, 18, 17, 16]]
# پسورد صحیح
correct_password = "1234"
def setup():
# خاموش کردن تمام LEDها در ابتدا
for led in ledPins:
led.value(0)
def read_keypad():
for i in range(ROWS):
# فعال کردن یک سطر در هر لحظه
rowPins[i].value(0)
for j in range(COLS):
# اگر یکی از ستونها فعال باشد، کلید فشرده شده است
if colPins[j].value() == 0:
# صبر کنید تا از بین رفتن نویزهای لحظهای
sleep(0.05)
# منتظر بمانید تا کلید آزاد شود
while colPins[j].value() == 0:
pass
# غیرفعال کردن سطر
rowPins[i].value(1)
return keys[i][j] # بازگرداندن کلید فشرده شده
# غیرفعال کردن سطر
rowPins[i].value(1)
return None
def check_password():
entered_password = ""
print("Enter Password:")
while True:
key = read_keypad()
if key:
if key == '#':
if entered_password == correct_password:
print("Password Correct")
return True
else:
print("Password Incorrect")
entered_password = "" # پاک کردن پسورد وارد شده
elif key == '*':
entered_password = "" # پاک کردن پسورد وارد شده
print(f"Current Input: {entered_password}")
else:
entered_password += key
print(f"Current Input: {entered_password}")
sleep(0.1)
def loop():
key = read_keypad()
if key:
print("Pressed:", key) # چاپ کلید فشرده شده برای بررسی
if key == '1':
ledPins[0].value(1)
elif key == '2':
ledPins[1].value(1)
elif key == '3':
ledPins[2].value(1)
elif key == '4':
ledPins[3].value(1)
elif key == '5':
ledPins[4].value(1)
elif key == '6':
ledPins[5].value(1)
elif key == '7':
ledPins[6].value(1)
elif key == '8':
ledPins[7].value(1)
elif key == '9':
ledPins[8].value(1)
elif key == '0':
for i in range(13):
ledPins[i].value(0)
elif key == 'A':
ledPins[9].value(1)
elif key == 'B':
ledPins[10].value(1)
elif key == 'C':
ledPins[11].value(1)
elif key == 'D':
ledPins[12].value(1)
elif key == '#':
for i in range(13):
ledPins[i].value(1)
sleep(0.01)
setup()
# تأیید پسورد قبل از اجرای عملیات اصلی
if check_password():
while True:
loop()