from machine import Pin
import utime
# Define pin numbers for LED segments
led_pins = [9, 8, 7, 6, 5, 4, 3, 2, 1, 0] # LED segments for floors 0 to 9
# Initialize LEDs
leds = [Pin(pin, Pin.OUT) for pin in led_pins]
# Define rows and columns for keypad
row_pins = [Pin(21, Pin.IN, Pin.PULL_UP), Pin(20, Pin.IN, Pin.PULL_UP), Pin(19, Pin.IN, Pin.PULL_UP), Pin(18, Pin.IN, Pin.PULL_UP)]
col_pins = [Pin(15, Pin.OUT), Pin(14, Pin.OUT), Pin(13, Pin.OUT), Pin(12, Pin.OUT)]
# Keypad setup
keys = [
["1", "2", "3", "A"],
["4", "5", "6", "B"],
["7", "8", "9", "C"],
["*", "0", "#", "D"]
]
current_floor = 0
def scan_keypad():
for col_num, col_pin in enumerate(col_pins):
col_pin.low()
for row_num, row_pin in enumerate(row_pins):
if not row_pin.value():
utime.sleep(0.1) # Debounce delay
while not row_pin.value():
pass # Wait for key release
col_pin.high()
key = keys[row_num][col_num]
print(f"Key Pressed: {key}")
return key
col_pin.high()
return None
def move_to_floor(floor):
global current_floor
if floor != current_floor:
# Determine the direction of movement
direction = 1 if floor > current_floor else -1
# Loop through floors in the direction of movement
for f in range(current_floor + direction, floor + direction, direction):
# Turn off the LED of the current floor
utime.sleep(2)
leds[current_floor].value(0)
# Turn on the LED of the next floor
leds[f].value(1)
print(f"Passing floor {f}")
# Pause to simulate elevator movement between floors
# Update current floor
current_floor = f
# Turn off the LED of the last passed floor
leds[current_floor].value(0)
# Turn on the LED of the destination floor
leds[floor].value(1)
print(f"Arrived at floor {floor}")
# Update current floor
current_floor = floor
# Turn on the LED of the destination floor
leds[floor].value(1)
print(f"Arrived at floor {floor}")
# Update current floor
current_floor = floor
# Initialize display with starting floor
leds[current_floor].value(1)
print(f"Starting at floor {current_floor}")
while True:
key = scan_keypad()
if key is not None:
if key.isdigit():
floor = int(key) # Convert key to floor index (0-9)
if 0 <= floor <= 9:
move_to_floor(floor)
else:
print(f"Invalid key: {key}")
utime.sleep(0.1) # Small delay to avoid excessive CPU usage
Loading
pi-pico-w
pi-pico-w