Eğitimler
ifStatementConditional
İf () ifadesi, tüm programlama kontrol yapılarının en temelidir. Belirli bir koşulun doğru olup olmamasına bağlı olarak bir şey yapmanıza veya yapmanıza izin verir. Şöyle görünüyor:
if (someCondition) { // do stuff if the condition is true }
İf-else adı verilen ve şöyle görünen ortak bir varyasyon vardır:
if (someCondition) { // do stuff if the condition is true } else { // do stuff if the condition is false }
Birincisi yanlışsa ikinci bir koşulu kontrol edebileceğiniz else-if da vardır:
if (someCondition) { // do stuff if the condition is true } else if (anotherCondition) { // do stuff only if the first condition is false // and the second condition is true }
İf ifadelerini her zaman kullanacaksınız. Aşağıdaki örnek, bir analog girişte okunan değer belirli bir eşiğin üzerine çıkarsa, pim 13 üzerindeki bir LED'i (birçok Arduino kartındaki yerleşik LED) açar.
Gerekli Donanım
- Arduino veya Genuino Kurulu
- Potansiyometre veya değişken direnç
Devre
büyütmek için resme tıklayın

Şematik
büyütmek için resme tıklayın

Kod
Aşağıdaki kodda, anakartta analogPin 0'a bağlı bir potansiyometreden toplanan verileri depolamak için analogValue
adlı bir değişken kullanılır. Bu veriler daha sonra bir eşik değeri ile karşılaştırılır. Analog değerin ayarlanan eşiğin üstünde olduğu tespit edilirse, dijital pim 13'e bağlı dahili LED yanar. AnalogValue değerinin <
(küçük) eşik olduğu bulunursa, LED kapalı kalır.
/*
Conditionals - If statement
This example demonstrates the use of if() statements.
It reads the state of a potentiometer (an analog input) and turns on an LED
only if the potentiometer goes above a certain threshold level. It prints the
analog value regardless of the level.
The circuit:
- potentiometer
Center pin of the potentiometer goes to analog pin 0.
Side pins of the potentiometer go to +5V and ground.
- LED connected from digital pin 13 to ground
- Note: On most Arduino boards, there is already an LED on the board connected
to pin 13, so you don't need any extra components for this example.
created 17 Jan 2009
modified 9 Apr 2012
by Tom Igoe
This example code is in the public domain.
http://www.arduino.cc/en/Tutorial/IfStatement
*/
// These constants won't change:
const int analogPin = A0; // pin that the sensor is attached to
const int ledPin = 13; // pin that the LED is attached to
const int threshold = 400; // an arbitrary threshold level that's in the range of the analog input
void setup() {
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize serial communications:
Serial.begin(9600);
}
void loop() {
// read the value of the potentiometer:
int analogValue = analogRead(analogPin);
// if the analog value is high enough, turn on the LED:
if (analogValue > threshold) {
digitalWrite(ledPin, HIGH);
} else {
digitalWrite(ledPin, LOW);
}
// print the analog value:
Serial.println(analogValue);
delay(1); // delay in between reads for stability
}
See Also
- if()
- if...else
- analogRead()
- digitalWrite()
- serial.begin()
- serial.print()
- Arrays- Loop için bir dizinin nasıl kullanılacağını gösteren bir varyasyon.
- ForLoopIteration- for döngüsü ile birden fazla LED'i kontrol edin.
- switchCase- Farklı sayıda değer arasında seçim yapma.
- switchCase2- Seri bağlantı noktasında alınan karakterlere göre farklı eylemlerin nasıl yapılacağını gösteren ikinci bir anahtar durum örneği.
- WhileStatementConditional- Bir düğme okunurken bir sensörü kalibre etmek için while döngüsü nasıl kullanılır?