Tutorial.WhileLoop History

Hide minor edits - Show changes to output

May 02, 2012, at 04:05 PM by Scott Fitzgerald -
Changed line 40 from:
(:source http://arduino.cc/en/pub/code/master/build/shared/examples/5.Control/WhileStatementConditional/WhileStatementConditional.ino lang=arduino tabwidth=4:)
to:
(:source http://arduino.cc/en/pub/code/master/build/shared/examples/05.Control/WhileStatementConditional/WhileStatementConditional.ino lang=arduino tabwidth=4:)
November 16, 2011, at 04:17 AM by Scott Fitzgerald -
Changed line 40 from:
(:source http://arduino.cc/en/pub/code/new-extension/build/shared/examples/5.Control/WhileStatementConditional/WhileStatementConditional.ino lang=arduino tabwidth=4:)
to:
(:source http://arduino.cc/en/pub/code/master/build/shared/examples/5.Control/WhileStatementConditional/WhileStatementConditional.ino lang=arduino tabwidth=4:)
September 30, 2011, at 03:19 AM by Scott Fitzgerald -
Changed line 40 from:
(:source http://arduino.cc/en/pub/code/master/build/shared/examples/5.Control/WhileStatementConditional/WhileStatementConditional.pde lang=arduino tabwidth=4:)
to:
(:source http://arduino.cc/en/pub/code/new-extension/build/shared/examples/5.Control/WhileStatementConditional/WhileStatementConditional.ino lang=arduino tabwidth=4:)
September 17, 2010, at 11:57 PM by Christian Cerrito -
Changed lines 42-59 from:
(:divend:)
to:
(:divend:)


!!!See Also:

* [[Reference/While | while() ]]
* [[Reference/DigitalRead| digitalRead()]]
* [[Reference/DigitalWrite | digitalWrite()]]
* [[Reference/AnalogRead | analogRead()]]
* [[Reference/AnalogWrite | analogWrite()]]
* [[Reference/Map | map()]]
* [[Reference/Constrain | constrain()]]
* [[Reference/If | if()]]


* [[ForLoop | For Loop]] - Control multiple LEDs with a For Loop.
* [[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 17, 2010, at 11:39 PM by Christian Cerrito -
September 17, 2010, at 11:38 PM by Christian Cerrito -
Changed lines 5-6 from:
Sometimes you want everything in the program to stop while a given condition is true. You can do this using a '''while loop'''. This example shows how to use a while loop to [[Calibration |calibrate]] the value of an analog sensor.
to:
Sometimes you want everything in the program to stop while a given condition is true. You can do this using a [[Reference/While | '''while loop''']]. This example shows how to use a while loop to [[Calibration |calibrate]] the value of an analog sensor.
September 17, 2010, at 10:49 PM by Tom Igoe -
Changed lines 36-37 from:
[- [[Attach:fritzingFileName.zip | download Fritzing file]] -]
to:
September 17, 2010, at 10:49 PM by Tom Igoe -
Changed lines 36-37 from:

to:
[- [[Attach:fritzingFileName.zip | download Fritzing file]] -]
September 17, 2010, at 10:47 PM by Tom Igoe -
Changed line 39 from:
((:div class=code :))
to:
(:div class=code :)
September 16, 2010, at 10:16 PM by Tom Igoe -
Changed line 41 from:
(:source http://arduino.cc/en/pub/code/master/build/shared/examples/5.Control/WhileStatementConditional/WhileStatementConditional.pde lang=arduino tabwidth=4:)
to:
(:source http://arduino.cc/en/pub/code/master/build/shared/examples/5.Control/WhileStatementConditional/WhileStatementConditional.pde lang=arduino tabwidth=4:)
September 16, 2010, at 09:18 AM by Christian Cerrito -
Changed line 39 from:
((:div class=code :)
to:
((:div class=code :))
September 16, 2010, at 09:17 AM by Christian Cerrito -
Changed lines 34-37 from:
%width=400px%[[Attach:while_schem.png | Attach:while_schem.png]]


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


September 16, 2010, at 08:58 AM by Christian Cerrito -
Changed lines 21-22 from:
Analog sensor (e.g. potentiometer, light sensor) on analog input 2. LED on digital pin 9.
to:
Connect your analog sensor (e.g. potentiometer, light sensor) on analog input 2 with a 10K ohm resistor to ground. Connect your button to digital pin, again with a 10K ohm resistor to ground. Connect your LED to digital pin 9, with a 220 ohm resistor in series.
September 16, 2010, at 08:55 AM by Christian Cerrito -
Changed lines 11-18 from:
to:
!!!Hardware Required

* Arduino Board
* (1) digital pushbutton or switch
* (1) photocell, or analog sensor
* (2) 10k ohm resistors
* breadboard
Changed lines 25-26 from:
%width=600px%[[Attach:while.png | Attach:while.png]]
to:
%width=600px%[[Attach:while_bb.png | Attach:while_bb.png]]
September 16, 2010, at 08:06 AM by Christian Cerrito -
Changed lines 18-19 from:
%width=500px%[[Attach:while.png | Attach:while.png]]
to:
%width=600px%[[Attach:while.png | Attach:while.png]]
September 16, 2010, at 08:06 AM by Christian Cerrito -
Changed lines 18-19 from:
%width=400px%[[Attach:while.png | Attach:while.png]]
to:
%width=500px%[[Attach:while.png | Attach:while.png]]
September 16, 2010, at 01:53 AM by Christian Cerrito -
Changed line 34 from:
(:source http://arduino.cc/en/pub/code/master/build/shared/examples/Control/WhileStatementConditional/WhileStatementConditional.pde lang=arduino tabwidth=4:)
to:
(:source http://arduino.cc/en/pub/code/master/build/shared/examples/5.Control/WhileStatementConditional/WhileStatementConditional.pde lang=arduino tabwidth=4:)
February 24, 2010, at 04:09 AM by Tom Igoe -
Changed line 32 from:
(:div class=code :)
to:
((:div class=code :)
Changed lines 34-58 from:
%color=#7e7e7e%/*
%color=#7e7e7e% Conditionals - while statement
%color=#7e7e7e%
%color=#7e7e7e% This example demonstrates the use of while() statements.
%color=#7e7e7e%
%color=#7e7e7e% While the pushbutton is pressed, the sketch runs the calibration routine.
%color=#7e7e7e% The sensor readings during the while loop define the minimum and maximum
%color=#7e7e7e% of expected values from the photo resistor.
%color=#7e7e7e%
%color=#7e7e7e% This is a variation on the calibrate example.
%color=#7e7e7e%
%color=#7e7e7e% The circuit:
%color=#7e7e7e% * photo resistor connected from +5V to analog in pin 0
%color=#7e7e7e% * 10K resistor connected from ground to analog in pin 0
%color=#7e7e7e% * LED connected from digital pin 9 to ground through 220 ohm resistor
%color=#7e7e7e% * pushbutton attached from pin 2 to +5V
%color=#7e7e7e% * 10K resistor attached from pin 2 to ground
%color=#7e7e7e%
%color=#7e7e7e% created 17 Jan 2009
%color=#7e7e7e% modified 25 Jun 2009
%color=#7e7e7e% by Tom Igoe
%color=#7e7e7e%
%color=#7e7e7e% http://arduino.cc/en/Tutorial/WhileLoop
%color=#7e7e7e%
%color=#7e7e7e% */
to:
(:source http://arduino.cc/en/pub/code/master/build/shared/examples/Control/WhileStatementConditional/WhileStatementConditional.pde lang=arduino tabwidth=4:)
Deleted lines 35-95:

%color=#7e7e7e%// These constants won't change:
const %color=#cc6600%int%% sensorPin = 2; %color=#7e7e7e%// pin that the sensor is attached to
const %color=#cc6600%int%% ledPin = 9; %color=#7e7e7e%// pin that the LED is attached to
const %color=#cc6600%int%% indicatorLedPin = 13; %color=#7e7e7e%// pin that the built-in LED is attached to
const %color=#cc6600%int%% buttonPin = 2; %color=#7e7e7e%// pin that the button is attached to


%color=#7e7e7e%// These variables will change:
%color=#cc6600%int%% sensorMin = 1023; %color=#7e7e7e%// minimum sensor value
%color=#cc6600%int%% sensorMax = 0; %color=#7e7e7e%// maximum sensor value
%color=#cc6600%int%% sensorValue = 0; %color=#7e7e7e%// the sensor value


%color=#cc6600%void%% %color=#cc6600%'''setup'''%%() {
%color=#7e7e7e%// set the LED pins as outputs and the switch pin as input:
%color=#cc6600%pinMode%%(indicatorLedPin, %color=#006699%OUTPUT%%);
%color=#cc6600%pinMode%% (ledPin, %color=#006699%OUTPUT%%);
%color=#cc6600%pinMode%% (buttonPin, %color=#006699%INPUT%%);
}

%color=#cc6600%void%% %color=#cc6600%'''loop'''%%() {
%color=#7e7e7e%// while the button is pressed, take calibration readings:
%color=#cc6600%while%% (%color=#cc6600%digitalRead%%(buttonPin) == %color=#006699%HIGH%%) {
calibrate();
}
%color=#7e7e7e%// signal the end of the calibration period
%color=#cc6600%digitalWrite%%(indicatorLedPin, %color=#006699%LOW%%);

%color=#7e7e7e%// read the sensor:
sensorValue = %color=#cc6600%analogRead%%(sensorPin);

%color=#7e7e7e%// apply the calibration to the sensor reading
sensorValue = %color=#cc6600%map%%(sensorValue, sensorMin, sensorMax, 0, 255);

%color=#7e7e7e%// in case the sensor value is outside the range seen during calibration
sensorValue = %color=#cc6600%constrain%%(sensorValue, 0, 255);

%color=#7e7e7e%// fade the LED using the calibrated value:
%color=#cc6600%analogWrite%%(ledPin, sensorValue);
}

%color=#cc6600%void%% calibrate() {
%color=#7e7e7e%// turn on the indicator LED to indicate that calibration is happening:
%color=#cc6600%digitalWrite%%(indicatorLedPin, %color=#006699%HIGH%%);
%color=#7e7e7e%// read the sensor:
sensorValue = %color=#cc6600%analogRead%%(sensorPin);

%color=#7e7e7e%// record the maximum sensor value
%color=#cc6600%if%% (sensorValue > sensorMax) {
sensorMax = sensorValue;
}

%color=#7e7e7e%// record the minimum sensor value
%color=#cc6600%if%% (sensorValue < sensorMin) {
sensorMin = sensorValue;
}
}


August 27, 2009, at 08:47 PM by Tom Igoe -
Added lines 20-22:
[-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:24 PM by Tom Igoe -
Changed lines 29-32 from:

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

%color=#7e7e7e%/*
%color=#7e7e7e% Conditionals - while statement
%color=#7e7e7e%
%color=#7e7e7e% This example demonstrates the use of while() statements.
%color=#7e7e7e%
%color=#7e7e7e% While the pushbutton is pressed, the sketch runs the calibration routine.
%color=#7e7e7e% The sensor readings during the while loop define the minimum and maximum
%color=#7e7e7e% of expected values from the photo resistor.
%color=#7e7e7e%
%color=#7e7e7e% This is a variation on the calibrate example.
%color=#7e7e7e%
%color=#7e7e7e% The circuit:
%color=#7e7e7e% * photo resistor connected from +5V to analog in pin 0
%color=#7e7e7e% * 10K resistor connected from ground to analog in pin 0
%color=#7e7e7e% * LED connected from digital pin 9 to ground through 220 ohm resistor
%color=#7e7e7e% * pushbutton attached from pin 2 to +5V
%color=#7e7e7e% * 10K resistor attached from pin 2 to ground
%color=#7e7e7e%
%color=#7e7e7e% created 17 Jan 2009
%color=#7e7e7e% modified 25 Jun 2009
%color=#7e7e7e% by Tom Igoe
%color=#7e7e7e%
%color=#7e7e7e% http://arduino.cc/en/Tutorial/WhileLoop
%color=#7e7e7e%
%color=#7e7e7e% */
Deleted line 56:
This example demonstrates the use of while() statements.
Changed lines 58-60 from:
While the pushbutton is pressed, the sketch runs the calibration routine.
The sensor readings during the while loop define the minimum and maximum
of expected values from the photo resistor.
to:
%color=#7e7e7e%// These constants won't change:
const %color=#cc6600%int%% sensorPin = 2; %color=#7e7e7e%// pin that the sensor is attached to
const %color=#cc6600%int%% ledPin = 9; %color=#7e7e7e%// pin that the LED is attached to
const %color=#cc6600%int%% indicatorLedPin = 13; %color=#7e7e7e%// pin that the built-in LED is attached to
const %color=#cc6600%int%% buttonPin = 2; %color=#7e7e7e%// pin that the button is attached to
Deleted line 63:
This is a variation on the calibrate example.
Changed lines 65-70 from:
The circuit:
* photo resistor connected from +5V to analog in pin 0
* 10K resistor connected from ground to analog in pin 0
* LED connected from digital pin 9 to ground through 220 ohm resistor
* pushbutton attached from pin 2 to +5V
* 10K resistor attached from pin 2 to ground
to:
%color=#7e7e7e%// These variables will change:
%color=#cc6600%int%% sensorMin = 1023; %color=#7e7e7e%// minimum sensor value
%color=#cc6600%int%% sensorMax = 0; %color=#7e7e7e%// maximum sensor value
%color=#cc6600%int%% sensorValue = 0; %color=#7e7e7e%// the sensor value
Deleted lines 69-71:
created 17 Jan 2009
modified 25 Jun 2009
by Tom Igoe
Changed lines 71-136 from:
http://arduino.cc/en/Tutorial/WhileLoop

*/


// These constants won't change:
const int sensorPin = 2; // pin that the sensor is attached to
const int ledPin = 9; // pin that the LED is attached to
const int indicatorLedPin = 13; // pin that the built-in LED is attached to
const int buttonPin = 2; // pin that the button is attached to


// These variables will change:
int sensorMin = 1023; // minimum sensor value
int sensorMax = 0; // maximum sensor value
int sensorValue = 0; // the sensor value


void setup() {
// set the LED pins as outputs and the switch pin as input:
pinMode(indicatorLedPin, OUTPUT);
pinMode (ledPin, OUTPUT);
pinMode (buttonPin, INPUT);
}

void loop() {
// while the button is pressed, take calibration readings:
while (digitalRead(buttonPin) == HIGH) {
calibrate();
}
// signal the end of the calibration period
digitalWrite(indicatorLedPin, LOW);

// read the sensor:
sensorValue = analogRead(sensorPin);

// apply the calibration to the sensor reading
sensorValue = map(sensorValue, sensorMin, sensorMax, 0, 255);

// in case the sensor value is outside the range seen during calibration
sensorValue = constrain(sensorValue, 0, 255);

// fade the LED using the calibrated value:
analogWrite(ledPin, sensorValue);
}

void calibrate() {
// turn on the indicator LED to indicate that calibration is happening:
digitalWrite(indicatorLedPin, HIGH);
// read the sensor:
sensorValue = analogRead(sensorPin);

// record the maximum sensor value
if (sensorValue > sensorMax) {
sensorMax = sensorValue;
}

// record the minimum sensor value
if (sensorValue < sensorMin) {
sensorMin = sensorValue;
}
}



@]
to:
%color=#cc6600%void%% %color=#cc6600%'''setup'''%%() {
%color=#7e7e7e%// set the LED pins as outputs and the switch pin as input:
%color=#cc6600%pinMode%%(indicatorLedPin, %color=#006699%OUTPUT%%);
%color=#cc6600%pinMode%% (ledPin, %color=#006699%OUTPUT%%);
%color=#cc6600%pinMode%% (buttonPin, %color=#006699%INPUT%%);
}

%color=#cc6600%void%% %color=#cc6600%'''loop'''%%() {
%color=#7e7e7e%// while the button is pressed, take calibration readings:
%color=#cc6600%while%% (%color=#cc6600%digitalRead%%(buttonPin) == %color=#006699%HIGH%%) {
calibrate();
}
%color=#7e7e7e%// signal the end of the calibration period
%color=#cc6600%digitalWrite%%(indicatorLedPin, %color=#006699%LOW%%);

%color=#7e7e7e%// read the sensor:
sensorValue = %color=#cc6600%analogRead%%(sensorPin);

%color=#7e7e7e%// apply the calibration to the sensor reading
sensorValue = %color=#cc6600%map%%(sensorValue, sensorMin, sensorMax, 0, 255);

%color=#7e7e7e%// in case the sensor value is outside the range seen during calibration
sensorValue = %color=#cc6600%constrain%%(sensorValue, 0, 255);

%color=#7e7e7e%// fade the LED using the calibrated value:
%color=#cc6600%analogWrite%%(ledPin, sensorValue);
}

%color=#cc6600%void%% calibrate() {
%color=#7e7e7e%// turn on the indicator LED to indicate that calibration is happening:
%color=#cc6600%digitalWrite%%(indicatorLedPin, %color=#006699%HIGH%%);
%color=#7e7e7e%// read the sensor:
sensorValue = %color=#cc6600%analogRead%%(sensorPin);

%color=#7e7e7e%// record the maximum sensor value
%color=#cc6600%if%% (sensorValue > sensorMax) {
sensorMax = sensorValue;
}

%color=#7e7e7e%// record the minimum sensor value
%color=#cc6600%if%% (sensorValue < sensorMin) {
sensorMin = sensorValue;
}
}



(:divend:)
June 25, 2009, at 11:35 PM by Tom Igoe -
Added lines 14-15:
Analog sensor (e.g. potentiometer, light sensor) on analog input 2. LED on digital pin 9.
June 25, 2009, at 09:06 PM by Tom Igoe -
Added lines 51-52:
http://arduino.cc/en/Tutorial/WhileLoop
June 25, 2009, at 08:56 PM by Tom Igoe -
Changed lines 5-6 from:
Sometimes you want everything in the program to stop while a given condition is true. You can do this using a '''while loop'''. This example shows how to use a while loop to [[Calibrate |calibrate]] the value of an analog sensor.
to:
Sometimes you want everything in the program to stop while a given condition is true. You can do this using a '''while loop'''. This example shows how to use a while loop to [[Calibration |calibrate]] the value of an analog sensor.
June 25, 2009, at 08:56 PM by Tom Igoe -
Changed lines 5-6 from:
Sometimes you want everything in the program to stop while a given condition is true. You can do this using a '''while loop'''. This example shows how to use a while loop to calibrate the value of an analog sensor.
to:
Sometimes you want everything in the program to stop while a given condition is true. You can do this using a '''while loop'''. This example shows how to use a while loop to [[Calibrate |calibrate]] the value of an analog sensor.
June 25, 2009, at 08:55 PM by Tom Igoe -
Deleted lines 13-16:
Attach:while.png

'''Schematic:'''
Added lines 16-21:
%width=400px%[[Attach:while.png | Attach:while.png]]

'''Schematic:'''

[-click the image to enlarge-]
June 25, 2009, at 08:11 PM by Tom Igoe -
Changed lines 1-2 from:
!!Code
to:
''Examples > Control Structures''

!!!While Loop

Sometimes you want everything in the program to stop while a given condition is true. You can do this using a '''while loop'''. This example shows how to use a while loop to calibrate the value of an analog sensor.

In the main loop, the sketch below reads the value of a photoresistor on analog pin 0 and uses it to fade an LED on pin 9. But while a button attached to digital pin 2 is pressed, the program runs a method called @@calibrate()@@ that looks for the highest and lowest values of the analog sensor. When you release the button, the sketch continues with the main loop.

This technique lets you update the maximum and minimum values for the photoresistor when the lighting conditions change.


!!!Circuit

Attach:while.png

'''Schematic:'''

[-click the image to enlarge-]

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



!!!Code
June 25, 2009, at 08:06 PM by Tom Igoe -
Added lines 1-89:
!!Code

[@
/*
Conditionals - while statement

This example demonstrates the use of while() statements.

While the pushbutton is pressed, the sketch runs the calibration routine.
The sensor readings during the while loop define the minimum and maximum
of expected values from the photo resistor.

This is a variation on the calibrate example.

The circuit:
* photo resistor connected from +5V to analog in pin 0
* 10K resistor connected from ground to analog in pin 0
* LED connected from digital pin 9 to ground through 220 ohm resistor
* pushbutton attached from pin 2 to +5V
* 10K resistor attached from pin 2 to ground

created 17 Jan 2009
modified 25 Jun 2009
by Tom Igoe

*/


// These constants won't change:
const int sensorPin = 2; // pin that the sensor is attached to
const int ledPin = 9; // pin that the LED is attached to
const int indicatorLedPin = 13; // pin that the built-in LED is attached to
const int buttonPin = 2; // pin that the button is attached to


// These variables will change:
int sensorMin = 1023; // minimum sensor value
int sensorMax = 0; // maximum sensor value
int sensorValue = 0; // the sensor value


void setup() {
// set the LED pins as outputs and the switch pin as input:
pinMode(indicatorLedPin, OUTPUT);
pinMode (ledPin, OUTPUT);
pinMode (buttonPin, INPUT);
}

void loop() {
// while the button is pressed, take calibration readings:
while (digitalRead(buttonPin) == HIGH) {
calibrate();
}
// signal the end of the calibration period
digitalWrite(indicatorLedPin, LOW);

// read the sensor:
sensorValue = analogRead(sensorPin);

// apply the calibration to the sensor reading
sensorValue = map(sensorValue, sensorMin, sensorMax, 0, 255);

// in case the sensor value is outside the range seen during calibration
sensorValue = constrain(sensorValue, 0, 255);

// fade the LED using the calibrated value:
analogWrite(ledPin, sensorValue);
}

void calibrate() {
// turn on the indicator LED to indicate that calibration is happening:
digitalWrite(indicatorLedPin, HIGH);
// read the sensor:
sensorValue = analogRead(sensorPin);

// record the maximum sensor value
if (sensorValue > sensorMax) {
sensorMax = sensorValue;
}

// record the minimum sensor value
if (sensorValue < sensorMin) {
sensorMin = sensorValue;
}
}



@]

Share