import machine
import time
# GPIO pin definitions
motion_sensor = machine.Pin(32, machine.Pin.IN)
sw1 = machine.Pin(15, machine.Pin.IN, machine.Pin.PULL_DOWN)
sw2 = machine.Pin(2, machine.Pin.IN, machine.Pin.PULL_DOWN)
# LEDs
R1 = machine.Pin(26, machine.Pin.OUT)
Y1 = machine.Pin(25, machine.Pin.OUT)
G1 = machine.Pin(33, machine.Pin.OUT)
R2 = machine.Pin(13, machine.Pin.OUT)
Y2 = machine.Pin(12, machine.Pin.OUT)
G2 = machine.Pin(14, machine.Pin.OUT)
lamp = machine.Pin(35, machine.Pin.IN)
# Function to reset all LEDs
def reset_leds():
R1.value(0)
Y1.value(0)
G1.value(0)
R2.value(0)
Y2.value(0)
G2.value(0)
lamp.value(0)
# Function to handle the traffic light sequence based on the table
def traffic_sequence():
# Step 1: R1 OFF, Y1 OFF, G1 OFF, R2 OFF, Y2 OFF, G2 OFF, Lamp OFF
reset_leds()
time.sleep(1)
# Step 2: R1 ON, Y1 OFF, G1 OFF, R2 OFF, Y2 OFF, G2 ON, Lamp ON
R1.value(1)
G2.value(1)
lamp.value(1)
time.sleep(1)
# Step 3: R1 OFF, Y1 ON, G1 OFF, R2 ON, Y2 OFF, G2 OFF, Lamp ON
R1.value(0)
Y1.value(1)
R2.value(1)
G2.value(0)
time.sleep(1)
# Step 4: R1 OFF, Y1 OFF, G1 ON, R2 ON, Y2 OFF, G2 OFF, Lamp ON
Y1.value(0)
G1.value(1)
time.sleep(1)
# Step 5: R1 OFF, Y1 OFF, G1 OFF, R2 OFF, Y2 ON, G2 OFF, Lamp ON
G1.value(0)
R2.value(0)
Y2.value(1)
time.sleep(1)
# Step 6: R1 ON, Y1 OFF, G1 OFF, R2 OFF, Y2 OFF, G2 ON, Lamp ON
R1.value(1)
Y2.value(0)
G2.value(1)
time.sleep(1)
# Main loop
while True:
if sw1.value() == 1: # SW1 is pressed, start traffic sequence
traffic_sequence()
if sw2.value() == 1: # SW2 is pressed, stop all LEDs
reset_leds()
if motion_sensor.value() == 1: # Motion detected, R1 and R2 ON, all others OFF
R1.value(1)
R2.value(1)
Y1.value(0)
G1.value(0)
Y2.value(0)
G2.value(0)
lamp.value(1)
else:
reset_leds() # No motion detected, reset LEDs