用Tinkercad学arduino之板载AnalogWrite控制LED渐亮渐暗

项目地址:https://www.tinkercad.com/things/fuEoV1EqLvS

 

/*
  渐显渐隐

  Arduino使用analogWrite函数在9号引脚上产生PWM脉冲宽度调制,使得发光二极管LED渐显渐隐,
  如果你想换一个引脚,请使用具有PWM输出能力的引脚,在多数板子上以波浪号tilder标注 ,
  像是 ~3, ~5, ~6, ~9, ~10 和 ~11.

*/

int brightness = 0;

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

void loop()
{
  for (brightness = 0; brightness <= 255; brightness += 5) {
    analogWrite(9, brightness);
    delay(30); // Wait for 30 millisecond(s)
  }
  for (brightness = 255; brightness >= 0; brightness -= 5) {
    analogWrite(9, brightness);
    delay(30); // Wait for 30 millisecond(s)
  }
}

 

posted @ 2020-12-03 00:53  meetrice  阅读(8)  评论(0编辑  收藏