# Import necessary modules from MicroPython
from machine import Pin
import time
# Define the GPIO pins for the 8 LEDs
# These are arranged in the order they will be cycled through.
# Using the new LED pins provided by the user.
LED_PINS = [2, 4, 5, 16, 17, 18, 21, 27] # Updated LED pins as per user's latest input
NUM_LEDS = len(LED_PINS) # Calculate number of LEDs
# Define the GPIO pins for the buttons
GREEN_BUTTON_PIN = 34 # Entrance Button
RED_BUTTON_PIN = 35 # Exit Button
# GPIO 36 and 39 are also provided by the user, but not assigned specific functionality in this code.
# BUTTON_3_PIN = 36
# BUTTON_4_PIN = 39
# --- Counter Variable ---
# This counter will keep track of how many LEDs are currently lit (representing cars).
# Its value will range from 0 (all off) to NUM_LEDS (8, all on).
current_car_count = 0 # Starts at 0, meaning no LEDs are currently lit (no cars in lot)
# --- Button Debouncing Variables ---
# Debouncing helps prevent multiple rapid readings from a single button press.
DEBOUNCE_DELAY_MS = 50 # milliseconds
# Convert debounce delay to ticks for time.ticks_diff()
# time.ticks_ms() returns a value that wraps around, so time.ticks_diff() is safer for comparisons
DEBOUNCE_DELAY_TICKS = DEBOUNCE_DELAY_MS
# Variables for the entrance button (Green Button)
entrance_button_state = 1 # 1 for HIGH (released), 0 for LOW (pressed)
last_entrance_button_state = 1
last_entrance_debounce_time = 0
# Variables for the exit button (Red Button)
exit_button_state = 1 # 1 for HIGH (released), 0 for LOW (pressed)
last_exit_button_state = 1
last_exit_debounce_time = 0
# --- Pin Objects ---
# Create Pin objects for LEDs
# Initialize all LEDs to OFF
led_objects = []
for pin_num in LED_PINS:
led = Pin(pin_num, Pin.OUT) # Set pin as an output
led.value(0) # Turn off LED initially (0 for LOW)
led_objects.append(led)
# Create Pin objects for buttons
# Pin.IN sets it as an input, Pin.PULL_UP enables the internal pull-up resistor.
# This means the button will read HIGH when released and LOW when pressed (connected to GND).
entrance_button = Pin(GREEN_BUTTON_PIN, Pin.IN, Pin.PULL_UP)
exit_button = Pin(RED_BUTTON_PIN, Pin.IN, Pin.PULL_UP)
# --- Helper Function to Update LEDs ---
# This function updates the state of all LEDs based on the current_car_count
def update_leds(count):
for i in range(NUM_LEDS): # Iterate through all available LED positions
if i < count:
led_objects[i].value(1) # Turn on the LED (1 for HIGH)
else:
led_objects[i].value(0) # Turn off the LED (0 for LOW)
# --- Setup equivalent ---
# In MicroPython, setup tasks are usually placed directly at the top level
# of the script or within a main function that is called at the end.
print("ESP32 LED Control for Car Parking (MicroPython)...")
print(f"Initial Car Count: {current_car_count}")
update_leds(current_car_count) # Ensure LEDs reflect initial count
# --- Main loop equivalent ---
# MicroPython scripts typically run in a continuous loop.
while True:
# Read the raw value of the entrance button pin
reading_entrance = entrance_button.value()
# --- Entrance Button Logic (Green Button) ---
# Check for state change to start debounce timer
if reading_entrance != last_entrance_button_state:
last_entrance_debounce_time = time.ticks_ms()
# If the button state has been stable for longer than the debounce delay
if time.ticks_diff(time.ticks_ms(), last_entrance_debounce_time) > DEBOUNCE_DELAY_TICKS:
# Only act if the button state has actually changed and is now pressed (0 for LOW)
if reading_entrance != entrance_button_state:
entrance_button_state = reading_entrance
if entrance_button_state == 0: # Entrance Button is pressed (LOW)
# Check if there's space for another car (counter less than max)
if current_car_count < NUM_LEDS: # NUM_LEDS is 8, so count can go up to 7 (for 8 LEDs)
current_car_count += 1 # Increment the car count
update_leds(current_car_count) # Update LEDs to reflect new 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 # Save the reading for the next iteration
# Read the raw value of the exit button pin
reading_exit = exit_button.value()
# --- Exit Button Logic (Red Button) ---
# Check for state change to start debounce timer
if reading_exit != last_exit_button_state:
last_exit_debounce_time = time.ticks_ms()
# If the button state has been stable for longer than the debounce delay
if time.ticks_diff(time.ticks_ms(), last_exit_debounce_time) > DEBOUNCE_DELAY_TICKS:
# Only act if the button state has actually changed and is now pressed (0 for LOW)
if reading_exit != exit_button_state:
exit_button_state = reading_exit
if exit_button_state == 0: # Exit Button is pressed (LOW)
# Check if there are cars to exit (counter greater than 0)
if current_car_count > 0:
current_car_count -= 1 # Decrement the car count
update_leds(current_car_count) # Update LEDs to reflect new 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 # Save the reading for the next iteration
# A small delay to keep the loop from running too fast, though debounce helps with this
time.sleep_ms(10)