此範例程式為控制第13腳位的LED燈以一定的速度漸漸變亮或漸漸變暗,以下為程式碼,程式法基本上跟[Arduino]範例程式-利用Arduino serial monitor作Debug文章一樣,但是不用設定終端機的Port位而已,所以我程式碼就不囉說解釋囉~
int value = 0; // initialize the LED value
int ledpin = 13; // initialize the ledpin
void setup() {
pinMode(ledpin, OUTPUT); // initialize the pin13 as an output.
}
void loop() {
for(value = 0 ; value <= 255; value+=5) {
analogWrite(ledpin, value);
delay(50);
} // become bright gradually
for(value = 255; value >=0; value-=5) {
analogWrite(ledpin, value);
delay(50);
} // become dark gradually
}