from machine import Pin
from utime import sleep
rows = [Pin(i, Pin.OUT) for i in (2,3,4,5)]
cols = [Pin(i, Pin.IN, Pin.PULL_UP) for i in (6,7,8,9)]
def scan_keypad():
for row_index, row_pin in enumerate(rows):
row_pin.value(0)
for col_index, col_pin in enumerate(cols):
if col_pin.value() == 0:
return [row_index, col_index]
row_pin.value(1)
return None
for row in rows:
row.value(1)
while True:
key = scan_keypad()
if key:
print(f"Button pressed at [row, col]: {key}")
while scan_keypad():
sleep(0.05)
sleep(0.05)