Arduino學習150.內建範例-呼吸燈
↓打開Arduino內建範例,檔案>範例>Basics>Fade
跳出另一個視窗↓
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);
}
說明著Pin9接LED燈,故硬體安裝如下↓
上傳後燈不再只是開和關的動作,燈由慢亮再慢暗,反覆持續著,
試著把參數(brightness 255)以及(delay
30)的數據修改上傳試試,
注意數位腳位有標示著~的3,5,6,9,10,11腳位,才有PWM訊號功能,
也可把腳位改到沒有~的腳位看看有何不同,
沒有留言:
張貼留言