Tutorial.IfStatement History

Show minor edits - Show changes to markup

May 02, 2012, at 04:08 PM by Scott Fitzgerald -
Changed line 77 from:
to:
November 16, 2011, at 04:16 AM by Scott Fitzgerald -
Changed line 77 from:
to:
September 30, 2011, at 03:18 AM by Scott Fitzgerald -
Changed line 77 from:
to:
September 28, 2010, at 01:30 PM by Tom Igoe -
Added lines 72-74:

In the code below, a variable called analogValue is used to store the data collected from a potentiometer connected to the Arduino on analogPin 0. This data is then compared to a threshold value. If the analog value is found to be above the set threshold the LED connected to digital pin 13 is turned on. If analogValue is found to be < threshold, the LED remains off.

Deleted lines 76-77:

In the code below, a variable called analogValue is used to store the data collected from a potentiometer connected to the Arduino on analogPin 0. This data is then compared to a threshold value. If the analog value is found to be above the set threshold the LED connected to digital pin 13 is turned on. If analogValue is found to be < threshold, the LED remains off.

September 23, 2010, at 10:38 PM by Christian Cerrito -
Added line 5:
September 23, 2010, at 10:38 PM by Christian Cerrito -
Changed lines 3-5 from:

If Statement

(Conditional Statement)

to:

If Statement (Conditional Statement)

September 19, 2010, at 10:06 PM by Christian Cerrito -
Changed lines 1-2 from:

''Examples > Control Structures'

to:

''Examples > Control Structures"

September 17, 2010, at 11:38 PM by Christian Cerrito -
Changed lines 6-8 from:

The if() statement is the most basic of all programming control structures. It allows you to make something happen or not depending on whether a given condition is true or not. It looks like this:

to:

The if() statement is the most basic of all programming control structures. It allows you to make something happen or not depending on whether a given condition is true or not. It looks like this:

September 17, 2010, at 10:43 PM by Tom Igoe -
Changed line 84 from:
  • [[Reference/AnalogRead | analogRead()]
to:
September 17, 2010, at 10:42 PM by Tom Igoe -
Changed lines 82-101 from:
to:
  • if()
  • if...else
  • [[Reference/AnalogRead | analogRead()]
  • digitalWrite()
  • serial.begin()
  • serial.print()

  • For Loop - Control multiple LEDs with a For Loop.
  • While Loop - Use a While Loop to calibrate a sensor while a button is being pressed.
  • Switch Case - Choose between a number of discrete values in a manner that is the equivalent of using multiples If statements. This example shows how to divide a sensor's range into a set of four bands and to take four different actions depending on which band the result is in.
September 17, 2010, at 10:39 PM by Tom Igoe -
Changed lines 8-10 from:

[@

to:
Changed lines 14-15 from:

@]

to:
Changed lines 20-21 from:

[@

to:
Changed lines 27-28 from:

@]

to:
Changed lines 33-34 from:

[@

to:
Changed lines 41-42 from:

@]

to:
Changed lines 83-92 from:
  • if...else
  • analogRead()
  • digitalWrite()
  • serial.begin()
  • serial.print()

  • For Loop? - Control multiple LEDs with a For Loop.
  • While Loop? - Use a While Loop to calibrate a sensor while a button is being pressed.
  • Switch Case? - Choose between a number of discrete values in a manner that is the equivalent of using multiples If statements. This example shows how to divide a sensor's range into a set of four bands and to take four different actions depending on which band the result is in.
  • Array: a variation on the For Loop example that demonstrates how to use an array.
to:
September 16, 2010, at 10:16 PM by Tom Igoe -
Changed line 66 from:
to:
September 16, 2010, at 08:04 AM by Christian Cerrito -
Changed lines 6-7 from:

The * [[Reference/if | if()]statement is the most basic of all programming control structures. It allows you to make something happen or not depending on whether a given condition is true or not. It looks like this:

to:

The if() statement is the most basic of all programming control structures. It allows you to make something happen or not depending on whether a given condition is true or not. It looks like this:

September 16, 2010, at 08:04 AM by Christian Cerrito -
Changed lines 6-7 from:

The If statement is the most basic of all programming control structures. It allows you to make something happen or not depending on whether a given condition is true or not. It looks like this:

to:

The * [[Reference/if | if()]statement is the most basic of all programming control structures. It allows you to make something happen or not depending on whether a given condition is true or not. It looks like this:

September 16, 2010, at 05:19 AM by Christian Cerrito -
Changed line 64 from:

In the code below, a variable called analogValue is used to store the data collected from a potentiometer connected to the Arduino on analogPin 0. This data is then compared to a threshold value. If the analog value is found to be above the set threshold the LED connected to digital pin 13 is turned on. If analogValue is found to be< threshold, the LED remains off.

to:

In the code below, a variable called analogValue is used to store the data collected from a potentiometer connected to the Arduino on analogPin 0. This data is then compared to a threshold value. If the analog value is found to be above the set threshold the LED connected to digital pin 13 is turned on. If analogValue is found to be < threshold, the LED remains off.

September 16, 2010, at 05:18 AM by Christian Cerrito -
Changed line 64 from:

In the code below, a variable called analogValue is used to store the data collected from a potentiometer connected to the Arduino on analogPin 0. This data is then compared to a threshold value. If the analogValue is found to be above the set threshold and an LED attached to digital pin 13 is turned on. If analogValue < threhold, the LED remains off.

to:

In the code below, a variable called analogValue is used to store the data collected from a potentiometer connected to the Arduino on analogPin 0. This data is then compared to a threshold value. If the analog value is found to be above the set threshold the LED connected to digital pin 13 is turned on. If analogValue is found to be< threshold, the LED remains off.

September 16, 2010, at 05:17 AM by Christian Cerrito -
Added lines 63-64:

In the code below, a variable called analogValue is used to store the data collected from a potentiometer connected to the Arduino on analogPin 0. This data is then compared to a threshold value. If the analogValue is found to be above the set threshold and an LED attached to digital pin 13 is turned on. If analogValue < threhold, the LED remains off.

September 16, 2010, at 05:11 AM by Christian Cerrito -
Changed lines 77-80 from:
  • Serial Call Response - send multiple variables using a call and response (handshaking) method.
  • Serial Call and Response ASCII - send multiple vairables using a call-and-response (handshaking) method, and ASCII-encoding the values before sending.
  • If Statement - how to use an if statement to change output conditions based on changing input conditions.
  • For Loop: - controlling multiple LEDs with a for loop.
to:
  • For Loop? - Control multiple LEDs with a For Loop.
  • While Loop? - Use a While Loop to calibrate a sensor while a button is being pressed.
  • Switch Case? - Choose between a number of discrete values in a manner that is the equivalent of using multiples If statements. This example shows how to divide a sensor's range into a set of four bands and to take four different actions depending on which band the result is in.
Deleted lines 80-81:
  • While Loop: how to use a while loop to calibrate a sensor while a button is being read.
  • Switch Case: how to choose between a discrete number of values. Equivalent to multiple If statements. This example shows how to divide a sensor's range into a set of four bands and to take four different actions depending on which band the result is in.
September 16, 2010, at 03:10 AM by Christian Cerrito -
Changed lines 75-76 from:
to:
September 16, 2010, at 03:10 AM by Christian Cerrito -
Added lines 67-83:

See Also:

  • if()
  • if...else
  • analogRead()
  • digitalWrite()
  • serial.begin()
  • serial.write()

  • Serial Call Response - send multiple variables using a call and response (handshaking) method.
  • Serial Call and Response ASCII - send multiple vairables using a call-and-response (handshaking) method, and ASCII-encoding the values before sending.
  • If Statement - how to use an if statement to change output conditions based on changing input conditions.
  • For Loop: - controlling multiple LEDs with a for loop.
  • Array: a variation on the For Loop example that demonstrates how to use an array.
  • While Loop: how to use a while loop to calibrate a sensor while a button is being read.
  • Switch Case: how to choose between a discrete number of values. Equivalent to multiple If statements. This example shows how to divide a sensor's range into a set of four bands and to take four different actions depending on which band the result is in.
September 16, 2010, at 03:00 AM by Christian Cerrito -
Changed lines 57-60 from:
to:

September 16, 2010, at 02:53 AM by Christian Cerrito -
Changed lines 57-60 from:

to:
September 16, 2010, at 02:52 AM by Christian Cerrito -
Changed lines 48-49 from:
to:
September 16, 2010, at 02:51 AM by Christian Cerrito -
September 16, 2010, at 02:49 AM by Christian Cerrito -
Changed lines 48-49 from:
to:
September 16, 2010, at 02:34 AM by Christian Cerrito -
Changed line 3 from:

If Statement

to:

If Statement

September 16, 2010, at 02:34 AM by Christian Cerrito -
Changed lines 1-5 from:

Examples > Control Structures

If Statement

(Conditional Statement)

to:

''Examples > Control Structures'

If Statement

(Conditional Statement)

Changed line 66 from:
to:
September 16, 2010, at 02:33 AM by Christian Cerrito -
Changed lines 3-4 from:

If Statement (Conditional Statement)

to:

If Statement

(Conditional Statement)

September 16, 2010, at 02:33 AM by Christian Cerrito -
Changed lines 36-42 from:
to:

Hardware Required

  • Arduino Board
  • (1) Potentiometer or variable resistor
  • (1) 220 ohm resistor
  • (1) LED
  • hook-up wire
September 16, 2010, at 02:28 AM by Christian Cerrito -
Changed lines 3-4 from:

If Statement (Conditional Statement)

to:

If Statement (Conditional Statement)

September 15, 2010, at 11:27 PM by Christian Cerrito -
Changed line 57 from:
to:
February 24, 2010, at 04:05 AM by Tom Igoe -
Changed line 57 from:
to:
February 24, 2010, at 04:04 AM by Tom Igoe -
Changed lines 57-84 from:
 /*
   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 LED goes above a certain threshold level. It prints the analog value
  regardless of the level.
  
  The circuit:
  * potentiometer connected to analog pin 0.
  Center pin of the potentiometer goes to the analog pin.
  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
  by Tom Igoe
  
   http://arduino.cc/en/Tutorial/IfStatement
  
  */

 // These constants won't change:
 const int analogPin = 0;     // 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
to:
Deleted lines 58-82:
 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, DEC);

 }

October 18, 2009, at 09:41 PM by David A. Mellis -
Changed line 88 from:
   pinMode(LED, OUTPUT);
to:
   pinMode(ledPin, OUTPUT);
August 27, 2009, at 08:47 PM by Tom Igoe -
Added lines 43-45:

image developed using Fritzing. For more circuit examples, see the Fritzing project page

July 05, 2009, at 07:25 PM by Tom Igoe -
Changed lines 52-55 from:

[@ /*

  Conditionals - If statement
to:
Changed lines 83-86 from:
 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 LED goes above a certain threshold level. It prints the analog value
 regardless of the level.
to:
 void setup() {
   // initialize the LED pin as an output:
   pinMode(LED, OUTPUT);
   // initialize serial communications:
   Serial.begin(9600);
 }
Changed lines 90-94 from:
 The circuit:
 * potentiometer connected to analog pin 0.
 Center pin of the potentiometer goes to the analog pin.
 side pins of the potentiometer go to +5V and ground
 * LED connected from digital pin 13 to ground
to:
 void loop() {
   // read the value of the potentiometer:
   int analogValue = analogRead(analogPin);
Changed lines 94-95 from:
 * 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.
to:
   // if the analog value is high enough, turn on the LED:
   if (analogValue > threshold) {
     digitalWrite(ledPin, HIGH);
   } 
   else {
     digitalWrite(ledPin,LOW); 
   }
Changed lines 102-103 from:
 created 17 Jan 2009
 by Tom Igoe
to:
   // print the analog value:
   Serial.println(analogValue, DEC);
Changed line 105 from:
  http://arduino.cc/en/Tutorial/IfStatement
to:
 }
Deleted line 106:
 */
Changed lines 108-136 from:

// These constants won't change: const int analogPin = 0; // 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(LED, 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, DEC);

} @]

to:
June 25, 2009, at 09:36 PM by Tom Igoe -
Changed lines 47-50 from:

to:

June 25, 2009, at 09:35 PM by Tom Igoe -
Changed lines 47-50 from:

to:

June 25, 2009, at 09:32 PM by Tom Igoe -
Changed lines 47-50 from:

to:

June 25, 2009, at 09:31 PM by Tom Igoe -
Changed lines 47-50 from:

to:

June 25, 2009, at 09:30 PM by Tom Igoe -
Changed lines 47-50 from:

to:

June 25, 2009, at 09:30 PM by Tom Igoe -
June 25, 2009, at 09:15 PM by Tom Igoe -
Added lines 1-106:

Examples > Control Structures

If Statement (Conditional Statement)

The If statement is the most basic of all programming control structures. It allows you to make something happen or not depending on whether a given condition is true or not. It looks like this:

if (someCondition) {
   // do stuff if the condition is true
}

There is a common variation called if-else that looks like this:

if (someCondition) {
   // do stuff if the condition is true
} else {
   // do stuff if the condition is false
}

There's also the else-if, where you can check a second condition if the first is false:

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
}

You'll use if statements all the time. The example below turns on an LED on pin 13 (the built-in LED on many Arduino boards) if the value read on an analog input goes above a certain threshold.

Circuit

click the image to enlarge

Schematic:

click the image to enlarge

Code

/*
  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 LED goes above a certain threshold level. It prints the analog value
 regardless of the level.

 The circuit:
 * potentiometer connected to analog pin 0.
 Center pin of the potentiometer goes to the analog pin.
 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
 by Tom Igoe

  http://arduino.cc/en/Tutorial/IfStatement

 */

// These constants won't change:
const int analogPin = 0;     // 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(LED, 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, DEC);

}

Share