- 利用 PWM (Pulse Width Modulation, 脈衝寬度調變) 控制LED燈光亮度。
元件︰
- LED x 1
- 220Ω 電阻 x 1
接線圖︰
架構圖︰
程式碼︰
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | const int ledPin = 9; int brightness = 0; int fadeAmount = 5; void setup() { pinMode(ledPin, OUTPUT); } void loop() { analogWrite(ledPin, brightness); brightness = brightness + fadeAmount; if ( brightness == 0 || brightness == 255 ) { fadeAmount = -fadeAmount; } delay(20); } |
沒有留言:
張貼留言