## **If條件判斷**
**條件判斷結構**是編程中的最基本結構,在Arduino和其他語言中往往用**if()**語句實現條件判斷。**if()**讓你能夠根據某個**條件**(**Condition**)的真假執行不同的代碼。(條件只有兩種可能,要么是**真**(**true**)要么是**假**(**false**))if語句的最簡單形式如下:
~~~
if (someCondition) {
// someCondition為 真 的時候處理這個花括號里的語句
}
~~~
If語句的完整形式如下:
~~~
if (someCondition) {
// someCondition為 真 的時候處理這個花括號里的語句
} else {
// someCondition為 假 的時候處理這個花括號里的語句
}
~~~
還可以使用**if-else**嵌套多個if條件語句。使用**if-else**就可以在只有第一個條件為false(假)時才繼續檢查其他條件,如果第一個為true(真)就不檢查了:
~~~
if (someCondition) {
// someCondition是 真 的時候處理這個花括號里的語句
} else if (anotherCondition) {
// someCondition是 假,并且anotherCondition是 真 的時候處理這個花括號里的語句
}
~~~
你可以隨時隨地使用if語句。下面的代碼將在A0口模擬信號值大于臨界值的時候點亮LED。本例使用大多數Arduino板上的板載LED(內部連接到13引腳)。
### **所需硬件**
* Arduino板或Genuino板
* 電位器或者可變電阻
* 連接線
* 電路連接

### **原理圖**

### **代碼**
在下列代碼中,**analogValue?**變量用來存儲電位器的模擬信號值(電位器在A0口)。analogValue的值將與臨界值(用**threshold**變量存儲)進行比較。如果大于臨界值就將13號引腳上的板載LED打開,如果小于臨界值就將它關閉。
~~~
/*
If條件判斷
本例展示if()語句的使用。代碼將讀取電位器的值,如果超過臨界值就把LED打開,否則就將LED關閉。程序會把電位器的值輸出到電腦。
電路搭建:
* 電位器中間引腳連接到A0。兩側的引腳分別連接到+5V和GND。
* LED連接13號引腳到GND
* 提示: 在大多數Arduino板上有13號引腳連接的板載LED,因此你一般不許要多加LED。
代碼公開。
*/
// 常量定義:
const int analogPin = A0; // 電位器引腳
const int ledPin = 13; // LED引腳
const int threshold = 400; // 模擬信號臨界值
void setup() {
// 初始化LED引腳為輸出模式:
pinMode(ledPin, OUTPUT);
// 初始化串口通信:
Serial.begin(9600);
}
void loop() {
// 讀取電位器值:
int analogValue = analogRead(analogPin);
// 模擬信號值是否超過臨界:
if (analogValue > threshold) {
digitalWrite(ledPin, HIGH);
} else {
digitalWrite(ledPin, LOW);
}
// 打印模擬信號值:
Serial.println(analogValue);
delay(1); // 為了穩定性,延遲1毫秒
}
~~~
### **相關資料**
[if()](https://www.arduino.cc/en/Reference/If)?
[if…else](https://www.arduino.cc/en/Reference/Else)?
[analogRead()](https://www.arduino.cc/en/Reference/AnalogRead)?
[digitalWrite()](https://www.arduino.cc/en/Reference/DigitalWrite)?
[serial.begin()](https://www.arduino.cc/en/Serial/Begin)?
[serial.print()](https://www.arduino.cc/en/Serial/Print)?
[原文鏈接](http://www.arduino.cc/en/Tutorial/IfStatement)
- 說明
- 系統示例文件目錄結構及說明
- 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