from machine import Pin
import time

# LED pins
led_pins = [2, 4, 5, 13, 14, 16, 17, 18]
leds = [Pin(pin, Pin.OUT) for pin in led_pins]

# Button pins
entrance_button = Pin(34, Pin.IN)
exit_button = Pin(35, Pin.IN)

car_count = 0
MAX_CARS = 8

# Previous button states (for edge detection)
prev_entrance_state = 0
prev_exit_state = 0

# Function to update LEDs
def update_leds():
    for i in range(8):
        leds[i].value(1 if i < car_count else 0)

# Main loop
while True:
    # Read current states
    entrance_state = entrance_button.value()
    exit_state = exit_button.value()

    # Entrance button pressed (edge detection)
    if entrance_state == 1 and prev_entrance_state == 0 and car_count < MAX_CARS:
        car_count += 1
        update_leds()
        time.sleep(0.1)  # short debounce

    # Exit button pressed (edge detection)
    if exit_state == 1 and prev_exit_state == 0 and car_count > 0:
        car_count -= 1
        update_leds()
        time.sleep(0.1)  # short debounce

    # Update previous states
    prev_entrance_state = entrance_state
    prev_exit_state = exit_state

    time.sleep(0.05)  # small loop delay