from machine import Pin
import time
LED_PINS = [2, 4, 5, 13, 14, 16, 17, 18]
NUM_LEDS = len(LED_PINS)
GREEN_BUTTON_PIN = 35 # Entrance Button
RED_BUTTON_PIN = 34 # Exit Button
current_car_count = 0
DEBOUNCE_DELAY_MS = 30
DEBOUNCE_DELAY_TICKS = DEBOUNCE_DELAY_MS
entrance_button_state = 1
last_entrance_button_state = 1
last_entrance_debounce_time = 0
exit_button_state = 1
last_exit_button_state = 1
last_exit_debounce_time = 0
led_objects = []
for pin_num in LED_PINS:
led = Pin(pin_num, Pin.OUT)
led.value(0)
led_objects.append(led)
entrance_button = Pin(GREEN_BUTTON_PIN, Pin.IN, Pin.PULL_UP)
exit_button = Pin(RED_BUTTON_PIN, Pin.IN, Pin.PULL_UP)
def update_leds(count):
for i in range(NUM_LEDS):
if i < count:
led_objects[i].value(1)
else:
led_objects[i].value(0)
print("ESP32 LED Control for Car Parking (MicroPython)...")
print(f"Initial Car Count: {current_car_count}")
update_leds(current_car_count)
while True:
reading_entrance = entrance_button.value()
if reading_entrance != last_entrance_button_state:
last_entrance_debounce_time = time.ticks_ms()
if time.ticks_diff(time.ticks_ms(), last_entrance_debounce_time) > DEBOUNCE_DELAY_TICKS:
if reading_entrance != entrance_button_state:
entrance_button_state = reading_entrance
if entrance_button_state == 0:
if current_car_count < NUM_LEDS:
current_car_count += 1
update_leds(current_car_count)
print(f"Car entered. Current Car Count: {current_car_count}")
else:
print(f"Parking full! Current Car Count: {current_car_count}")
last_entrance_button_state = reading_entrance
reading_exit = exit_button.value()
if reading_exit != last_exit_button_state:
last_exit_debounce_time = time.ticks_ms()
if time.ticks_diff(time.ticks_ms(), last_exit_debounce_time) > DEBOUNCE_DELAY_TICKS:
if reading_exit != exit_button_state:
exit_button_state = reading_exit
if exit_button_state == 0:
if current_car_count > 0:
current_car_count -= 1
update_leds(current_car_count)
print(f"Car exited. Current Car Count: {current_car_count}")
else:
print(f"Parking empty! Current Car Count: {current_car_count}")
last_exit_button_state = reading_exit
time.sleep_ms(10)