from machine import Pin,PWM #匯入控制接腳的程式庫
import time #匯入控制時間的程式庫
SW=Pin(12,Pin.IN,Pin.PULL_UP) #開關接腳(輸入模式為高電位觸發)
LED_R=PWM(Pin(25,Pin.OUT),1000) #LED_R接腳輸出
LED_G=PWM(Pin(33,Pin.OUT),1000) #LED_R接腳輸出
LED_B=Pin(32,Pin.OUT) #LED_R接腳輸出
PressTimes=0
LED_R.duty(1023)
LED_G.duty(1023)
LED_B.value(1)
def LED_R_Shine(): #定義LED_R_Shine函式
while True:
for i in range(1023,-1,-1):
LED_R.duty(i)
print(i)
time.sleep(0.001)
if SW.value()==0:
break
for i in range(1024):
LED_R.duty(i)
print(i)
time.sleep(0.001)
if SW.value()==0:
break
def LED_G_Shine(): #定義LED_G_Shine函式
while True:
for i in range(1023,-1,-1):
LED_G.duty(i)
print(i)
time.sleep(0.001)
if SW.value()==0:
break
for i in range(1024):
LED_G.duty(i)
print(i)
time.sleep(0.001)
if SW.value()==0:
break
def LED_B_Shine(): #定義LED_B_Shine函式
for i in range(3):
LED_B.value(0) #LED_B亮
time.sleep(3) #時間間隔3秒
LED_B.value(1) #LED_B暗
time.sleep(1.5) #時間間隔1.5秒
while True: #while迴圈重複執行
if SW.value()==0: #當按下開關
PressTimes=PressTimes+1
print("SET",PressTimes)
time.sleep(1)
if PressTimes==1:
LED_R_Shine()
elif PressTimes==2:
LED_R.duty(1023)
LED_G_Shine()
elif PressTimes==3:
LED_G.duty(1023)
LED_B_Shine()
elif PressTimes==4:
print("End Program!")
break
while SW.value()==0: #當SW3.value()函數裡的值為0
pass #不做任何事,繼續執行程式
time.sleep(0.3)