由于没有拓展的LED,于是拿串口13来实验了,虽然效果不明显,但是基本上是差不多了。这里关键是analogWrite(led,brightness)的使用,详见:点击打开链接。注意一点是arduino的loop函数相当于一个循环,它会一直重复执行,剩下的就和C/C++编程语言的语法差不多了。

参考代码如下:

int led = 13;
int brightness = 0;
int fadeAmount = 15;

void setup() {
   pinMode(led, OUTPUT);
}

void loop() {
   analogWrite(led, brightness);
   brightness = brightness + fadeAmount;
   if (brightness == 0 || brightness == 255) {
      fadeAmount = -fadeAmount ;
   }
   delay(100);
}