from machine import Pin
import time
CAPACITY = 15 # maximum number of cars the garage can hold
# ----- INPUT: push buttons (internal pull-up, so a press reads 0) -----
btn_in = Pin(14, Pin.IN, Pin.PULL_UP) # increment: a car enters
btn_out = Pin(27, Pin.IN, Pin.PULL_UP) # decrement: a car exits
# ----- OUTPUT: the four LEDs -----
led_green = Pin(2, Pin.OUT) # space still available
led_red = Pin(4, Pin.OUT) # garage full
led_yellow = Pin(5, Pin.OUT) # a car entered (event pulse)
led_blue = Pin(18, Pin.OUT) # a car exited (event pulse)
count = 0 # number of cars currently inside the garage
def update_status_leds():
# GREEN and RED are STATE LEDs: exactly one of them is on at any moment.
if count >= CAPACITY:
led_green.value(0) # no free space
led_red.value(1) # garage is full
else:
led_green.value(1) # at least one free space
led_red.value(0)
def blink(led):
# YELLOW and BLUE are EVENT LEDs: a short pulse to mark an action.
led.value(1)
time.sleep_ms(300)
led.value(0)
# Start-up state: the garage is empty, so GREEN is on.
update_status_leds()
prev_in = 1 # remember the previous reading to detect a new press
prev_out = 1
while True:
cur_in = btn_in.value()
cur_out = btn_out.value()
# ---- a car ENTERS: falling edge (1 -> 0) on the increment button ----
if prev_in == 1 and cur_in == 0:
if count < CAPACITY:
count += 1
print("Car entered. Count =", count)
blink(led_yellow) # yellow pulse for one entry
update_status_leds() # refresh green / red
else:
print("Garage FULL. Entry ignored. Count =", count)
time.sleep_ms(50) # simple debounce
# ---- a car EXITS: falling edge (1 -> 0) on the decrement button ----
if prev_out == 1 and cur_out == 0:
if count > 0:
count -= 1
print("Car exited. Count =", count)
blink(led_blue) # blue pulse for one exit
update_status_leds() # refresh green / red
else:
print("Garage EMPTY. Exit ignored. Count =", count)
time.sleep_ms(50) # simple debounce
prev_in = cur_in
prev_out = cur_out
time.sleep_ms(10)