## **模拟信号输出[Mega]**
> 本例会使用Arduino Mega 或 Genuino Mega 板让12个LED一个接一个的进行亮度渐变。使用Mega的原因由于它有更多支持**PWM**操作的引脚。
### **需要硬件**
* Arduino Mega 或 Genuino Mega 板
* 12个红色LED
* 12个220Ω电阻
* 跳线
* 面包板
* 杜邦线/电路连接线
### **电路**
![图片来自官网](http://img.blog.csdn.net/20160509170032928)
将12个LED的阳极(长脚)通过220Ω限流电阻分别连接到2-13号引脚。将12个LED的阴极(短脚)连接到GND。
### **原理图**
![图片来自官网](http://img.blog.csdn.net/20160509170048432)
### **代码**
在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