from machine import Pin
from time import sleep_ms, ticks_ms, ticks_diff
# CONFIGURATION — change according to your keypad
ROWS = [2, 3, 4, 5] # GPIOs connected to rows
COLS = [6, 7, 8, 9, 10] # GPIOs connected to columns
key_map = [
['A1', 'A2', 'A3', 'A4', 'A5'],
['B1', 'B2', 'B3', 'B4', 'B5'],
['C1', 'C2', 'C3', 'C4', 'C5'],
['D1', 'D2', 'D3', 'D4', 'D5']
]
# Constants
DEBOUNCE_MS = 50
# Initialize rows as output
row_pins = [Pin(pin_num, Pin.OUT) for pin_num in ROWS]
# Initialize columns as input with pull-up
col_pins = [Pin(pin_num, Pin.IN, Pin.PULL_UP) for pin_num in COLS]
# State tracking for debouncing
last_state = [[False for _ in COLS] for _ in ROWS]
last_time = [[0 for _ in COLS] for _ in ROWS]
def scan_keypad():
for r, row in enumerate(row_pins):
row.low() # Drive current row LOW
for c, col in enumerate(col_pins):
pressed = not col.value() # Active LOW
current_time = ticks_ms()
if pressed != last_state[r][c]:
if ticks_diff(current_time, last_time[r][c]) > DEBOUNCE_MS:
last_state[r][c] = pressed
last_time[r][c] = current_time
#chr#(ord('A') + r) wont save with this in here!
if pressed:
print(key_map[r][c])
row.high() # Set row back HIGH
while True:
scan_keypad()
sleep_ms(10)