from machine import Pin
import utime
keyMatrix = [
[ "1", "2", "3", "A" ],
[ "4", "5", "6", "B" ],
[ "7", "8", "9", "C" ],
[ "*", "0", "#", "D" ]
]
rowPins = [7,6,5,4]
colPins = [3,2,1,0]
row = []
column = []
for item in rowPins:
row.append(machine.Pin(item,machine.Pin.OUT))
for item in colPins:
column.append(machine.Pin(item,machine.Pin.IN,machine.Pin.PULL_DOWN))
key = '0'
def scanKeypad():
global key
for rowKey in range(4):
row[rowKey].value(1)
for colKey in range(4):
if column[colKey].value() == 1:
key = keyMatrix[rowKey][colKey]
row[rowKey].value(0)
return key
row[rowKey].value(0)
return None
def printKey():
key=scanKeypad()
if key is not None:
print("Key pressed is:{}".format(key))
utime.sleep(0.2)
while True:
printKey()