## **While循環**
> 有時你可能需要進行這樣的操作:如果某個條件為**true**的話就一直停在那里,直到它由true變成false才繼續執行(反過來也可以,開始為false,直到true才繼續)。你可以使用while循環來做到這一點。本例向你展示如何使用while循環來校準模擬信號傳感器。
在**loop中**,我們讀取A0口的值,來給9號引腳上的LED調光。 但是當2號引腳上的按鍵被按下時,程序就會去執行**calibrate()**?函數(函數名意思叫 校準,這是自己定義的函數)。**calibrate()**會尋找模擬信號的最大、最小值。當你松開按鍵時,loop()中的其他代碼才會繼續被執行。
本例所展示的技巧能夠讓你在環境光線發生明顯改變時進行人工校準。
### **所需硬件**
* Arduino板或Genuino板
* 按鍵或開關
* 光敏電阻或其他模擬信號傳感器
* 2個10kΩ電阻
* 面包板
* 連接線
### **電路**
?
連接模擬信號傳感器(例如:光敏電阻) 到A2口,并且加一個10kΩ下拉電阻。連接按鍵到數字引腳,并且加一個10kΩ下拉電阻。LED和220Ω電阻串接后連接到9號引腳。
### **原理圖**

### **代碼**
~~~
/*
While循環
本例向你展示while()的使用。
當按鍵被按下時,工程將會啟動開始校準。
while循環中獲得的傳感器數據講被取最大、最小值,然后對光敏電阻進行校準。
這是校準模擬信號的另一個例子,之前也介紹過一種方法,不過這次重在演示while循環的使用。
電路搭建:
* 光敏電阻連接+5V 和A0口
* 10kΩ電阻連接GND和A0口
* LED與220Ω電阻串聯,并接到9號引腳
* 按鍵連接到A2口和+5V
* 10KΩ電阻連接到A2口到GND
代碼公開。
*/
// 常亮定義:
const int sensorPin = A2; // 傳感器引腳
const int ledPin = 9; // LED引腳
const int indicatorLedPin = 13; // 板載LED引腳
const int buttonPin = 2; // 按鍵引腳
// 變量:
int sensorMin = 1023; // 傳感器最小值
int sensorMax = 0; // 傳感器最大值
int sensorValue = 0; // 傳感器值
void setup() {
// 將indicatorLedPin設置為OUTPUT模式:
pinMode(indicatorLedPin, OUTPUT);
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT);
}
void loop() {
// 當按鍵被按下時進行校準:
while (digitalRead(buttonPin) == HIGH) {
calibrate();
}
// 關閉LED,標志校準結束
digitalWrite(indicatorLedPin, LOW);
// 讀取傳感器值:
sensorValue = analogRead(sensorPin);
// 運用最大、最小值來校準傳感器值
sensorValue = map(sensorValue, sensorMin, sensorMax, 0, 255);
// 放置sensorValue超過上下限(正常最小0,最大255)
sensorValue = constrain(sensorValue, 0, 255);
// 使用sensorValue給LED調光:
analogWrite(ledPin, sensorValue);
}
void calibrate() {
// 將LED打開,標示校準過程的開始:
digitalWrite(indicatorLedPin, HIGH);
// 讀取傳感器值:
sensorValue = analogRead(sensorPin);
// 記錄最大值
if (sensorValue > sensorMax) {
sensorMax = sensorValue;
}
// 記錄最小值
if (sensorValue < sensorMin) {
sensorMin = sensorValue;
}
}
~~~
### **相關資料**
[while()](https://www.arduino.cc/en/Reference/While)?
[digitalRead()](https://www.arduino.cc/en/Reference/DigitalRead)?
[digitalWrite()](https://www.arduino.cc/en/Reference/DigitalWrite)?
[analogRead()](https://www.arduino.cc/en/Reference/AnalogRead)?
[analogWrite()](https://www.arduino.cc/en/Reference/AnalogWrite)?
[map()](https://www.arduino.cc/en/Reference/Map)?
[constrain()](https://www.arduino.cc/en/Reference/Constrain)?
[if()](https://www.arduino.cc/en/Reference/If)?
[原文鏈接](http://www.arduino.cc/en/Tutorial/WhileLoop)
- 說明
- 系統示例文件目錄結構及說明
- 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