2016年1月17日 星期日

Arduino UNO學習331.內建範例-PWM與數位



Arduino學習331.內建範例-PWM與數位

前面的學習都知道了UNO板的數位接腳2,4,7,8,12,13,3,5,6,9,10,11,
二者之間的不同,標示差異多了符號,表示該腳位才可傳送PWM數據

硬體準備
分別由數位腳Pin2~13各接上燈及電阻共12

打開內建範例>Analog>AnalogWriteMega
// These constants won't change.  They're used to give names
// to the pins used:
const int lowestPin = 2;
const int highestPin = 13;


void setup() {
  // set pins 2 through 13 as outputs:
  for (int thisPin = lowestPin; thisPin <= highestPin; thisPin++) {
    pinMode(thisPin, OUTPUT);
  }
}

void loop() {
  // iterate over the pins:
  for (int thisPin = lowestPin; thisPin <= highestPin; thisPin++) {
    // fade the LED on thisPin from off to brightest:
    for (int brightness = 0; brightness < 255; brightness++) {
      analogWrite(thisPin, brightness);
      delay(2);
    }
    // fade the LED on thisPin from brithstest to off:
    for (int brightness = 255; brightness >= 0; brightness--) {
      analogWrite(thisPin, brightness);
      delay(2);
    }
    // pause between LEDs:
    delay(100);
  }
}

將程式上傳後,很明顯的可以看到,PWM腳位和純數位腳位的亮燈差異,
https://www.arduino.cc/en/Tutorial/AnalogWriteMega


沒有留言:

張貼留言