Arduino例程解读与实验(4.Fade 渐明渐暗)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/weixin_42948798/article/details/83927288

Arduino例程解读与实验(4.Fade 渐明渐暗)

/*
  Fade//渐明渐暗

  This example shows how to fade an LED on pin 9 using the analogWrite()
  function.
  //该例程展示如何使用analogWrite()函数实现与9脚连接的LED灯渐明渐暗变化。
  The analogWrite() function uses PWM, so if you want to change the pin you're
  using, be sure to use another PWM capable pin. On most Arduino, the PWM pins
  are identified with a "~" sign, like ~3, ~5, ~6, ~9, ~10 and ~11.
//analogWrite()函数能实现PWM(脉宽调制)功能,所以如果你要把9脚更换为别的引脚,一定要确保所更换的引脚具有PWM功能。在大多说Arduino 板上,具有PWM 功能的引脚都用“~”符号注明,如 ~3, ~5, ~6, ~9, ~10 , ~11等。
  This example code is in the public domain.
例程代码见以下网址
  http://www.arduino.cc/en/Tutorial/Fade
*/

int led = 9;           // the PWM pin the LED is attached to//LED灯连接的PWM引脚
int brightness = 0;    // how bright the LED is//LED灯的亮度值
int fadeAmount = 5;    // how many points to fade the LED by//每次的变化值

// the setup routine runs once when you press reset:
//按下复位键后setup()只运行一次
void setup() {
  // declare pin 9 to be an output://声明9脚为输出模式
  pinMode(led, OUTPUT);
}

// the loop routine runs over and over again forever://loop()为无限循环
void loop() {
  // set the brightness of pin 9://设置第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//延迟30毫秒观察效果
  delay(30);
}

猜你喜欢

转载自blog.csdn.net/weixin_42948798/article/details/83927288
今日推荐