print("Hello, ESP32!")
from machine import Pin
from time import sleep
# =========================
# Buttons
# =========================
enter_button = Pin(18, Pin.IN, Pin.PULL_UP)
exit_button = Pin(19, Pin.IN, Pin.PULL_UP)
# =========================
# LEDs
# =========================
green = Pin(13, Pin.OUT)
red = Pin(26, Pin.OUT)
yellow = Pin(25, Pin.OUT)
blue = Pin(33, Pin.OUT)
# =========================
# Parking data
# =========================
cars = 0
MAX_CARS = 15
# =========================
# Initial LED state
# =========================
green.on()
red.off()
yellow.off()
blue.off()
# =========================
# Update state LEDs
# =========================
def update_state_leds():
if cars < MAX_CARS:
green.on()
red.off()
else:
green.off()
red.on()
# =========================
# Main program
# =========================
while True:
# Update garage state
update_state_leds()
# -------------------------
# Car Entry
# -------------------------
if enter_button.value() == 0:
if cars < MAX_CARS:
cars += 1
# Accepted entry event
yellow.on()
sleep(0.2)
yellow.off()
print("Car entered.")
print("Cars:", cars)
# Update state after entry
update_state_leds()
# Ignore entry when garage is full
while enter_button.value() == 0:
sleep(0.05)
# -------------------------
# Car Exit
# -------------------------
if exit_button.value() == 0:
if cars > 0:
cars -= 1
# Accepted exit event
blue.on()
sleep(0.2)
blue.off()
print("Car exited.")
print("Cars:", cars)
# Update state after exit
update_state_leds()
# Ignore exit when garage is empty
while exit_button.value() == 0:
sleep(0.05)
sleep(0.05)