## **模擬信號的校準**
> 本例向你展示校準傳感器的一個技巧:在啟動時先讀取傳感器值5秒鐘,然后尋找其最大、最小值。校準后的最大、最小值將在map函數有用。
### **所需硬件**
* Arduino板或Genuino板
* LED 模擬信號的傳感器(光敏電阻就OK)
* 10kΩ電阻
* 220Ω電阻
* 跳線
* 面包板
* 杜邦線/面包板線
### **電路**

模擬信號傳感器(例如:電位器/光線傳感器等)連接到A2,LED連接到9號引腳。
用220Ω限流電阻串聯LED到9號引腳。將光敏電阻一腳連接到5V,另一個腳連接到A0,并加一個10kΩ下拉電阻。
### **原理圖**

### **代碼**
在setup前,你應像這樣定義初始的最大最小值:
~~~
int sensorMin = 1023; // 最小值
int sensorMax = 0; // 最大值
~~~
這種方式可能看起來有點初級,先學習下吧。?
最開始,你設置一個最小值初始值。如果讀取到任何小于它的值,就用這個更小的數字更新最小值。與此類似,最開始,你設置一個最大值初始值,如果讀取到任何大它的值,就用這個更大的數字更新最大值。像這樣寫:
~~~
// 用最初五秒進行校準
while (millis() < 5000) {
sensorValue = analogRead(sensorPin);
// 記錄傳感器最大值
if (sensorValue > sensorMax) {
sensorMax = sensorValue;
}
// 記錄傳感器最小值
if (sensorValue < sensorMin) {
sensorMin = sensorValue;
}
}
~~~
通過這種方式,傳感器的值就能夠被成功映射到一個范圍(因為最大、最小值確定了):
~~~
// 將校準結果應用到傳感器讀取中
sensorValue = map(sensorValue, sensorMin, sensorMax, 0, 255);
~~~
下面是整個代碼:
~~~
/*
模擬信號的校準
展示校準傳感器的一種方式。在程序執行前五秒時會找到傳感器值的最大、最小值。
這種方式可能看起來有點初級,先學習下吧。
最開始你設置一個最大值和最小值的初始值,然后在執行過程中尋找更小的值和更大的值并且不斷更新最開始設置的值。
電路搭建:
* 模擬信號傳感器(光敏電阻就可以)連接到A0
* LED連接9號引腳到GND
代碼是公開的.
*/
// 常量:
const int sensorPin = A0; // 傳感器引腳
const int ledPin = 9; // LED引腳
// 變量:
int sensorValue = 0; // 傳感器值
int sensorMin = 1023; // 最小值
int sensorMax = 0; // 最大值
void setup() {
// 打開LED以表示校準開始:
pinMode(13, OUTPUT);
digitalWrite(13, HIGH);
// 在最初五秒進行校準
while (millis() < 5000) {
sensorValue = analogRead(sensorPin);
// 記錄最大值
if (sensorValue > sensorMax) {
sensorMax = sensorValue;
}
// 記錄最小值
if (sensorValue < sensorMin) {
sensorMin = sensorValue;
}
}
// 關閉LED表示校準完成
digitalWrite(13, LOW);
}
void loop() {
// 讀取傳感器:
sensorValue = analogRead(sensorPin);
// 將校準后的最大最小值用進去:
sensorValue = map(sensorValue, sensorMin, sensorMax, 0, 255);
// 防止sensorValue超過范圍
sensorValue = constrain(sensorValue, 0, 255);
// 用校準后的值來給LED調光:
analogWrite(ledPin, sensorValue);
}
~~~
相關資料
[while()](https://www.arduino.cc/en/Reference/While)?
[millis()](https://www.arduino.cc/en/Reference/Millis)?
[constrain()](https://www.arduino.cc/en/Reference/Constrain)?
[map()](https://www.arduino.cc/en/Reference/Map)?
[If](https://www.arduino.cc/en/Reference/If)?
- 說明
- 系統示例文件目錄結構及說明
- 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