## **模擬信號輸出[Mega]**
> 本例會使用Arduino Mega 或 Genuino Mega 板讓12個LED一個接一個的進行亮度漸變。使用Mega的原因由于它有更多支持**PWM**操作的引腳。
### **需要硬件**
* Arduino Mega 或 Genuino Mega 板
* 12個紅色LED
* 12個220Ω電阻
* 跳線
* 面包板
* 杜邦線/電路連接線
### **電路**

將12個LED的陽極(長腳)通過220Ω限流電阻分別連接到2-13號引腳。將12個LED的陰極(短腳)連接到GND。
### **原理圖**

### **代碼**
在setup()函數中使用了一個for()循環來給2-13號引腳設置工作模式為OUTPUT。.?
接著在loop()函數中,有一個嵌套的for循環。?
最外層循環是:
~~~
for (int thisPin =lowestPin; thisPin <= highestPin; thisPin++)
~~~
最外層循環會遍歷所有的LED。在最外層循環切換到下一個LED之前,還需要做兩件事。
* 第一:你需要將當前LED逐漸點亮
~~~
for (int brightness = 0; brightness < 255; brightness++) {
analogWrite(thisPin, brightness);
delay(2);
}
~~~
* 第二:你需要讓當前LED逐漸熄滅?
這個循環每過一次,brightness變量值+1,并且這個值會被寫到引腳。當brightness達到最高的PWM值255時,下面的循環開始執行:
~~~
for (int brightness = 255; brightness >= 0; brightness--) {
analogWrite(thisPin, brightness);
delay(2);
}
~~~
這個循環減少brightness變量值,這會將LED亮度減低到0。當brightness值為0時,最外層for循環繼續循環執行。下一個LED就會重復上述過程。
~~~
/*
模擬信號輸出[Mega]
這個例子讓2-13號引腳連接的LED一個接一個的漸變。
本例子運行在Arduino Mega板,之前的板子不可用。
電路連接:
* LED分別從2到13連接到GND
代碼是公開的。
*/
//常量,用來定義引腳范圍
const int lowestPin = 2;
const int highestPin = 13;
void setup() {
//設置2號引腳到13:
for (int thisPin = lowestPin; thisPin <= highestPin; thisPin++) {
pinMode(thisPin, OUTPUT);
}
}
void loop() {
// 迭代遍歷所有引腳:
for (int thisPin = lowestPin; thisPin <= highestPin; thisPin++) {
//讓LED漸亮:
for (int brightness = 0; brightness < 255; brightness++) {
analogWrite(thisPin, brightness);
delay(2);
}
//讓LED漸暗:
for (int brightness = 255; brightness >= 0; brightness--) {
analogWrite(thisPin, brightness);
delay(2);
}
// 在LED漸變之間delay:
delay(100);
}
}
~~~
### **相關資料**
[for()](https://www.arduino.cc/en/Reference/For)?
[analogWrite()](https://www.arduino.cc/en/Reference/AnalogWrite)?
[delay()](https://www.arduino.cc/en/Reference/Delay)?
- 說明
- 系統示例文件目錄結構及說明
- 01.Basics
- AnalogReadSerial
- BareMinimum
- Blink
- DigitalReadSerial
- Fade
- ReadAnalogVoltage
- 02.Digital
- BlinkWithoutDelay
- Button
- Debounce
- DigitalInputPullup
- StateChangeDetection
- toneKeyboard
- toneMelody
- toneMultiple
- tonePitchFollower
- 03.Analog
- AnalogInOutSerial
- AnalogInput
- AnalogWriteMega
- Calibration
- Fading
- Smoothing
- 04.Communication
- ASCIITable
- Dimmer
- Graph
- Midi
- MultiSerial
- PhysicalPixel
- ReadASCIIString
- SerialCallResponse
- SerialCallResponseASCII
- SerialEvent
- SerialPassthrough
- VirtualColorMixer
- 05.Control
- Arrays
- ForLoopIteration
- IfStatementConditional
- switchCase
- switchCase2
- WhileStatementConditional
- 06.Sensors
- ADXL3xx
- Knock
- Memsic2125
- Ping
- 07.Display
- barGraph
- RowColumnScanning
- 08.Strings
- CharacterAnalysis
- StringAdditionOperator
- StringAppendOperator
- StringCaseChanges
- StringCharacters
- StringComparisonOperators
- StringConstructors
- StringIndexOf
- StringLength
- StringLengthTrim
- StringReplace
- StringStartsWithEndsWith
- StringSubstring
- StringToInt
- 09.USB
- Keyboard
- KeyboardLogout
- KeyboardMessage
- KeyboardReprogram
- KeyboardSerial
- KeyboardAndMouseControl
- Mouse
- ButtonMouseControl
- JoystickMouseControl
- 10.StarterKit_BasicKit (與特定硬件相關,暫無)
- p02_SpaceshipInterface
- p03_LoveOMeter
- p04_ColorMixingLamp
- p05_ServoMoodIndicator
- p06_LightTheremin
- p07_Keyboard
- p08_DigitalHourglass
- p09_MotorizedPinwheel
- p10_Zoetrope
- p11_CrystalBall
- p12_KnockLock
- p13_TouchSensorLamp
- p14_TweakTheArduinoLogo
- p15_HackingButtons
- 11.ArduinoISP(暫無)
- ArduinoISP