1. 程式人生 > >Arduino例程解讀與實驗(4.Fade 漸明漸暗)

Arduino例程解讀與實驗(4.Fade 漸明漸暗)

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);
}