|
單向原始碼
- #define Speed 4 //Min = 1 ,Max = 5
- #define Long 180 //Min = 0 ,Max = 254
- byte Led[5] ={3,5,6,9,10}; //建立儲存LED接腳位置陣列
- int Br[5]; //建立儲存LED亮度陣列
- byte i;
- //-----副程式區-----//
- int Br_dec(byte LED,int BR)
- {
- analogWrite(LED,BR); //對LED腳位輸出PWM信號
- BR-=1; //並將值衰減一次後回傳
- if(BR < 1) BR=0;
- return BR;
- }
- void setup()
- {
- for(i=0;i<5;i++) pinMode(Led[i], OUTPUT);
- }
- void loop()
- {
- for(i=0;i<5;i++) Br[i] = 255; //存入各LED點燈亮度值
- i = 0; //預設開始點燈位置
- do
- {
- Br[i] = Br_dec(Led[i],Br[i]);
- if(i>0) Br[i-1] = Br_dec(Led[i-1],Br[i-1]);
- if(i>1) Br[i-2] = Br_dec(Led[i-2],Br[i-2]);
- if(i>2) Br[i-3] = Br_dec(Led[i-3],Br[i-3]);
- if(i>3) Br[i-4] = Br_dec(Led[i-4],Br[i-4]);
- if(Br[i]==Long && i<4) i++; //判斷是否點亮下一顆燈
- delay(6-Speed); //設定延遲時間
- }while(Br[4] > 0); //判斷最後一個燈是否熄滅
- delay(150);
- }
複製代碼
雙向來回原始碼再下面(要自己先想想再來看哦!)
|
|