import RPi.GPIO as GPIO # For Raspberry Pi
import time
# Define GPIO pins for the 7-segment display
segment_pins = [28,27,26,22,21,20,19]
# Define GPIO pins for the keypad rows and columns
row_pins = [13,12,11,10]
col_pins = [9,8,7,6]
# Define the 7-segment display digit map
digit_map = {
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]
}
# Setup GPIO pins
GPIO.setmode(GPIO.BOARD)
for pin in segment_pins:
GPIO.setup(pin, GPIO.OUT)
for row_pin in row_pins:
GPIO.setup(row_pin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
for col_pin in col_pins:
GPIO.setup(col_pin, GPIO.OUT)
try:
while True:
# Read keypad input
for col_pin in col_pins:
GPIO.output(col_pin, GPIO.LOW)
for row_pin in row_pins:
if GPIO.input(row_pin) == GPIO.LOW:
key = (col_pins.index(col_pin) * len(row_pins)) + row_pins.index(row_pin)
if key in digit_map:
digit = digit_map[key]
for i, value in enumerate(digit):
GPIO.output(segment_pins[i], value)
GPIO.output(col_pin, GPIO.HIGH)
time.sleep(0.1)
except KeyboardInterrupt:
GPIO.cleanup()