4-1 4位元按鈕開關模組-紅綠燈控制
#-*- coding:big5 -*- #!/usr/bin/python # 範例: LED紅綠燈實驗- # S1功能-全部固定為紅燈(需在正常模式下按下) # S2功能-南北向固定綠燈(需在正常模式下按下) # S3功能-東西向固定綠燈(需在正常模式下按下) # S4功能-回到正常模式 # 接腳說明: GPIO 3-11 接至 LED紅綠燈 # GPIO 15-22 接至 LED紅綠燈 # GPIO 26-32 接至 按鍵模組 # import RPi.GPIO as GPIO import time , threading gpio_list = [3,5,7,8,10,11,12,13,15,16,18,19,21,22,23,24] led1_R = 3 led1_Y = 5 led1_G = 7 led2_R = 8 led2_Y = 10 led2_G = 11 led3_R = 15 led3_Y = 16 led3_G = 18 led4_R = 19 led4_Y = 21 led4_G = 22 S1 = 26 S2 = 29 S3 = 31 S4 = 32 #DELAY_TIME = 1 # Use physical pin numbers GPIO.setmode(GPIO.BOARD) TimerCnt_DELAY = 1 #TIMER1------------------------------------------------10ms def Timer1(): global TimerCnt_DELAY if TimerCnt_DELAY > 0 : TimerCnt_DELAY = TimerCnt_DELAY - 1 global t1 t1 = threading.Timer(0.01 , Timer1) t1.start() #TIMER1------------------------------------------------ for i in range(16): GPIO.setup(gpio_list[i], GPIO.OUT) for i in range(16): GPIO.output(gpio_list[i], GPIO.LOW) GPIO.setup(S1, GPIO.IN) GPIO.setup(S2, GPIO.IN) GPIO.setup(S3, GPIO.IN) GPIO.setup(S4, GPIO.IN) S1_buffer = 0 S2_buffer = 0 S3_buffer = 0 S4_buffer = 0 status = 0 status_NS_G = 0 status_WE_G = 0 t1 = threading.Timer(0.01 , Timer1) t1.start() print("13.SIGNAL DEMO Starting... ") while True: if TimerCnt_DELAY == 0 : if status == 0 : GPIO.output(led2_Y, GPIO.LOW) #RED GPIO.output(led4_Y, GPIO.LOW) GPIO.output(led2_R, GPIO.HIGH) GPIO.output(led4_R, GPIO.HIGH) TimerCnt_DELAY = 100 #1S status = 1 elif status == 1 : GPIO.output(led1_R, GPIO.LOW) #NS GREEN GPIO.output(led3_R, GPIO.LOW) GPIO.output(led1_G, GPIO.HIGH) GPIO.output(led3_G, GPIO.HIGH) if status_NS_G == 1 : status = 9 else: status = 2 TimerCnt_DELAY = 500 #5S elif status == 2 : GPIO.output(led1_G, GPIO.LOW) #NS YELLOW GPIO.output(led3_G, GPIO.LOW) GPIO.output(led1_Y, GPIO.HIGH) GPIO.output(led3_Y, GPIO.HIGH) TimerCnt_DELAY = 200 #2S status = 3 elif status == 3 : GPIO.output(led1_Y, GPIO.LOW) #RED GPIO.output(led3_Y, GPIO.LOW) GPIO.output(led1_R, GPIO.HIGH) GPIO.output(led3_R, GPIO.HIGH) TimerCnt_DELAY = 100 #1S status = 4 elif status == 4 : GPIO.output(led2_R, GPIO.LOW) #WE GREEN GPIO.output(led4_R, GPIO.LOW) GPIO.output(led2_G, GPIO.HIGH) GPIO.output(led4_G, GPIO.HIGH) if status_WE_G == 1 : status = 10 else: status = 5 TimerCnt_DELAY = 500 #5S elif status == 5 : GPIO.output(led2_G, GPIO.LOW) #WE YELLOW GPIO.output(led4_G, GPIO.LOW) GPIO.output(led2_Y, GPIO.HIGH) GPIO.output(led4_Y, GPIO.HIGH) TimerCnt_DELAY = 200 #2S status = 0 elif status == 6 : #ALL RED GPIO.output(led2_G, GPIO.LOW) GPIO.output(led4_G, GPIO.LOW) GPIO.output(led2_Y, GPIO.LOW) GPIO.output(led4_Y, GPIO.LOW) GPIO.output(led2_R, GPIO.HIGH) GPIO.output(led4_R, GPIO.HIGH) GPIO.output(led1_G, GPIO.LOW) GPIO.output(led3_G, GPIO.LOW) GPIO.output(led1_Y, GPIO.LOW) GPIO.output(led3_Y, GPIO.LOW) GPIO.output(led1_R, GPIO.HIGH) GPIO.output(led3_R, GPIO.HIGH) elif status == 7 : GPIO.output(led1_G, GPIO.LOW) #NS YELLOW GPIO.output(led3_G, GPIO.LOW) GPIO.output(led1_Y, GPIO.HIGH) GPIO.output(led3_Y, GPIO.HIGH) TimerCnt_DELAY = 200 #2S status = 6 elif status == 8 : GPIO.output(led2_G, GPIO.LOW) #WE YELLOW GPIO.output(led4_G, GPIO.LOW) GPIO.output(led2_Y, GPIO.HIGH) GPIO.output(led4_Y, GPIO.HIGH) TimerCnt_DELAY = 200 #2S status = 6 elif status == 9 : GPIO.output(led2_G, GPIO.LOW) GPIO.output(led4_G, GPIO.LOW) GPIO.output(led2_Y, GPIO.LOW) GPIO.output(led4_Y, GPIO.LOW) GPIO.output(led2_R, GPIO.HIGH) GPIO.output(led4_R, GPIO.HIGH) GPIO.output(led1_G, GPIO.HIGH) GPIO.output(led3_G, GPIO.HIGH) GPIO.output(led1_Y, GPIO.LOW) GPIO.output(led3_Y, GPIO.LOW) GPIO.output(led1_R, GPIO.LOW) GPIO.output(led3_R, GPIO.LOW) elif status == 10 : GPIO.output(led2_G, GPIO.HIGH) GPIO.output(led4_G, GPIO.HIGH) GPIO.output(led2_Y, GPIO.LOW) GPIO.output(led4_Y, GPIO.LOW) GPIO.output(led2_R, GPIO.LOW) GPIO.output(led4_R, GPIO.LOW) GPIO.output(led1_G, GPIO.LOW) GPIO.output(led3_G, GPIO.LOW) GPIO.output(led1_Y, GPIO.LOW) GPIO.output(led3_Y, GPIO.LOW) GPIO.output(led1_R, GPIO.HIGH) GPIO.output(led3_R, GPIO.HIGH) #button if ((GPIO.input(S1) == 0) and (S1_buffer == 1)): if ((status == 1)or(status == 4)or(status == 3)or(status == 0)) : status = 6 TimerCnt_DELAY = 100 #1S if (status == 2): status = 7 TimerCnt_DELAY = 0 if (status == 5): status = 8 TimerCnt_DELAY = 0 print ("S1_PUSH") S1_buffer = GPIO.input(S1) if ((GPIO.input(S2) == 0) and (S2_buffer == 1)): if ((status == 1)or(status == 4)or(status == 2)) : status = 9 else: status_NS_G = 1 TimerCnt_DELAY = 0 print ("S2_PUSH") S2_buffer = GPIO.input(S2) if ((GPIO.input(S3) == 1) and (S3_buffer == 0)): if ((status == 1)or(status == 4)or(status == 5)) : status = 10 else: status_WE_G = 1 TimerCnt_DELAY = 0 print ("S3_PUSH") S3_buffer = GPIO.input(S3) if ((GPIO.input(S4) == 1) and (S4_buffer == 0)): if (status == 6) : status = 0 if (status == 9) : status = 2 if (status == 10) : status = 5 status_NS_G = 0 status_WE_G = 0 print ("S4_PUSH") S4_buffer = GPIO.input(S4) time.sleep(0.001) #button GPIO.cleanup()