int led = 10; int brightness = 0; int lighten = 5; void setup() { } void loop() { analogWrite(led,brightness); if(brightness<=250) brightness = brightness + lighten; else brightness=0; delay(50); } ``` Scratch的寫法: 有沒有發現LED燈明暗的狀況不明顯?原因是甚麼呢? 因為LED燈需要的電壓很小,所以變暗的狀況不明顯,如果要明顯一點,需要在低電壓時拉長時間,如何修改呢? 課後發想:如何改寫成for迴圈?
進階搜尋