# Smart Parking System - Wokwi ESP32 (MicroPython)
# -------------------------------------------------
# Features:
# - uasyncio RTOS-like tasks
# - Simulated moving vehicles (entry + exit lanes)
# - Parking slot system
# - Servo gate
# - LED status
# - Real-time simulation of approach / departure
import uasyncio as asyncio
from machine import Pin, PWM
import random
# ---------------- CONFIG ----------------
CAPACITY = 3
ENTRY_TRIG_DIST = 20
EXIT_TRIG_DIST = 20
# ---------------- HARDWARE ----------------
servo = PWM(Pin(13), freq=50)
servo.duty(115)
led_g = Pin(26, Pin.OUT)
led_r = Pin(27, Pin.OUT)
led_entry = PWM(Pin(32), freq=1000)
led_exit = PWM(Pin(22), freq=1000)
# ---------------- STATE ----------------
slots = 0
# ---------------- SIMULATION STATE ----------------
entry_vehicle_dist = 999
exit_vehicle_dist = 999
entry_active = False
exit_active = False
# ---------------- SERVO ----------------
gate_state = "CLOSED"
def set_gate(state):
global gate_state
gate_state = state
if state == "OPEN":
servo.duty(40)
else:
servo.duty(115)
# ---------------- VEHICLE SIMULATION ----------------
async def vehicle_sim_task():
global entry_vehicle_dist, exit_vehicle_dist, entry_active, exit_active
while True:
# spawn entry vehicle randomly
if not entry_active and random.random() < 0.2:
entry_active = True
entry_vehicle_dist = 80
# move entry vehicle
if entry_active:
entry_vehicle_dist -= 10
if entry_vehicle_dist <= 5:
entry_vehicle_dist = 5
# spawn exit vehicle randomly
if not exit_active and random.random() < 0.1:
exit_active = True
exit_vehicle_dist = 80
# move exit vehicle
if exit_active:
exit_vehicle_dist -= 12
if exit_vehicle_dist <= 5:
exit_vehicle_dist = 5
await asyncio.sleep_ms(300)
# ---------------- ENTRY TASK ----------------
async def entry_task():
global slots, entry_active, entry_vehicle_dist
while True:
if entry_vehicle_dist < ENTRY_TRIG_DIST and slots < CAPACITY:
slots += 1
set_gate("OPEN")
await asyncio.sleep(1)
set_gate("CLOSED")
entry_active = False
entry_vehicle_dist = 999
await asyncio.sleep_ms(100)
# ---------------- EXIT TASK ----------------
async def exit_task():
global slots, exit_active, exit_vehicle_dist
while True:
if (exit_vehicle_dist < EXIT_TRIG_DIST) and slots > 0:
slots -= 1
set_gate("OPEN")
await asyncio.sleep(1)
set_gate("CLOSED")
exit_active = False
exit_vehicle_dist = 999
await asyncio.sleep_ms(150)
# ---------------- UI TASKS ----------------
async def ui_task():
while True:
if slots >= CAPACITY:
led_r.value(1)
led_g.value(0)
else:
led_r.value(0)
led_g.value(1)
print("Slots:", slots,
"EntryDist:", entry_vehicle_dist,
"ExitDist:", exit_vehicle_dist)
await asyncio.sleep_ms(200)
async def led_entry_task():
global entry_vehicle_dist
while True:
brightness = max(1, 1023 - (entry_vehicle_dist * 12))
led_entry.duty(brightness)
await asyncio.sleep_ms(50)
async def led_exit_task():
global exit_vehicle_dist
while True:
brightness = max(1, 1023 - (exit_vehicle_dist * 12))
led_exit.duty(brightness)
await asyncio.sleep_ms(50)
# ---------------- MAIN ----------------
async def main():
asyncio.create_task(vehicle_sim_task())
asyncio.create_task(entry_task())
asyncio.create_task(exit_task())
asyncio.create_task(ui_task())
asyncio.create_task(led_entry_task())
asyncio.create_task(led_exit_task())
while True:
await asyncio.sleep(1)
asyncio.run(main())