Tutorial.IfStatement History

Hide minor edits - Show changes to output

May 02, 2012, at 04:08 PM by Scott Fitzgerald -
Changed line 77 from:
(:source http://arduino.cc/en/pub/code/master/build/shared/examples/5.Control/IfStatementConditional/IfStatementConditional.ino lang=arduino tabwidth=4:)
to:
(:source http://arduino.cc/en/pub/code/master/build/shared/examples/05.Control/IfStatementConditional/IfStatementConditional.ino lang=arduino tabwidth=4:)
November 16, 2011, at 04:16 AM by Scott Fitzgerald -
Changed line 77 from:
(:source http://arduino.cc/en/pub/code/new-extension/build/shared/examples/5.Control/IfStatementConditional/IfStatementConditional.ino lang=arduino tabwidth=4:)
to:
(:source http://arduino.cc/en/pub/code/master/build/shared/examples/5.Control/IfStatementConditional/IfStatementConditional.ino lang=arduino tabwidth=4:)
September 30, 2011, at 03:18 AM by Scott Fitzgerald -
Changed line 77 from:
(:source http://arduino.cc/en/pub/code/master/build/shared/examples/5.Control/IfStatementConditional/IfStatementConditional.pde lang=arduino tabwidth=4:)
to:
(:source http://arduino.cc/en/pub/code/new-extension/build/shared/examples/5.Control/IfStatementConditional/IfStatementConditional.ino lang=arduino tabwidth=4:)
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 [[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 [[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 17, 2010, at 10:43 PM by Tom Igoe -
Changed line 84 from:
* [[Reference/AnalogRead | analogRead()]
to:
* [[Reference/AnalogRead | analogRead()]]
September 17, 2010, at 10:42 PM by Tom Igoe -
Changed lines 82-101 from:
* [[Reference/if | @@if()@@ ]]
* [[Reference/Else| @@if...else@(:sourceend:)

]
* [[Reference/AnalogRead | @@analogRead()@(:sourceend:)

]
* [[Reference/digitalWrite | @@digitalWrite()@(:sourceend:)

]
* [[Serial/Begin| @@serial.begin()@(:sourceend:)

]
* [[Serial/Print | @@serial.print()@(:sourceend:)

]

* [[Forloop | For Loop]] - Control multiple LEDs with a For Loop.
* [[Whileloop | While Loop]] - Use a While Loop to calibrate a sensor while a button is being pressed.
* [[Switchcase | 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.
to:
* [[Reference/if | if() ]]
* [[Reference/Else| if...else]]
* [[Reference/AnalogRead | analogRead()]
* [[Reference/digitalWrite | digitalWrite()]]
* [[Serial/Begin| serial.begin()]]
* [[Serial/Print | serial.print()]]

* [[ForLoop | For Loop]] - Control multiple LEDs with a For Loop.
* [[WhileLoop | While Loop]] - Use a While Loop to calibrate a sensor while a button is being pressed.
* [[SwitchCase | 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:

(:source lang=arduino tabwidth=4:)
Changed lines 14-15 from:
@]
to:
(:sourceend:)


Changed lines 20-21 from:
[@
to:
(:source lang=arduino tabwidth=4:)
Changed lines 27-28 from:
@]
to:
(:sourceend:)


Changed lines 33-34 from:
[@
to:
(:source lang=arduino tabwidth=4:)
Changed lines 41-42 from:
@]
to:
(:sourceend:)


Changed lines 83-92 from:
* [[Reference/Else| @@if...else@@]]
* [[Reference/AnalogRead | @@analogRead()@@]]
* [[Reference/digitalWrite | @@digitalWrite()@@]]
* [[Serial/Begin| @@serial.begin()@@]]
* [[Serial/Print | @@serial.print()@@]]

* [[Tutorial/Forloop | For Loop]] - Control multiple LEDs with a For Loop.
* [[Tutorial/Whileloop | While Loop]] - Use a While Loop to calibrate a sensor while a button is being pressed.
* [[Tutorial/Switchcase | 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.
* [[Tutorial/Array]]: a variation on the For Loop example that demonstrates how to use an array.
to:
* [[Reference/Else| @@if...else@(:sourceend:)

]
* [[Reference/AnalogRead | @@analogRead()@(:sourceend:)

]
* [[Reference/digitalWrite | @@digitalWrite()@(:sourceend:)

]
* [[Serial/Begin| @@serial.begin()@(:sourceend:)

]
* [[Serial/Print | @@serial.print()@(:sourceend:)

]

* [[Forloop | For Loop]] - Control multiple LEDs with a For Loop.
* [[Whileloop | While Loop]] - Use a While Loop to calibrate a sensor while a button is being pressed.
* [[Switchcase | 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.
September 16, 2010, at 10:16 PM by Tom Igoe -
Changed line 66 from:
(:source http://arduino.cc/en/pub/code/master/build/shared/examples/5.Control/IfStatementConditional/IfStatementConditional.pde language=arduino tabwidth=4:)
to:
(:source http://arduino.cc/en/pub/code/master/build/shared/examples/5.Control/IfStatementConditional/IfStatementConditional.pde lang=arduino tabwidth=4:)
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 [[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 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:
* [[Tutorial/SerialCallResponse | Serial Call Response]] - send multiple variables using a call and response (handshaking) method.
* [[Tutorial/SerialCallResponseASCII | Serial Call and Response ASCII]] - send multiple vairables using a call-and-response (handshaking) method, and ASCII-encoding the values before sending.
* [[Tutorial/IfStatement | If Statement]] - how to use an if statement to change output conditions based on changing input conditions.
* [[Tutorial/ForLoop | For Loop]]: - controlling multiple LEDs with a for loop.
to:
* [[Tutorial/Forloop | For Loop]] - Control multiple LEDs with a For Loop.
* [[Tutorial/Whileloop | While Loop]] - Use a While Loop to calibrate a sensor while a button is being pressed.
* [[Tutorial/Switchcase | 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:
* [[Tutorial/While Loop]]: how to use a while loop to calibrate a sensor while a button is being read.
* [[Tutorial/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:
* [[Serial/Write | @@serial.write()@@]]
to:
* [[Serial/Print | @@serial.print()@@]]
September 16, 2010, at 03:10 AM by Christian Cerrito -
Added lines 67-83:

!!!See Also:

* [[Reference/if | @@if()@@ ]]
* [[Reference/Else| @@if...else@@]]
* [[Reference/AnalogRead | @@analogRead()@@]]
* [[Reference/digitalWrite | @@digitalWrite()@@]]
* [[Serial/Begin| @@serial.begin()@@]]
* [[Serial/Write | @@serial.write()@@]]

* [[Tutorial/SerialCallResponse | Serial Call Response]] - send multiple variables using a call and response (handshaking) method.
* [[Tutorial/SerialCallResponseASCII | Serial Call and Response ASCII]] - send multiple vairables using a call-and-response (handshaking) method, and ASCII-encoding the values before sending.
* [[Tutorial/IfStatement | If Statement]] - how to use an if statement to change output conditions based on changing input conditions.
* [[Tutorial/ForLoop | For Loop]]: - controlling multiple LEDs with a for loop.
* [[Tutorial/Array]]: a variation on the For Loop example that demonstrates how to use an array.
* [[Tutorial/While Loop]]: how to use a while loop to calibrate a sensor while a button is being read.
* [[Tutorial/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:
%width=400px%[[Attach:ifstatment_sch.png | Attach:ifstatement_sch.png]]


to:
%width=400px%[[Attach:ifStatement_sch.png | Attach:ifStatement_sch.png]]


September 16, 2010, at 02:53 AM by Christian Cerrito -
Changed lines 57-60 from:
%width=400px%[[Attach:if_schem.png | Attach:if_schem.png]]


to:
%width=400px%[[Attach:ifstatment_sch.png | Attach:ifstatement_sch.png]]


September 16, 2010, at 02:52 AM by Christian Cerrito -
Changed lines 48-49 from:
%width=400px%[[Attach:ifstatement_sch.png | Attach:ifstatement_sch.png]]
to:
%width=400px%[[Attach:if.png | Attach:if.png]]
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:
%width=400px%[[Attach:if.png | Attach:if.png]]
to:
%width=400px%[[Attach:ifstatement_sch.png | Attach:ifstatement_sch.png]]
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:
(:divend:)
to:
(:divend:)
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:
(:source http://arduino.cc/en/pub/code/master/build/shared/examples/Control/IfStatementConditional/IfStatementConditional.pde language=arduino tabwidth=4:)
to:
(:source http://arduino.cc/en/pub/code/master/build/shared/examples/5.Control/IfStatementConditional/IfStatementConditional.pde language=arduino tabwidth=4:)
February 24, 2010, at 04:05 AM by Tom Igoe -
Changed line 57 from:
(:source http://arduino.cc/en/pub/code/master/build/shared/examples/Control/ifStatementConditional/ifStatementConditional.pde language=arduino tabwidth=4:)
to:
(:source http://arduino.cc/en/pub/code/master/build/shared/examples/Control/IfStatementConditional/IfStatementConditional.pde language=arduino tabwidth=4:)
February 24, 2010, at 04:04 AM by Tom Igoe -
Changed lines 57-84 from:
%color=#7e7e7e%/*
%color=#7e7e7e% Conditionals - If statement
%color=#7e7e7e%
%color=#7e7e7e% This example demonstrates the use of if() statements.
%color=#7e7e7e% It reads the state of a potentiometer (an analog input) and turns on an LED
%color=#7e7e7e% only if the LED goes above a certain threshold level. It prints the analog value
%color=#7e7e7e% regardless of the level.
%color=#7e7e7e%
%color=#7e7e7e% The circuit:
%color=#7e7e7e% * potentiometer connected to analog pin 0.
%color=#7e7e7e% Center pin of the potentiometer goes to the analog pin.
%color=#7e7e7e% side pins of the potentiometer go to +5V and ground
%color=#7e7e7e% * LED connected from digital pin 13 to ground
%color=#7e7e7e%
%color=#7e7e7e% * Note: On most Arduino boards, there is already an LED on the board
%color=#7e7e7e% connected to pin 13, so you don't need any extra components for this example.
%color=#7e7e7e%
%color=#7e7e7e% created 17 Jan 2009
%color=#7e7e7e% by Tom Igoe
%color=#7e7e7e%
%color=#7e7e7e% http://arduino.cc/en/Tutorial/IfStatement
%color=#7e7e7e%
%color=#7e7e7e% */

%color=#7e7e7e%// These constants won't change:
const %color=#cc6600%int%% analogPin = 0; %color=#7e7e7e%// pin that the sensor is attached to
const %color=#cc6600%int%% ledPin = 13; %color=#7e7e7e%// pin that the LED is attached to
const %color=#cc6600%int%% threshold = 400; %color=#7e7e7e%// an arbitrary threshold level that's in the range of the analog input
to:
(:source http://arduino.cc/en/pub/code/master/build/shared/examples/Control/ifStatementConditional/ifStatementConditional.pde language=arduino tabwidth=4:)
Deleted lines 58-82:
%color=#cc6600%void%% %color=#cc6600%'''setup'''%%() {
%color=#7e7e7e%// initialize the LED pin as an output:
%color=#cc6600%pinMode%%(ledPin, %color=#006699%OUTPUT%%);
%color=#7e7e7e%// initialize serial communications:
%color=#cc6600%Serial%%.%color=#cc6600%begin%%(9600);
}

%color=#cc6600%void%% %color=#cc6600%'''loop'''%%() {
%color=#7e7e7e%// read the value of the potentiometer:
%color=#cc6600%int%% analogValue = %color=#cc6600%analogRead%%(analogPin);

%color=#7e7e7e%// if the analog value is high enough, turn on the LED:
%color=#cc6600%if%% (analogValue > threshold) {
%color=#cc6600%digitalWrite%%(ledPin, %color=#006699%HIGH%%);
}
%color=#cc6600%else%% {
%color=#cc6600%digitalWrite%%(ledPin,%color=#006699%LOW%%);
}

%color=#7e7e7e%// print the analog value:
%color=#cc6600%Serial%%.%color=#cc6600%println%%(analogValue, %color=#006699%DEC%%);

}

October 18, 2009, at 09:41 PM by David A. Mellis -
Changed line 88 from:
%color=#cc6600%pinMode%%(LED, %color=#006699%OUTPUT%%);
to:
%color=#cc6600%pinMode%%(ledPin, %color=#006699%OUTPUT%%);
August 27, 2009, at 08:47 PM by Tom Igoe -
Added lines 43-45:
[-image developed using [[http://www.fritzing.org |Fritzing]]. For more circuit examples, see the [[http://fritzing.org/projects/|Fritzing project page]] -]

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

[@
/*
Conditionals - If statement
to:
(:div class=code :)

%color=#7e7e7e%/*
%color=#7e7e7e% Conditionals - If statement
%color=#7e7e7e%
%color=#7e7e7e% This example demonstrates the use of if() statements.
%color=#7e7e7e% It reads the state of a potentiometer (an analog input) and turns on an LED
%color=#7e7e7e% only if the LED goes above a certain threshold level. It prints the analog value
%color=#7e7e7e% regardless of the level.
%color=#7e7e7e%
%color=#7e7e7e% The circuit:
%color=#7e7e7e% * potentiometer connected to analog pin 0.
%color=#7e7e7e% Center pin of the potentiometer goes to the analog pin.
%color=#7e7e7e% side pins of the potentiometer go to +5V and ground
%color=#7e7e7e% * LED connected from digital pin 13 to ground
%color=#7e7e7e%
%color=#7e7e7e% * Note: On most Arduino boards, there is already an LED on the board
%color=#7e7e7e% connected to pin 13, so you don't need any extra components for this example.
%color=#7e7e7e%
%color=#7e7e7e% created 17 Jan 2009
%color=#7e7e7e% by Tom Igoe
%color=#7e7e7e%
%color=#7e7e7e% http://arduino.cc/en/Tutorial/IfStatement
%color=#7e7e7e%
%color=#7e7e7e% */

%color=#7e7e7e%// These constants won't change:
const %color=#cc6600%int%% analogPin = 0; %color=#7e7e7e%// pin that the sensor is attached to
const %color=#cc6600%int%% ledPin = 13; %color=#7e7e7e%// pin that the LED is attached to
const %color=#cc6600%int%% threshold = 400; %color=#7e7e7e%// an arbitrary threshold level that's in the range of the analog input
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:
%color=#cc6600%void%% %color=#cc6600%'''setup'''%%() {
%color=#7e7e7e%// initialize the LED pin as an output:
%color=#cc6600%pinMode%%(LED, %color=#006699%OUTPUT%%);
%color=#7e7e7e%// initialize serial communications:
%color=#cc6600%Serial%%.%color=#cc6600%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:
%color=#cc6600%void%% %color=#cc6600%'''loop'''%%() {
%color=#7e7e7e%// read the value of the potentiometer:
%color=#cc6600%int%% analogValue = %color=#cc6600%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:
%color=#7e7e7e%// if the analog value is high enough, turn on the LED:
%color=#cc6600%if%% (analogValue > threshold) {
%color=#cc6600%digitalWrite%%(ledPin, %color=#006699%HIGH%%);
}
%color=#cc6600%else%% {
%color=#cc6600%digitalWrite%%(ledPin,%color=#006699%LOW%%);
}
Changed lines 102-103 from:
created 17 Jan 2009
by Tom Igoe
to:
%color=#7e7e7e%// print the analog value:
%color=#cc6600%Serial%%.%color=#cc6600%println%%(analogValue, %color=#006699%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:
(:divend:)
June 25, 2009, at 09:36 PM by Tom Igoe -
Changed lines 47-50 from:
%width=400px%[[Attach:if_schem2.png | Attach:if_schem2.png]]


to:
%width=400px%[[Attach:if_schem.png | Attach:if_schem.png]]


June 25, 2009, at 09:35 PM by Tom Igoe -
Changed lines 47-50 from:
%width=400px%[[Attach:if_schem2.png | Attach:if_schem.png]]


to:
%width=400px%[[Attach:if_schem2.png | Attach:if_schem2.png]]


June 25, 2009, at 09:32 PM by Tom Igoe -
Changed lines 47-50 from:
%width=400px%[[Attach:if_schem.png | Attach:if_schem.png]]


to:
%width=400px%[[Attach:if_schem2.png | Attach:if_schem.png]]


June 25, 2009, at 09:31 PM by Tom Igoe -
Changed lines 47-50 from:
%width=400px%[[Attach:if_schem2.png | Attach:if_schem2.png]]


to:
%width=400px%[[Attach:if_schem.png | Attach:if_schem.png]]


June 25, 2009, at 09:30 PM by Tom Igoe -
Changed lines 47-50 from:
%width=400px%[[Attach:if_schem.png | Attach:if_schem.png]]


to:
%width=400px%[[Attach:if_schem2.png | Attach:if_schem2.png]]


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-]

%width=400px%[[Attach:if.png | Attach:if.png]]

'''Schematic:'''

[-click the image to enlarge-]

%width=400px%[[Attach:if_schem.png | Attach:if_schem.png]]



!!!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