from machine import Pin, Timer
import utime
# 定義 LED 腳位
red_straight_pin = Pin(0, Pin.OUT)
yellow_straight_pin = Pin(1, Pin.OUT)
green_straight_pin = Pin(2, Pin.OUT)
red_horizontal_pin = Pin(13, Pin.OUT)
yellow_horizontal_pin = Pin(14, Pin.OUT)
green_horizontal_pin = Pin(15, Pin.OUT)
# 定義按鈕腳位
button_a = Pin(3, Pin.IN, Pin.PULL_UP)
button_b = Pin(4, Pin.IN, Pin.PULL_UP)
button_c = Pin(5, Pin.IN, Pin.PULL_UP)
button_d = Pin(6, Pin.IN, Pin.PULL_UP)
button_e = Pin(7, Pin.IN, Pin.PULL_UP) # 假設連接到腳位7
button_f = Pin(8, Pin.IN, Pin.PULL_UP) # 假設連接到腳位8
button_g = Pin(9, Pin.IN, Pin.PULL_UP) # 假設連接到腳位9
button_h = Pin(10, Pin.IN, Pin.PULL_UP) # 假設連接到腳位10
# 初始化 Timer
led_timer = Timer()
# 關閉所有燈號
def turn_off_all_lights():
red_straight_pin.value(0)
yellow_straight_pin.value(0)
green_straight_pin.value(0)
red_horizontal_pin.value(0)
yellow_horizontal_pin.value(0)
green_horizontal_pin.value(0)
# 同時亮紅燈
def both_red(timer):
red_straight_pin.value(not red_straight_pin.value())
red_horizontal_pin.value(not red_horizontal_pin.value())
# 同時亮黃燈
def both_yellow(timer):
yellow_straight_pin.value(not yellow_straight_pin.value())
yellow_horizontal_pin.value(not yellow_horizontal_pin.value())
# 同時橫向亮黃燈,直向亮紅燈
def flash_horizontal_red_straight_yellow_horizontal(timer):
yellow_horizontal_pin.value(not yellow_horizontal_pin.value())
red_straight_pin.value(not red_straight_pin.value())
# 同時橫向亮紅燈,直向亮黃燈
def flash_horizontal_red_horizontal_yellow_straight(timer):
red_horizontal_pin.value(not red_horizontal_pin.value())
yellow_straight_pin.value(not yellow_straight_pin.value())
# 新增的按鈕功能
def horizontal_green_vertical_red(timer):
green_horizontal_pin.value(not green_horizontal_pin.value())
red_straight_pin.value(not red_straight_pin.value())
def horizontal_red_vertical_green(timer):
red_horizontal_pin.value(not red_horizontal_pin.value())
green_straight_pin.value(not green_straight_pin.value())
def traffic_light_pattern_1(timer):
# R(5s) -> R/Y(0.5s) -> G(5s) -> Y(0.5s)
turn_off_all_lights()
red_straight_pin.value(1)
red_horizontal_pin.value(1)
utime.sleep(5)
yellow_straight_pin.value(1)
yellow_horizontal_pin.value(1)
utime.sleep(0.5)
red_straight_pin.value(0)
red_horizontal_pin.value(0)
yellow_straight_pin.value(0)
yellow_horizontal_pin.value(0)
green_straight_pin.value(1)
green_horizontal_pin.value(1)
utime.sleep(5)
green_straight_pin.value(0)
green_horizontal_pin.value(0)
yellow_straight_pin.value(1)
yellow_horizontal_pin.value(1)
utime.sleep(0.5)
yellow_straight_pin.value(0)
yellow_horizontal_pin.value(0)
def traffic_light_pattern_2(timer):
# R(8s) -> R/Y(0.5s) -> G(2s) -> Y(0.5s)
turn_off_all_lights()
red_straight_pin.value(1)
red_horizontal_pin.value(1)
utime.sleep(8)
yellow_straight_pin.value(1)
yellow_horizontal_pin.value(1)
utime.sleep(0.5)
red_straight_pin.value(0)
red_horizontal_pin.value(0)
yellow_straight_pin.value(0)
yellow_horizontal_pin.value(0)
green_straight_pin.value(1)
green_horizontal_pin.value(1)
utime.sleep(2)
green_straight_pin.value(0)
green_horizontal_pin.value(0)
yellow_straight_pin.value(1)
yellow_horizontal_pin.value(1)
utime.sleep(0.5)
yellow_straight_pin.value(0)
yellow_horizontal_pin.value(0)
# 初始化按鈕狀態
last_button_state = 1
# 主迴圈接收使用者命令
while True:
# 檢測按鈕狀態,按下時執行相對應的指令
print("Button A:", button_a.value())
print("Button B:", button_b.value())
print("Button C:", button_c.value())
print("Button D:", button_d.value())
print("Button E:", button_e.value())
print("Button F:", button_f.value())
print("Button G:", button_g.value())
print("Button H:", button_h.value())
current_button_state_a = button_a.value()
current_button_state_b = button_b.value()
current_button_state_c = button_c.value()
current_button_state_d = button_d.value()
current_button_state_e = button_e.value()
current_button_state_f = button_f.value()
current_button_state_g = button_g.value()
current_button_state_h = button_h.value()
if current_button_state_a == 0 and last_button_state == 1:
led_timer.deinit()
turn_off_all_lights()
led_timer.init(period=500, mode=Timer.PERIODIC, callback=both_red)
elif current_button_state_b == 0 and last_button_state == 1:
led_timer.deinit()
turn_off_all_lights()
led_timer.init(period=500, mode=Timer.PERIODIC, callback=both_yellow)
elif current_button_state_c == 0 and last_button_state == 1:
led_timer.deinit()
turn_off_all_lights()
led_timer.init(period=500, mode=Timer.PERIODIC, callback=flash_horizontal_red_straight_yellow_horizontal)
elif current_button_state_d == 0 and last_button_state == 1:
led_timer.deinit()
turn_off_all_lights()
led_timer.init(period=500, mode=Timer.PERIODIC, callback=flash_horizontal_red_horizontal_yellow_straight)
elif current_button_state_e == 0 and last_button_state == 1:
led_timer.deinit()
turn_off_all_lights()
led_timer.init(period=500, mode=Timer.PERIODIC, callback=horizontal_green_vertical_red)
elif current_button_state_f == 0 and last_button_state == 1:
led_timer.deinit()
turn_off_all_lights()
led_timer.init(period=500, mode=Timer.PERIODIC, callback=horizontal_red_vertical_green)
elif current_button_state_g == 0 and last_button_state == 1:
led_timer.deinit()
turn_off_all_lights()
led_timer.init(period=500, mode=Timer.PERIODIC, callback=traffic_light_pattern_1)
elif current_button_state_h == 0 and last_button_state == 1:
led_timer.deinit()
turn_off_all_lights()
led_timer.init(period=500, mode=Timer.PERIODIC, callback=traffic_light_pattern_2)
#last_button_state = current_button_state_a | current_button_state_b | current_button_state_c | current_button_state_d | current_button_state_e | current_button_state_f | current_button_state_g | current_button_state_h
if last_button_state == 0:
led_timer.deinit()
turn_off_all_lights()
utime.sleep_ms(10)