from machine import Pin, Timer
import time
# กำหนดพินสำหรับ LED สีแดงและสีเขียว และ Limit Switch สำหรับแต่ละ Unit
units = [3$0
{'red': Pin(7, Pin.OUT), 'green': Pin(6, Pin.OUT), 'switch': Pin(5, Pin.IN, Pin.PULL_DOWN), 'blink_timer': None, 'green_timer': None, 'last_pressed': 0, 'active': False, 'red_on': False},
{'red': Pin(11, Pin.OUT), 'green': Pin(10, Pin.OUT), 'switch': Pin(9, Pin.IN, Pin.PULL_DOWN), 'blink_timer': None, 'green_timer': None, 'last_pressed': 0, 'active': False, 'red_on': False},
{'red': Pin(15, Pin.OUT), 'green': Pin(14, Pin.OUT), 'switch': Pin(13, Pin.IN, Pin.PULL_DOWN), 'blink_timer': None, 'green_timer': None, 'last_pressed': 0, 'active': False, 'red_on': False},
{'red': Pin(17, Pin.OUT), 'green': Pin(16, Pin.OUT), 'switch': Pin(18, Pin.IN, Pin.PULL_DOWN), 'blink_timer': None, 'green_timer': None, 'last_pressed': 0, 'active': False, 'red_on': False},
{'red': Pin(21, Pin.OUT), 'green': Pin(20, Pin.OUT), 'switch': Pin(22, Pin.IN, Pin.PULL_DOWN), 'blink_timer': None, 'green_timer': None, 'last_pressed': 0, 'active': False, 'red_on': False},
{'red': Pin(27, Pin.OUT), 'green': Pin(26, Pin.OUT), 'switch': Pin(28, Pin.IN, Pin.PULL_DOWN), 'blink_timer': None, 'green_timer': None, 'last_pressed': 0, 'active': False, 'red_on': False}
]
DEBOUNCE_TIME = 200 # 200 milliseconds for debounce
def set_led_state(led_red, led_green, red_state, green_state):
led_red.value(red_state)
led_green.value(green_state)
def handle_blink_timer(timer, unit):
if unit['red_on']:
set_led_state(unit['red'], unit['green'], 0, 0) # ปิด LED สีแดง
unit['red_on'] = False
unit['blink_timer'].init(mode=Timer.ONE_SHOT, period=900, callback=lambda t: handle_blink_timer(t, unit)) # 0.9 วินาทีสำหรับปิด
else:
set_led_state(unit['red'], unit['green'], 1, 0) # เปิด LED สีแดง
unit['red_on'] = True
unit['blink_timer'].init(mode=Timer.ONE_SHOT, period=100, callback=lambda t: handle_blink_timer(t, unit)) # 0.1 วินาทีสำหรับเปิด
def handle_green_timer(timer, unit):
if unit['blink_timer'] is not None:
unit['blink_timer'].deinit() # ยกเลิกการกระพริบไฟสีแดง
set_led_state(unit['red'], unit['green'], 0, 1) # ปิด LED สีแดง และเปิด LED สีเขียวค้างไว้
def check_switch(unit):
current_time = time.ticks_ms()
if unit['switch'].value() == 1 and time.ticks_diff(current_time, unit['last_pressed']) > DEBOUNCE_TIME:
unit['last_pressed'] = current_time
if not unit['active']:
unit['active'] = True
if unit['blink_timer'] is not None:
unit['blink_timer'].deinit()
unit['blink_timer'] = Timer(-1)
unit['blink_timer'].init(mode=Timer.ONE_SHOT, period=100, callback=lambda t: handle_blink_timer(t, unit)) # เริ่มการกระพริบไฟสีแดง
if unit['green_timer'] is not None:
unit['green_timer'].deinit()
unit['green_timer'] = Timer(-1)
unit['green_timer'].init(mode=Timer.ONE_SHOT, period=2000, callback=lambda t: handle_green_timer(t, unit)) # ตั้งเวลา 2 วินาทีเพื่อสลับเป็นไฟเขียว
elif unit['switch'].value() == 0 and unit['active']:
unit['active'] = False
release_switch(unit)
def release_switch(unit):
if unit['blink_timer'] is not None:
unit['blink_timer'].deinit()
if unit['green_timer'] is not None:
unit['green_timer'].deinit()
set_led_state(unit['red'], unit['green'], 0, 0) # ปิด LED ทั้งสองดวง
# ฟังก์ชันหลัก
def main():
while True:
for unit in units:
check_switch(unit)
time.sleep(0.01) # เพิ่มการหน่วงเวลาเพื่อป้องกันการใช้ CPU สูงเกินไป
# เรียกใช้ฟังก์ชันหลัก
main()