from time import sleep
from machine import Pin
# seven segment display
#
# a
# ---
# f| |b
# |_g_|
# e| |c
# |___|
# d
# pins: 2, 3, 4, 5, 6, 7, 8
# Define segment pins (a, b, c, d, e, f, g)
segments = [Pin(pin, Pin.OUT) for pin in range(2, 9)]
# output pins - ROWS
# 20, 21, 22, 26
out_pins = []
for i in range(20, 23):
out_pins.append(Pin(i, Pin.OUT))
# then: 20 -> out_pins[0]
out_pins.append(Pin(26, Pin.OUT))
# input pins - COLS
# 16, 17, 18, 19
in_pins = []
for i in range(16, 20):
in_pins.append(Pin(i, Pin.IN, Pin.PULL_DOWN))
# then: 16 -> in_pins[0]
keys = [
['D', '#', '0', '*'],
['C', '9', '8', '7'],
['B', '6', '5', '4'],
['A', '3', '2', '1']
]
# Mapping of numbers to seven-segment display segments (0-9)
hex_to_segments = {
0: (0, 0, 0, 0, 0, 1, 0), # Segments for displaying '0'
1: (1, 0, 0, 1, 1, 1, 1), # Segments for displaying '1'
2: (0, 0, 1, 0, 0, 0, 1), # Segments for displaying '2'
3: (0, 0, 0, 0, 1, 0, 1), # Segments for displaying '3'
4: (1, 0, 0, 1, 1, 0, 0), # Segments for displaying '4'
5: (0, 1, 0, 0, 1, 0, 0), # Segments for displaying '5'
6: (0, 1, 0, 0, 0, 0, 0), # Segments for displaying '6'
7: (0, 0, 0, 1, 1, 1, 1), # Segments for displaying '7'
8: (0, 0, 0, 0, 0, 0, 0), # Segments for displaying '8'
9: (0, 0, 0, 0, 1, 0, 0), # Segments for displaying '9'
10: (0, 0, 0, 1, 0, 0, 0), # Segments for displaying 'A'
11: (1, 1, 0, 0, 0, 0, 0), # Segments for displaying 'B'
12: (0, 1, 1, 0, 0, 1, 0), # Segments for displaying 'C'
13: (1, 0, 0, 0, 0, 0, 1), # Segments for displaying 'D'
14: (0, 1, 1, 0, 0, 0, 0), # Segments for displaying 'E'
15: (0, 1, 1, 1, 0, 0, 0), # Segments for displaying 'F'
16: (0, 0, 0, 0, 0, 0, 0), # Segment for Display of *
17: (1, 1, 1, 1, 1, 1, 1) # Segment for Display of #
}
# Function to display a number on the seven-segment display
def display_number(num):
segments_state = hex_to_segments.get(num, (0, 0, 0, 0, 0, 0, 0)) # Get segments for the number
for pin, state in zip(segments, segments_state):
pin.value(state) # Turn segments on/off based on the state
# Display numbers 0 to 9 repeatedly
while True:
# switch output pins to log. 1
for i in range(4):
row = out_pins[i]
# switch on one of rows
row.on()
for j in range(4):
# see value of the pin
col = in_pins[j]
if (col.value() == 1):
print(i, j, keys[i][j])
if (keys[i][j] == "A"):
disp = int(10)
elif (keys[i][j] == "B"):
disp = int(11)
elif (keys[i][j] == "C"):
disp = int(12)
elif (keys[i][j] == "D"):
disp = int(13)
elif (keys[i][j] == "*"):
disp = int(16)
elif (keys[i][j] == "#"):
disp = int(17)
else:
disp = int(keys[i][j])
display_number(disp)
# switch off the row
row.off()