import time
import board
import digitalio
import busio
import lcd
import i2c_pcf8574_interface
# Durasi setiap lampu (dalam saat)
RED_TIME = 10
YELLOW_TIME = 3
GREEN_TIME = 10
CROSSING_TIME = 8
# LCD setup
I2C_ADDR = 0x27
I2C_NUM_ROWS = 2
I2C_NUM_COLS = 16
# Initialize I2C and LCD
i2c = busio.I2C(scl=board.GP5, sda=board.GP4)
address = 0x27
interface = i2c_pcf8574_interface.I2CPCF8574Interface(i2c, address)
display = lcd.LCD(interface, num_rows=2, num_cols=16)
display.set_backlight(True)
display.set_display_enabled(True)
# Setup LEDs
green = digitalio.DigitalInOut(board.GP19)
yellow = digitalio.DigitalInOut(board.GP20)
red = digitalio.DigitalInOut(board.GP21)
for led in [green, yellow, red]:
led.direction = digitalio.Direction.OUTPUT
# Setup pedestrian button SW1
button = digitalio.DigitalInOut(board.GP15)
button.direction = digitalio.Direction.INPUT
button.pull = digitalio.Pull.UP
def reset_lights():
red.value = False
yellow.value = False
green.value = False
def display_timer(label, duration):
for t in range(duration, 0, -1):
display.clear()
display.print(f"{label}: {t}s")
time.sleep(1)
def wait_for_button_press():
display.clear()
display.print("Press SW1 to \nstart")
while button.value:
time.sleep(0.1)
display.clear()
display.print("Request Received")
time.sleep(1)
def run_sequence():
# RED
reset_lights()
red.value = True
display_timer("RED", RED_TIME)
# YELLOW
reset_lights()
yellow.value = True
display_timer("YELLOW", YELLOW_TIME)
# GREEN
reset_lights()
green.value = True
display_timer("GREEN", GREEN_TIME)
# CROSSING
reset_lights()
display_timer("CROSSING", CROSSING_TIME)
# End
display.clear()
display.print("Cycle Complete")
time.sleep(2)
# Start program
while True:
wait_for_button_press()
run_sequence()