print("Hello, ESP32!")
from machine import Pin
import time
# Pin Definitions
BTN_INC_PIN = 12
BTN_DEC_PIN = 14
LED_GREEN_PIN = 18
LED_RED_PIN = 19
LED_YELLOW_PIN = 21
LED_BLUE_PIN = 22
# Setup Inputs
btn_inc = Pin(BTN_INC_PIN, Pin.IN, Pin.PULL_DOWN)
btn_dec = Pin(BTN_DEC_PIN, Pin.IN, Pin.PULL_DOWN)
# Setup Outputs
led_green = Pin(LED_GREEN_PIN, Pin.OUT)
led_red = Pin(LED_RED_PIN, Pin.OUT)
led_yellow = Pin(LED_YELLOW_PIN, Pin.OUT)
led_blue = Pin(LED_BLUE_PIN, Pin.OUT)
# System Variables
count = 0
MAX_CAPACITY = 15
def update_state_leds():
if count < MAX_CAPACITY:
led_green.value(1)
led_red.value(0)
else:
led_green.value(0)
led_red.value(1)
# Initialize System State
update_state_leds()
print(f"System Ready. Initial Count: {count}")
last_inc = 0
last_dec = 0
while True:
curr_inc = btn_inc.value()
curr_dec = btn_dec.value()
# Entry Logic
if curr_inc == 1 and last_inc == 0:
if count < MAX_CAPACITY:
count += 1
print(f"Car Entered. Total: {count}")
update_state_leds()
led_yellow.value(1)
time.sleep(0.2)
led_yellow.value(0)
else:
print("Garage Full! Entry Request Ignored.")
time.sleep(0.1)
# Exit Logic
if curr_dec == 1 and last_dec == 0:
if count > 0:
count -= 1
print(f"Car Exited. Total: {count}")
update_state_leds()
led_blue.value(1)
time.sleep(0.2)
led_blue.value(0)
else:
print("Garage Empty! Exit Request Ignored.")
time.sleep(0.1)
last_inc = curr_inc
last_dec = curr_dec
time.sleep(0.02)