# main.py for Wokwi ESP32 Simulation - SIMPLIFIED VERSION
from machine import Pin
import time
#GPIO Pin Definitions
# Define pins for the 8 LEDs
# Using a contiguous range for simplicity in this basic example
# Make sure these pins are correctly wired in your Wokwi diagram!
led_pins = [2, 4, 5, 13, 14, 16, 17, 18] # Example: Using 8 pins from the recommended list
# Define pins for the Entrance and Exit buttons
entrance_button_pin = 34
exit_button_pin = 35
# Initialize Hardware Components
# Initialize LED pins as output
leds = []
for pin_num in led_pins:
led = Pin(pin_num, Pin.OUT)
leds.append(led)
# Initialize button pins as input with a pull-up resistor
# Pin.PULL_UP means the pin reads HIGH when idle and LOW when pressed (connected to GND)
entrance_button = Pin(entrance_button_pin, Pin.IN, Pin.PULL_UP)
exit_button = Pin(exit_button_pin, Pin.IN, Pin.PULL_UP)
# Global Variables
car_count = 0 # Current number of cars inside the office
MAX_CARS = 8 # Maximum allowed cars
MIN_CARS = 0 # Minimum allowed cars
# Function to update LEDs
def update_leds():
"""
Updates the state of the LEDs based on the current car_count.
"""
for i in range(MAX_CARS):
if i < car_count:
leds[i].value(1) # Turn LED ON
else:
leds[i].value(0) # Turn LED OFF
# Main Loop
print("Simplified Monitoring System Started. Ready for button presses.")
update_leds() # Ensure all LEDs are off initially
# Keep track of the last button state to detect presses (simple debounce)
last_entrance_state = entrance_button.value()
last_exit_state = exit_button.value()
while True:
current_entrance_state = entrance_button.value()
current_exit_state = exit_button.value()
# Check for Entrance Button Press (button goes from HIGH to LOW)
if current_entrance_state == 0 and last_entrance_state == 1:
if car_count < MAX_CARS:
car_count += 1
print(f"Car entered. Current cars: {car_count}")
update_leds()
else:
print("Office is full. Cannot enter.")
# Check for Exit Button Press (button goes from HIGH to LOW)
if current_exit_state == 0 and last_exit_state == 1:
if car_count > MIN_CARS:
car_count -= 1
print(f"Car exited. Current cars: {car_count}")
update_leds()
else:
print("Office is empty. Cannot exit.")
# Update last states for next iteration
last_entrance_state = current_entrance_state
last_exit_state = current_exit_state
time.sleep(0.05) # A small delay to save CPU cycles and for basic debounce