from machine import Pin
import time
# Define the keypad layout
keypad = [
['1', '2', '3', 'A'],
['4', '5', '6', 'B'],
['7', '8', '9', 'C'],
['*', '0', '#', 'D']
]
# Define the pins for the rows and columns
rows = [Pin(i, Pin.OUT) for i in range(0, 3)]
cols = [Pin(i, Pin.IN, Pin.PULL_UP) for i in range(4, 7)]
def read_keypad():
for col in range(3):
cols[col].high()
for row in range(3):
if rows[row].value() == 1:
cols[col].low()
return keypad[row][col]
cols[col].low()
return None
def get_input():
input_string = ''
while len(input_string) < 2:
key = read_keypad()
if key is not None:
input_string += key
time.sleep(0.3) # Debounce delay
return input_string
# Main loop
while True:
result = get_input()
print("Keypad Input: ", result)
# Define LED pins
led_pins = {
'45': Pin(28, Pin.OUT),
'99': Pin(27, Pin.OUT),
'23': Pin(26, Pin.OUT),
'87': Pin(22, Pin.OUT)
}
# Input string
input_string = "45,99,23,87"
# Split the string into individual numbers
numbers = input_string.split(',')
# Turn on the corresponding LEDs
for number in numbers:
if number in led_pins:
led_pins[number].on()
# Keep the LEDs on for a while
time.sleep(5)
# Turn off the LEDs
for number in numbers:
if number in led_pins:
led_pins[number].off()