漸亮漸暗(Fading)
目的: 讓 LED 持續地逐漸變亮後再逐漸變暗。
材料清單:
1. Arduino 模組 x1
2. 麵包板 x1
3. 線材若干
4. LED x1
5. 220R 電阻 x1
電路圖:
接線圖:
程式碼:
/*
Fade.pde
使用 analogWrite() 讓插在 9 pin 上的 LED 逐漸變亮後又變暗。
*/
int led = 9; // the pin that the LED is attached to
int brightness = 0; // how bright the LED is
int fadeAmount = 5; // how many points to fade the LED by
// the setup routine runs once when you press reset:
void setup() {
// declare pin 9 to be an output:
pinMode(led, OUTPUT);
}
// the loop routine runs over and over again forever:
void loop() {
// set the brightness of pin 9:
analogWrite(led, 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 ;
}
// wait for 30 milliseconds to see the dimming effect
delay(30);
}
相關連結:
1. Arduino 官網 http://arduino.cc/en/Tutorial/Fade
沒有留言:
張貼留言