print("Hello, ESP32!")
import machine
import time
# Define LED pins
LED_PINS = [
machine.Pin(2, machine.Pin.OUT), # LED 1
machine.Pin(4, machine.Pin.OUT), # LED 2
machine.Pin(5, machine.Pin.OUT), # LED 3
machine.Pin(13, machine.Pin.OUT), # LED 4
machine.Pin(14, machine.Pin.OUT), # LED 5
machine.Pin(16, machine.Pin.OUT), # LED 6
machine.Pin(17, machine.Pin.OUT), # LED 7
machine.Pin(18, machine.Pin.OUT) # LED 8
]
# Define buttons
ENTRANCE_BUTTON = machine.Pin(34, machine.Pin.IN, machine.Pin.PULL_UP)
EXIT_BUTTON = machine.Pin(35, machine.Pin.IN, machine.Pin.PULL_UP)
# Variables
car_count = 0
last_entrance_press = 0
last_exit_press = 0
DEBOUNCE_DELAY = 200 # milliseconds
# Function to update LEDs
def update_leds(count):
for i in range(8):
if i < count:
LED_PINS[i].on()
else:
LED_PINS[i].off()
print("System Ready. Initial car count: 0")
update_leds(car_count)
# Main loop
while True:
now = time.ticks_ms()
# Entrance button press
if ENTRANCE_BUTTON.value() == 0 and time.ticks_diff(now, last_entrance_press) > DEBOUNCE_DELAY:
if car_count < 8:
car_count += 1
print(f"Car entered. Count: {car_count}")
update_leds(car_count)
else:
print("Garage full (8 cars).")
last_entrance_press = now
# Exit button press
if EXIT_BUTTON.value() == 0 and time.ticks_diff(now, last_exit_press) > DEBOUNCE_DELAY:
if car_count > 0:
car_count -= 1
print(f"Car exited. Count: {car_count}")
update_leds(car_count)
else:
print("No cars inside.")
last_exit_press = now
time.sleep_ms(50)