from machine import Pin
from time import sleep
yellow=Pin(23,Pin.OUT)
red=Pin(22,Pin.OUT)
green=Pin(21,Pin.OUT)
blue=Pin(17,Pin.OUT)
yellowBtn=Pin(26,Pin.IN, Pin.PULL_DOWN)
blueBtn=Pin(12,Pin.IN, Pin.PULL_DOWN)
maxi= 15
mini= 0
occupied_spaces = 0
yellow_last_state= 0
blue_last_state = 0
green.on()
while True:
yellow_current_state= yellowBtn.value()
blue_current_state= blueBtn.value()
if yellow_current_state == 1 and yellow_last_state == 0:
sleep(0.2)
if occupied_spaces < maxi :
print("There is a space")
yellow.on()
sleep(0.5)
yellow.off()
print("A car has just entered")
occupied_spaces += 1
if occupied_spaces == maxi:
print(f"There is no spaces left")
print("Garage is occupied")
green.off()
red.on()
else:
print(f"There is {maxi - occupied_spaces} spaces left")
yellow_last_state=yellow_current_state
if blue_current_state == 1 and blue_last_state == 0:
sleep(0.2)
if occupied_spaces > mini:
print("A car has just left")
red.off()
green.on()
sleep(0.2)
blue.on()
sleep(0.5)
blue.off()
occupied_spaces += -1
if occupied_spaces == mini:
print("Garage is empty")
else:
print(f"There is {maxi - occupied_spaces} space left")
blue_last_state =blue_current_state
sleep(0.3)