import time
import board
import digitalio
# -----------------------------------------
# Keypad setup (4x4)
# -----------------------------------------
rows = [board.GP13, board.GP12, board.GP11, board.GP10]
cols = [board.GP9, board.GP8, board.GP7, board.GP6]
row_pins = []
col_pins = []
for r in rows:
pin = digitalio.DigitalInOut(r)
pin.direction = digitalio.Direction.OUTPUT
pin.value = True # High (inactive)
row_pins.append(pin)
for c in cols:
pin = digitalio.DigitalInOut(c)
pin.direction = digitalio.Direction.INPUT
pin.pull = digitalio.Pull.UP
col_pins.append(pin)
# Key map
keys = [
['1','2','3','A'],
['4','5','6','B'],
['7','8','9','C'],
['*','0','#','D']
]
# -----------------------------------------
# Keypad scanning function
# -----------------------------------------
def scan_keypad():
"""Scan 4x4 keypad and return pressed key or None"""
for r in range(4):
row_pins[r].value = False # Activate row
for c in range(4):
if not col_pins[c].value: # Key pressed
row_pins[r].value = True
return keys[r][c]
row_pins[r].value = True # Deactivate row
return None
# -----------------------------------------
# 7-Segment Character Map
# -----------------------------------------
segments = {
'0':[1,1,1,1,1,1,0],
'1':[0,1,1,0,0,0,0],
'2':[1,1,0,1,1,0,1],
'3':[1,1,1,1,0,0,1],
'4':[0,1,1,0,0,1,1],
'5':[1,0,1,1,0,1,1],
'6':[1,0,1,1,1,1,1],
'7':[1,1,1,0,0,0,0],
'8':[1,1,1,1,1,1,1],
'9':[1,1,1,1,0,1,1],
'A':[1,1,1,0,1,1,1],
'B':[0,0,1,1,1,1,1],
'C':[1,0,0,1,1,1,0],
'D':[0,1,1,1,1,0,1],
'-':[0,0,0,1,0,0,0],
'h':[0,0,1,1,1,1,1],
'E':[1,0,1,1,1,1,0], # Exit pattern
' ':[0,0,0,0,0,0,0]
}
# -----------------------------------------
# Segment pins
# -----------------------------------------
seg_pins = [
board.GP16, # a
board.GP17, # b
board.GP18, # c
board.GP19, # d
board.GP20, # e
board.GP21, # f
board.GP22 # g
]
seg_outputs = []
for p in seg_pins:
pin = digitalio.DigitalInOut(p)
pin.direction = digitalio.Direction.OUTPUT
seg_outputs.append(pin)
# -----------------------------------------
# Display function
# -----------------------------------------
def display(char):
pattern = segments.get(char, segments[' '])
for i in range(7):
seg_outputs[i].value = pattern[i]
# -----------------------------------------
# Main loop with EXIT functionality
# -----------------------------------------
last_key = None
debounce_time = 0.2 # Improved debouncing
last_press_time = 0
print(" KEYPAD 7-SEGMENT DISPLAY READY!")
print("Press 0-9, A-D, *, #")
print("Press 'B' twice quickly to EXIT")
print("-" * 30)
while True:
key = scan_keypad()
current_time = time.time()
if key and key != last_key and (current_time - last_press_time) > debounce_time:
print(f"Key pressed: '{key}'")
last_press_time = current_time
# ---- EXIT on double B press ----
if key == 'B' and last_key == 'B':
print("\n Exiting program... Goodbye!")
display(' ')
time.sleep(1)
break
# ---- Numeric ----
elif key.isdigit():
display(key)
# ---- A-D ----
elif key in ['A','C','D']:
display(key)
elif key == 'B':
display('B')
# ---- Special ----
elif key == '*':
display('-')
elif key == '#':
display('h')
last_key = key
elif not key:
last_key = None # Reset when key released
time.sleep(0.01) # Faster loop for better responsiveness