from machine import Pin
from time import sleep
# Setup LEDs on GPIO pins 2 through 9
led_pins = [Pin(pin_num, Pin.OUT) for pin_num in range(2, 10)]
# Setup entrance and exit buttons with internal pull-down resistors
entrance_button = Pin(14, Pin.IN, Pin.PULL_DOWN) # Entrance button on GPIO14
exit_button = Pin(27, Pin.IN, Pin.PULL_DOWN) # Exit button on GPIO27
car_count = 0 # Current number of cars inside
def update_leds(count):
# Turn on LEDs equal to the count, turn off the rest
for i, led in enumerate(led_pins):
led.value(1 if i < count else 0)
def wait_for_release(pin):
# Wait until the button is released to avoid multiple counts on one press
while pin.value() == 1:
sleep(0.01)
while True:
if entrance_button.value() == 1:
if car_count < 8:
car_count += 1
update_leds(car_count)
wait_for_release(entrance_button)
if exit_button.value() == 1:
if car_count > 0:
car_count -= 1
update_leds(car_count)
wait_for_release(exit_button)
sleep(0.05)