from machine import Pin
import time
# Define LED GPIO pins
led_pins = [2, 4, 5, 13, 14, 16, 17, 18]
leds = [Pin(pin, Pin.OUT) for pin in led_pins]
# Define entrance and exit buttons
entrance_button = Pin(34, Pin.IN, Pin.PULL_UP)
exit_button = Pin(35, Pin.IN, Pin.PULL_UP)
# Variables
car_count = 0 # Number of cars currently inside (0 to 8)
max_cars = 8
# Track previous button states for edge detection
prev_entrance_state = 1
prev_exit_state = 1
def update_leds():
"""Update LEDs based on current car count."""
for i in range(len(leds)):
leds[i].value(1 if i < car_count else 0)
def turn_off_all_leds():
"""Ensure all LEDs are off initially."""
for led in leds:
led.value(0)
# Initialization
print("Smart Car Monitoring System Started")
turn_off_all_leds()
# Main loop
while True:
# Read current button states
curr_entrance_state = entrance_button.value()
curr_exit_state = exit_button.value()
# Detect rising edge for entrance button (button press)
if prev_entrance_state == 1 and curr_entrance_state == 0:
if car_count < max_cars:
car_count += 1
print(f"Car entered. Current count: {car_count}")
update_leds()
else:
print("Max capacity reached. No more cars allowed.")
time.sleep(0.2) # Debounce delay
# Detect rising edge for exit button (button press)
if prev_exit_state == 1 and curr_exit_state == 0:
if car_count > 0:
car_count -= 1
print(f"Car exited. Current count: {car_count}")
update_leds()
else:
print("No cars to exit.")
time.sleep(0.2) # Debounce delay
# Update previous states
prev_entrance_state = curr_entrance_state
prev_exit_state = curr_exit_state
time.sleep(0.01) # Loop delay