## **信號平滑**
> 本例中程序不斷的讀取模擬信號值,計算平均值,并且將平均值輸出到電腦。本例所展示的方法能夠平滑飄忽不定或忽上忽下的模擬信號。例子中也展示了使用數組存儲數據的方法。
### **所需硬件**
* Arduino板或Genuino板
* 10kΩ電位器
* 連接線
### **電路搭建**
?
將電位器兩側的引腳分別連接到5V和GND,將中間引腳連接到A0。
### **原理圖**

### **代碼**
下列代碼按照次序存儲10次讀取值到數組,將這十個值求和,并且求取平均數,得到的平均數會起到平滑作用。由于計算平均值的時刻是每次添加新的傳感器值時(每增加一個就計算一次)。因此,每次代碼執行的時間都是相同的。
你可以增加數組的容量,來達到更好的平滑。
~~~
/*
信號平滑
不斷讀取模擬信號值、求取平均值后輸出到電腦。將10次數據存儲到數組并且不斷計算平均數。
電路連接:
* 模擬信號傳感器(電位器即可)連接到A0
代碼是公開的.
*/
// 保存的數據個數越多,平滑效果就越好,不過每次計算時間就越長。
//注意:應該使用常量來定義數組容量,而非變量
// 我們且將元素個數限制為10
const int numReadings = 10;
int readings[numReadings]; // 存儲數組
int readIndex = 0; // 當前讀到的位置(數組下標)
int total = 0; // 和
int average = 0; // 平均值
int inputPin = A0;
void setup() {
// 初始化和電腦的串口連接:
Serial.begin(9600);
// 初始化數組,用0填充:
for (int thisReading = 0; thisReading < numReadings; thisReading++) {
readings[thisReading] = 0;
}
}
void loop() {
// 總和扣除上次的值:
total = total - readings[readIndex];
// 從傳感器讀新值:
readings[readIndex] = analogRead(inputPin);
// 將新值加到總和:
total = total + readings[readIndex];
// 下表加一:
readIndex = readIndex + 1;
// 如果在數組的最后
if (readIndex >= numReadings) {
// 從頭開始:
readIndex = 0;
}
// 計算平均值:
average = total / numReadings;
// 作為ASCII字符傳到電腦
Serial.println(average);
delay(1); // 為穩定,延遲1毫秒。
}
~~~
### **相關資料**
[array](https://www.arduino.cc/en/Reference/Array)?
[if](https://www.arduino.cc/en/Reference/If)?
[for](https://www.arduino.cc/en/Reference/For)?
[serial](https://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