# P3H Traffic Light solution Template
from machine import Pin
#import utime
import time
time.sleep(0.1) # Wait for USB to become ready
# Button-controlled LED example
from machine import Pin, PWM
#import utime
import time
time.sleep(0.1) # Wait for USB to become ready
#Set up the Traffic lights LEDs (separate LEDs)
Green_led = Pin(15, Pin.OUT)
Red_led = Pin(14, Pin.OUT)
Yellow_led = Pin(7, Pin.OUT)
# Set up RGB LED pinsfor pedestrian signal
RGB_red_pin = 12
RGB_green_pin = 4
RGB_blue_pin = 5
# RGB LED pins (using PWM for color control)
RGB_red = PWM(Pin(RGB_red_pin))
RGB_green = PWM(Pin(RGB_green_pin))
RGB_blue = PWM(Pin(RGB_blue_pin))
# Set PWM frequency (1000 Hz is good for LEDs)
RGB_red.freq(1000)
RGB_green.freq(1000)
RGB_blue.freq(1000)
# Set up the button on GP16 with an internal pull-up resistor
button = Pin(16, Pin.IN, Pin.PULL_UP)
## Timing variables (in milliseconds)
GREEN_TIME = 5000 # Green light stays on for 5 seconds
previous_time = time.ticks_ms()
# Print startup message
print("Smart Traffic Light System Starting...")
print("Press button during green light to trigger pedestrian cycle")
# Initialize - start with green light ON, everything else OFF
Green_led.value(1)
# Convert 0-255 values to 0-65535 for duty_u16 (16-bit PWM)
RGB_red.duty_u16(int(255 * 65535 / 255))
RGB_green.duty_u16(int(165 * 65535 / 255))
RGB_blue.duty_u16(int(0 * 65535 / 255))
# Main loop runs continuously
while True:
# Read the button state
button_state = button.value()
print (f"Button state :{button_state}")
# With a pull-up resistor, button pressed = 0 (LOW)
# Get current time
current_time = time.ticks_ms()
# Calculate how much time has passed since last cycle
elapsed_time = time.ticks_diff(current_time, previous_time)
# Print status for debugging
print("Time:", elapsed_time, " Button:", button_state)
# Check if button was pressed OR if green time has expired
if button_state == 0 or elapsed_time >= GREEN_TIME:
# ========== YELLOW LIGHT ==========
# Turn off green, turn on yellow
Green_led.value(0)
Yellow_led.value(1)
time.sleep_ms(2500) # Yellow for 2.5 seconds
# ========== RED LIGHT ==========
# Turn off yellow, turn on red
Green_led.value(0)
Yellow_led.value(0)
Red_led.value(1)
# ========== PEDESTRIAN WALK SIGNAL (WHITE) ==========
# Turn on all RGB colors to make white for 4 seconds
RGB_red.duty_u16(int(255 * 65535 / 255))
RGB_green.duty_u16(int(255 * 65535 / 255))
RGB_blue.duty_u16(int(255 * 65535 / 255))
time.sleep_ms(4000)
# ========== FLASHING ORANGE WARNING ==========
# Flash orange 8 times (2 seconds total: 250ms on, 250ms off)
# Flash 1
RGB_red.duty_u16(int(255 * 65535 / 255))
RGB_green.duty_u16(int(100 * 65535 / 255)) # Much less green
RGB_blue.duty_u16(int(0 * 65535 / 255))
time.sleep_ms(250)
RGB_red.duty_u16(int(0 * 65535 / 255))
RGB_green.duty_u16(int(0 * 65535 / 255))
RGB_blue.duty_u16(int(0 * 65535 / 255))
time.sleep_ms(250)
# Flash 2
RGB_red.duty_u16(int(255 * 65535 / 255))
RGB_green.duty_u16(int(100 * 65535 / 255)) # Much less green
RGB_blue.duty_u16(int(0 * 65535 / 255))
time.sleep_ms(250)
RGB_red.duty_u16(int(0 * 65535 / 255))
RGB_green.duty_u16(int(0 * 65535 / 255))
RGB_blue.duty_u16(int(0 * 65535 / 255))
time.sleep_ms(250)
# Flash 3
RGB_red.duty_u16(int(255 * 65535 / 255))
RGB_green.duty_u16(int(100 * 65535 / 255)) # Much less green
RGB_blue.duty_u16(int(0 * 65535 / 255))
time.sleep_ms(250)
RGB_red.duty_u16(int(0 * 65535 / 255))
RGB_green.duty_u16(int(0 * 65535 / 255))
RGB_blue.duty_u16(int(0 * 65535 / 255))
time.sleep_ms(250)
# Flash 4
RGB_red.duty_u16(int(255 * 65535 / 255))
RGB_green.duty_u16(int(100 * 65535 / 255)) # Much less green
RGB_blue.duty_u16(int(0 * 65535 / 255))
time.sleep_ms(250)
RGB_red.duty_u16(int(0 * 65535 / 255))
RGB_green.duty_u16(int(0 * 65535 / 255))
RGB_blue.duty_u16(int(0 * 65535 / 255))
time.sleep_ms(250)
# ========== SOLID ORANGE DON'T WALK ==========
# Orange ON (stays on)
RGB_red.duty_u16(int(255 * 65535 / 255))
RGB_green.duty_u16(int(100 * 65535 / 255)) # Much less green
RGB_blue.duty_u16(int(0 * 65535 / 255))
# Red light stays on for remaining time
# Total red time should be 7.5 seconds
# We've used 6 seconds (4s white + 2s flashing)
# So wait 1.5 more seconds
time.sleep_ms(1500)
# ========== RETURN TO GREEN ==========
# Turn off red light
Red_led(0)
Yellow_led(0)
# Turn off yellow, turn on red
Green_led(1)
# Reset the timer for next cycle
previous_time = time.ticks_ms()
# Small delay to prevent excessive CPU usage
time.sleep_ms(10)