from machine import Pin
import time
entrance_btn = Pin(34, Pin.IN, Pin.PULL_UP) # Entrance pushbutton
exit_btn = Pin(35, Pin.IN, Pin.PULL_UP) # Exit pushbutton
# Setup 8 LEDs to indicate number of cars inside
leds = [2, 4, 5, 13, 14, 16, 17, 18]
lights = []
for pin in leds:
lights.append(Pin(pin, Pin.OUT))
count_num = 0
max_capacity = 8
prev_entrance = 1
prev_exit = 1
# Function to update the LEDs based on car count
def light_leds(count):
for i in range(max_capacity): # Loop through all LEDs up to max_capacity
if i < count:
lights[i].value(1) # Turn LED ON
else:
lights[i].value(0) # Turn LED OFF
# Main loop
print("System is activated.")
print("Initial Count:", count_num)
light_leds(count_num)
while True:
entrance_state = entrance_btn.value()
exit_state = exit_btn.value()
if entrance_state == 0 and prev_entrance == 1:
if count_num < max_capacity:
count_num += 1
print("Car In. Count:", count_num)
light_leds(count_num)
else:
print("Full Capacity! Cannot enter.")
time.sleep(0.1) # Small debounce delay *after* detecting a press
# Check if exit button is pressed (falling edge: from high to low)
if exit_state == 0 and prev_exit == 1:
if count_num > 0:
count_num -= 1
print("Car Out. Count:", count_num)
light_leds(count_num)
else:
print("No cars inside. Cannot exit.")
time.sleep(0.1) # Small debounce delay *after* detecting a press
# Save current button states for next loop iteration
prev_entrance = entrance_state
prev_exit = exit_state
time.sleep(0.05) # Main polling delay for the loop