from machine import Pin
import time
RPINS=[2,3,4,5]
CPINS=[6,7,8,9]
KEYMAP=[
['1','2','3','A'],
['4','5','6','B'],
['7','8','9','C'],
['*','0','#','D']
]
rows=[Pin(r,Pin.OUT) for r in RPINS]
for counter in rows:
counter.value(0)
clos=[Pin(c,Pin.IN,Pin.PULL_DOWN) for c in CPINS]
led=Pin(25,Pin.OUT)
def readKeypad():
for i,rpin in enumerate(rows):
rpin.value(1)
for j,cpin in enumerate(clos):
if cpin.value()==1:
time.sleep_ms(50)
if cpin.value()==1:
return KEYMAP[i][j]
rpin.value(0)
return None
print("Keypad ready.Press a key...")
while True:
key=readKeypad()
if key:
print(f"key Pressed:{key}")
led.toggle()
time.sleep_ms(200)
time.sleep_ms(50)