床頭灯控制(Fade)
Feb. 12, 2016

[1]
本實驗延伸Ex1_1的
Lab1_1_2,將LED的正極由原先接到Arduino第13腳改接到
第9腳,並將程式由呼叫digitalWrite()改為呼叫
analogWrite()。因為analogWrite()使用變更
工作週期(duty cycle)的
PWM技術來模擬連續性的
類比信号,故能使LED亮度而有
漸明/暗的效果,也就是常聽到
呼吸灯的由來。
本範例过関後,可獲得
LED灯光Fade卡一張。
難易度:易
材料
- LED(Light-Emitting Diode,發光二極体)一顆
- 220W電阻一顆(色环:紅紅棕金)
- 杜邦線2條
- 麵包板一塊
- Arduino Uno R3一塊(本系列文章皆簡稱為Arduino開發版)
- A-B型USB傳輸線一條
程式(
Fade.ino)
在Arduino IDE中由Fade File->Examples->1.Basics->Fade開啟Arduino內建的範例程式。
int brightness = 0; // how bright the LED is
int fadeAmount = 5; // how many points to fade the LED by
void setup() {
// declare pin 9 to be an output:
pinMode(9, OUTPUT);
}
void loop() {
// set the brightness of pin 9:
analogWrite(9, brightness);
// change the brightness for next time through the loop:
brightness = brightness + fadeAmount;
// reverse the direction of the fading at the ends of the fade:
if (brightness == 0 || brightness == 255){
fadeAmount = -fadeAmount ;
}
delay(30); // wait for 30 milliseconds to see the dimming effect
}
流程圖(
.vsdx)
執行結果
接線圖(
.fzz)
高手必練
修改控制LED明滅度fadeAmount的值(例如:5->10),再覌察LED灯的明滅是否有不同變化