## **模擬信號與串口**
> 本例向你展示如何從模擬信號輸入引腳讀模擬信號值、將讀出的值映射到0-255的范圍,并根據結果進行**PWM**來讓LED漸暗/漸亮以及打印結果到**Arduino IDE**上的**串口監視器**。
### **所需硬件**
* Arduino板Genuino板
* 電位器
* 紅色LED
* 220Ω電阻
* 杜邦線
### **電路**
?
電位器一個引腳連接到5V,中間引腳連接到A0口,最后一個引腳連接到GND。?
接下來,在9號引腳連一個220Ω限流電阻并串接一個LED。(LED長腳(陽極)應連接到電阻,短腳(陰極)連接到GND)。
### **原理圖**

### **代碼**
在下列代碼中。**setup**函數只做兩件事情:第一是定義了兩個引腳(A0給電位器,9號引腳給LED),以及兩個變量(**sensorValue**和**outputValue**)。第二是打開串口通信。
接下來,在主循環中,**sensorValue**被用來存儲來自電位器的原始模擬信號值。Arduino有一個返回值為0到1023的**analogRead**函數,以及一個參數為0-255的**analogWrite**函數。為了讓來自電位器的模擬信號值能正確操作LED,我們需要將值的范圍映射成0-255。
為了完成轉換,用map()函數即可:
~~~
outputValue = map(sensorValue, 0, 1023, 0, 255);
~~~
**outputValue**時刻與電位器映射后的值保持相等。**map()**函數有五個參數,依次是:要映射的原始值,原始值最小值,原始值最大值,映射后的最小值,映射后的最大值。這樣,傳感器返回的數據就能夠從其原來的0-1023被映射為0-255了。
接下來,新映射的傳感器數據就能會被輸出到**analogOutPin**來使LED在電位器轉動時漸亮/漸暗。最終,原始值和被映射到0-255的傳感器值將被輸出到Arduino IDE中。
~~~
/*
模擬信號與串口
讀取模擬輸入引腳的值,并且將它映射到0-255。然后使用映射后的值來為輸出引腳設定脈寬(通過PWM)。
并將結果輸出到串口監視器。
電路搭建:
* 電位器中間腳連接到A0。兩邊的引腳分別連接到+5V和GND.
* LED連接數字引腳9到GND
示例代碼公開
*/
// 常量的聲明定義,用作引腳定義
const int analogInPin = A0; // 電位器連接到的模擬輸入引腳
const int analogOutPin = 9; // LED連接的輸出引腳
//變量的聲明定義
int sensorValue = 0; // 從電位器讀到的值
int outputValue = 0; // PWM值
void setup() {
// 初始化串口(波特率9600):
Serial.begin(9600);
}
void loop() {
// 讀取模擬輸入值:
sensorValue = analogRead(analogInPin);
// 映射模擬輸入值到0-255:
outputValue = map(sensorValue, 0, 1023, 0, 255);
// 改變模擬輸出值:
analogWrite(analogOutPin, outputValue);
// 結果輸出到串口監視器:
Serial.print("sensor = ");
Serial.print(sensorValue);
Serial.print("\t output = ");
Serial.println(outputValue);
// 等兩毫秒以讓數模轉換器完成工作:
delay(2);
}
~~~
### **相關資料**:
[map()](http://www.arduino.cc/en/Reference/Map)?
[analogRead()](http://www.arduino.cc/en/Reference/AnalogRead)?
[analogWrite()](http://www.arduino.cc/en/Reference/AnalogWrite)?
[serial()](http://www.arduino.cc/en/Reference/Serial)?
- 說明
- 系統示例文件目錄結構及說明
- 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
