Tutorial.ForLoop History

Hide minor edits - Show changes to output

May 02, 2012, at 04:07 PM by Scott Fitzgerald -
Changed line 41 from:
(:source http://arduino.cc/en/pub/code/master/build/shared/examples/5.Control/ForLoopIteration/ForLoopIteration.ino lang=arduino tabwidth=4:)
to:
(:source http://arduino.cc/en/pub/code/master/build/shared/examples/05.Control/ForLoopIteration/ForLoopIteration.ino lang=arduino tabwidth=4:)
November 16, 2011, at 04:17 AM by Scott Fitzgerald -
Changed line 41 from:
(:source http://arduino.cc/en/pub/code/new-extension/build/shared/examples/5.Control/ForLoopIteration/ForLoopIteration.ino lang=arduino tabwidth=4:)
to:
(:source http://arduino.cc/en/pub/code/master/build/shared/examples/5.Control/ForLoopIteration/ForLoopIteration.ino lang=arduino tabwidth=4:)
September 30, 2011, at 03:18 AM by Scott Fitzgerald -
Changed line 41 from:
(:source http://arduino.cc/en/pub/code/master/build/shared/examples/5.Control/ForLoopIteration/ForLoopIteration.pde lang=arduino tabwidth=4:)
to:
(:source http://arduino.cc/en/pub/code/new-extension/build/shared/examples/5.Control/ForLoopIteration/ForLoopIteration.ino lang=arduino tabwidth=4:)
September 28, 2010, at 01:30 PM by Tom Igoe -
Deleted lines 34-35:
(:div class=code :)
Added lines 39-40:
(:div class=code :)
September 24, 2010, at 05:59 AM by Tom Igoe -
Changed lines 31-32 from:
%width=400px%[[Attach:forloop2_schem.png | Attach:forLoop2_schem.png]]
to:
%width=400px%[[Attach:forLoop2_schem.png | Attach:forLoop2_schem.png]]
September 23, 2010, at 10:39 PM by Christian Cerrito -
Changed lines 3-5 from:
!!! For Loop
!!! (aka Knight Rider)
to:
!!! For Loop (aka The Knight Rider)
September 23, 2010, at 10:39 PM by Christian Cerrito -
Changed line 3 from:
!! For Loop
to:
!!! For Loop
September 18, 2010, at 12:11 AM by Christian Cerrito -
Changed lines 46-47 from:
to:
!!!See Also:
September 18, 2010, at 12:10 AM by Christian Cerrito -
Changed lines 6-7 from:
Often you want to iterate over a series of pins and do something to each one. For instance, this example blinks 6 LEDs attached the Arduino by using a [[Reference/For |for()]] loop to cycle back and forth through digital pins 2-7. The LEDS are turned on and off, in sequence, by using both the [[Reference/digitalWrite | digitalWrite()]] and [[Reference/Delay | delay()]] functions .
to:
Often you want to iterate over a series of pins and do something to each one. For instance, this example blinks 6 LEDs attached the Arduino by using a [[Reference/For |'''for()''']] loop to cycle back and forth through digital pins 2-7. The LEDS are turned on and off, in sequence, by using both the [[Reference/digitalWrite | digitalWrite()]] and [[Reference/Delay | delay()]] functions .
September 17, 2010, at 10:44 PM by Tom Igoe -
Changed lines 44-46 from:
(:divend:

to:
(:divend:)

September 16, 2010, at 10:16 PM by Tom Igoe -
Changed line 42 from:
(:source http://arduino.cc/en/pub/code/master/build/shared/examples/5.Control/ForLoopIteration/ForLoopIteration.pde language=arduino tabwidth=4:)
to:
(:source http://arduino.cc/en/pub/code/master/build/shared/examples/5.Control/ForLoopIteration/ForLoopIteration.pde lang=arduino tabwidth=4:)
September 16, 2010, at 08:05 AM by Christian Cerrito -
Changed lines 6-7 from:
Often you want to iterate over a series of pins and do something to each one. For instance, this example blinks 6 LEDs attached the Arduino by using a [[Reference/For | @@for()@@ ]] loop to cycle back and forth through digital pins 2-7. The LEDS are turned on and off, in sequence, by using both the [[Reference/digitalWrite | @@digitalWrite()@@]] and [[Reference/Delay | @@delay()@@]] functions .
to:
Often you want to iterate over a series of pins and do something to each one. For instance, this example blinks 6 LEDs attached the Arduino by using a [[Reference/For |for()]] loop to cycle back and forth through digital pins 2-7. The LEDS are turned on and off, in sequence, by using both the [[Reference/digitalWrite | digitalWrite()]] and [[Reference/Delay | delay()]] functions .
September 16, 2010, at 07:59 AM by Christian Cerrito -
Changed lines 40-41 from:
In the main loop of the code, two @@for()@@ loops are used to loop incrementally, turning on the LEDs, one by one, connected to lowest pin to the highest.
to:
In the main loop of the code, two @@for()@@ loops are used to loop incrementally, stepping through the LEDs, one by one, from pin 2 to pin seven. Once pin 7 is lit, the process reverses, stepping back down through each LED.
September 16, 2010, at 07:56 AM by Christian Cerrito -
September 16, 2010, at 07:53 AM by Christian Cerrito -
Changed lines 6-7 from:
Often you want to iterate over a series of pins and do something to each one. For instance, this example blinks 6 LEDs attached the Arduino by using a [[Reference/For | @@for()@@ ]] loop to cycle back and forth through digital pins 2-7. The LEDS are turned on and off, in sequence, by using both the [Reference/digitalWrite | @@digitalWrite()@@]] and [[Reference/Delay | @@delay()@@]] functions .
to:
Often you want to iterate over a series of pins and do something to each one. For instance, this example blinks 6 LEDs attached the Arduino by using a [[Reference/For | @@for()@@ ]] loop to cycle back and forth through digital pins 2-7. The LEDS are turned on and off, in sequence, by using both the [[Reference/digitalWrite | @@digitalWrite()@@]] and [[Reference/Delay | @@delay()@@]] functions .
September 16, 2010, at 07:52 AM by Christian Cerrito -
Changed lines 6-7 from:
Often you want to iterate over a series of pins and do something to each one. For instance, this example blinks 6 LEDs, one by one, attached the Arduino by using a [[Reference/For | @@for()@@ ]] loop to cycle back and forth through digital pins 2-7.
to:
Often you want to iterate over a series of pins and do something to each one. For instance, this example blinks 6 LEDs attached the Arduino by using a [[Reference/For | @@for()@@ ]] loop to cycle back and forth through digital pins 2-7. The LEDS are turned on and off, in sequence, by using both the [Reference/digitalWrite | @@digitalWrite()@@]] and [[Reference/Delay | @@delay()@@]] functions .
Deleted lines 9-10:
This example makes use of 6 LEDs connected to the pins 2 - 7 on the board using 220 Ohm resistors. The sketch makes the LEDs blink in a sequence, one by one using only [[Reference/digitalWrite | @@digitalWrite()@@]] and [[Reference/Delay | @@delay()@@]] .
September 16, 2010, at 07:50 AM by Christian Cerrito -
Changed lines 10-11 from:
This example makes use of 6 LEDs connected to the pins 2 - 7 on the board using 220 Ohm resistors. The sketch makes the LEDs blink in a sequence, one by one using only * [[Reference/digitalWrite | @@digitalWrite()@@]] and * [[Reference/Delay | @@delay()@@]] .
to:
This example makes use of 6 LEDs connected to the pins 2 - 7 on the board using 220 Ohm resistors. The sketch makes the LEDs blink in a sequence, one by one using only [[Reference/digitalWrite | @@digitalWrite()@@]] and [[Reference/Delay | @@delay()@@]] .
September 16, 2010, at 07:49 AM by Christian Cerrito -
Changed lines 10-11 from:
This example makes use of 6 LEDs connected to the pins 2 - 7 on the board using 220 Ohm resistors. The sketch makes the LEDs blink in a sequence, one by one using only '''digitalWrite(pinNumber,HIGH/LOW)''' and '''delay(time)'''.
to:
This example makes use of 6 LEDs connected to the pins 2 - 7 on the board using 220 Ohm resistors. The sketch makes the LEDs blink in a sequence, one by one using only * [[Reference/digitalWrite | @@digitalWrite()@@]] and * [[Reference/Delay | @@delay()@@]] .
September 16, 2010, at 07:48 AM by Christian Cerrito -
Changed lines 6-7 from:
Often you want to iterate over a series of pins and do something to each one. For instance, this example lights 6 LEDs attached the Arduino by using a [[Reference/For | @@for()@@ ]] loop to cycle back and forth through digital pins 2-7.
to:
Often you want to iterate over a series of pins and do something to each one. For instance, this example blinks 6 LEDs, one by one, attached the Arduino by using a [[Reference/For | @@for()@@ ]] loop to cycle back and forth through digital pins 2-7.
September 16, 2010, at 07:47 AM by Christian Cerrito -
Changed lines 6-7 from:
Often you want to iterate over a series of pins and do something to each one. For example, this example lights up a series of LEDs attached to pins 2 through 7 of the Arduino.
to:
Often you want to iterate over a series of pins and do something to each one. For instance, this example lights 6 LEDs attached the Arduino by using a [[Reference/For | @@for()@@ ]] loop to cycle back and forth through digital pins 2-7.
September 16, 2010, at 07:43 AM by Christian Cerrito -
Changed lines 49-51 from:
* [[Reference/if | @@if()@@ ]]
* [[Reference/Else| @@if...else@@]]
* [[Reference/AnalogRead | @@analogRead()@@]]
to:
* [[Reference/For | @@for()@@ ]]
Changed lines 51-54 from:
* [[Serial/Begin| @@serial.begin()@@]]
* [[Serial/Print | @@serial.print()@@]]

* [[Tutorial/ForLoop]] - Control multiple LEDs with a For Loop.
to:
* [[Reference/Delay | @@delay()@@]]
September 16, 2010, at 07:41 AM by Christian Cerrito -
Changed lines 56-58 from:
* [[Tutorial/Forloop]] - Control multiple LEDs with a For Loop.
* [[Tutorial/Whileloop]] - Use a While Loop to calibrate a sensor while a button is being pressed.
* [[Tutorial/Switchcase]] - 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:
* [[Tutorial/ForLoop]] - Control multiple LEDs with a For Loop.
* [[Tutorial/WhileLoop]] - Use a While Loop to calibrate a sensor while a button is being pressed.
* [[Tutorial/SwitchCase]] - 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 16, 2010, at 07:40 AM by Christian Cerrito -
Changed lines 56-58 from:
* [[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.
to:
* [[Tutorial/Forloop]] - Control multiple LEDs with a For Loop.
* [[Tutorial/Whileloop]] - Use a While Loop to calibrate a sensor while a button is being pressed.
* [[Tutorial/Switchcase]] - 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 16, 2010, at 07:38 AM by Christian Cerrito -
September 16, 2010, at 07:38 AM by Christian Cerrito -
September 16, 2010, at 07:37 AM by Christian Cerrito -
Added lines 21-22:
Connect six LEDS, with 220 ohm resistors in series, to digital pins 2-7 on your Arduino.
Changed lines 39-40 from:

(:source http://arduino.cc/en/pub/code/master/build/shared/examples/5.Control/ForLoopIteration/ForLoopIteration.pde language=arduino tabwidth=4:)
to:

The code below begins by utilizing a @@for()@@ loop to assign digital pins 2-7 as outputs for the 6 LEDs used.

In the main loop of the code, two @@for()@@ loops are used to loop incrementally, turning on the LEDs, one by one, connected to lowest pin to the highest.

(:source http://arduino.cc/en/pub/code/master/build/shared/examples/5.Control/ForLoopIteration/ForLoopIteration.pde language=arduino tabwidth=4:)
September 16, 2010, at 07:28 AM by Christian Cerrito -
Added lines 12-18:
!!!Hardware Required
* Arduino Board
* (6) 220 ohm resistors
* (6) LEDs
* hook-up wire
* breadboard
Changed lines 40-53 from:
(:divend:)
to:
(:divend:


* [[Reference/if | @@if()@@ ]]
* [[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.
September 16, 2010, at 07:24 AM by Christian Cerrito -
Changed lines 25-26 from:
%width=400px%[[Attach:forLoop_schem.png | Attach:forLoop_schem.png]]
to:
%width=400px%[[Attach:forloop2_schem.png | Attach:forLoop2_schem.png]]
September 15, 2010, at 11:28 PM by Christian Cerrito -
Changed line 31 from:
(:source http://arduino.cc/en/pub/code/master/build/shared/examples/Control/ForLoopIteration/ForLoopIteration.pde language=arduino tabwidth=4:)
to:
(:source http://arduino.cc/en/pub/code/master/build/shared/examples/5.Control/ForLoopIteration/ForLoopIteration.pde language=arduino tabwidth=4:)
February 24, 2010, at 04:06 AM by Tom Igoe -
Changed lines 31-46 from:
%color=#7e7e7e%/*
%color=#7e7e7e% For Loop Iteration
%color=#7e7e7e%
%color=#7e7e7e% Demonstrates the use of a for() loop.
%color=#7e7e7e% Lights multiple LEDs in sequence, then in reverse.
%color=#7e7e7e%
%color=#7e7e7e% The circuit:
%color=#7e7e7e% * LEDs from pins 2 through 7 to ground
%color=#7e7e7e%
%color=#7e7e7e% created 2006
%color=#7e7e7e% by David A. Mellis
%color=#7e7e7e% modified 5 Jul 2009
%color=#7e7e7e% by Tom Igoe
%color=#7e7e7e%
%color=#7e7e7e% http://www.arduino.cc/en/Tutorial/ForLoop
%color=#7e7e7e% */
to:
(:source http://arduino.cc/en/pub/code/master/build/shared/examples/Control/ForLoopIteration/ForLoopIteration.pde language=arduino tabwidth=4:)
Deleted lines 32-60:
%color=#cc6600%int%% timer = 100; %color=#7e7e7e%// The higher the number, the slower the timing.

%color=#cc6600%void%% %color=#cc6600%'''setup'''%%() {
%color=#7e7e7e%// use a for loop to initialize each pin as an output:
%color=#cc6600%for%% (%color=#cc6600%int%% thisPin = 2; thisPin < 8; thisPin++) {
%color=#cc6600%pinMode%%(thisPin, %color=#006699%OUTPUT%%);
}
}

%color=#cc6600%void%% %color=#cc6600%'''loop'''%%() {
%color=#7e7e7e%// loop from the lowest pin to the highest:
%color=#cc6600%for%% (%color=#cc6600%int%% thisPin = 2; thisPin < 8; thisPin++) {
%color=#7e7e7e%// turn the pin on:
%color=#cc6600%digitalWrite%%(thisPin, %color=#006699%HIGH%%);
%color=#cc6600%delay%%(timer);
%color=#7e7e7e%// turn the pin off:
%color=#cc6600%digitalWrite%%(thisPin, %color=#006699%LOW%%);
}

%color=#7e7e7e%// loop from the highest pin to the lowest:
%color=#cc6600%for%% (%color=#cc6600%int%% thisPin = 7; thisPin >= 2; thisPin--) {
%color=#7e7e7e%// turn the pin on:
%color=#cc6600%digitalWrite%%(thisPin, %color=#006699%HIGH%%);
%color=#cc6600%delay%%(timer);
%color=#7e7e7e%// turn the pin off:
%color=#cc6600%digitalWrite%%(thisPin, %color=#006699%LOW%%);
}
}
September 26, 2009, at 06:43 PM by David A. Mellis -
Changed line 59 from:
%color=#cc6600%for%% (%color=#cc6600%int%% thisPin = 0; thisPin < 8; thisPin++) {
to:
%color=#cc6600%for%% (%color=#cc6600%int%% thisPin = 2; thisPin < 8; thisPin++) {
August 27, 2009, at 08:47 PM by Tom Igoe -
Added lines 18-20:
[-image developed using [[http://www.fritzing.org |Fritzing]]. For more circuit examples, see the [[http://fritzing.org/projects/|Fritzing project page]] -]

July 06, 2009, at 08:12 PM by Tom Igoe -
Changed line 3 from:
!! For Loop and Arrays
to:
!! For Loop
July 06, 2009, at 08:11 PM by Tom Igoe -
Added line 25:
Changed line 56 from:
%color=#cc6600%for%% (%color=#cc6600%int%% thisPin = 0; i < 8; thisPin++) {
to:
%color=#cc6600%for%% (%color=#cc6600%int%% thisPin = 0; thisPin < 8; thisPin++) {
Changed line 65 from:
%color=#cc6600%for%% (thisPin = 7; thisPin >= 2; thisPin--) {
to:
%color=#cc6600%for%% (%color=#cc6600%int%% thisPin = 7; thisPin >= 2; thisPin--) {
Changed line 74 from:
(:divend:)
to:
(:divend:)
July 05, 2009, at 08:54 PM by Tom Igoe -
Added lines 1-73:
''Examples > Control Structures''

!! For Loop and Arrays
!!! (aka Knight Rider)

Often you want to iterate over a series of pins and do something to each one. For example, this example lights up a series of LEDs attached to pins 2 through 7 of the Arduino.

We also call this example "[[http://en.wikipedia.org/wiki/KITT|Knight Rider]]" in memory of a TV-series from the 80's where David Hasselhoff had an AI machine named KITT driving his Pontiac. The car had been augmented with plenty of LEDs in all possible sizes performing flashy effects. In particular, it had a display that scanned back and forth across a line, as shown in this exciting [[http://video.yahoo.com/video/play?vid=707585 |fight between KITT and KARR]]. This example duplicates the KITT display.

This example makes use of 6 LEDs connected to the pins 2 - 7 on the board using 220 Ohm resistors. The sketch makes the LEDs blink in a sequence, one by one using only '''digitalWrite(pinNumber,HIGH/LOW)''' and '''delay(time)'''.

!!!Circuit

[-click the image to enlarge-]

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

'''Schematic:'''

[-click the image to enlarge-]

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

!!!Code
(:div class=code :)

%color=#7e7e7e%/*
%color=#7e7e7e% For Loop Iteration
%color=#7e7e7e%
%color=#7e7e7e% Demonstrates the use of a for() loop.
%color=#7e7e7e% Lights multiple LEDs in sequence, then in reverse.
%color=#7e7e7e%
%color=#7e7e7e% The circuit:
%color=#7e7e7e% * LEDs from pins 2 through 7 to ground
%color=#7e7e7e%
%color=#7e7e7e% created 2006
%color=#7e7e7e% by David A. Mellis
%color=#7e7e7e% modified 5 Jul 2009
%color=#7e7e7e% by Tom Igoe
%color=#7e7e7e%
%color=#7e7e7e% http://www.arduino.cc/en/Tutorial/ForLoop
%color=#7e7e7e% */

%color=#cc6600%int%% timer = 100; %color=#7e7e7e%// The higher the number, the slower the timing.

%color=#cc6600%void%% %color=#cc6600%'''setup'''%%() {
%color=#7e7e7e%// use a for loop to initialize each pin as an output:
%color=#cc6600%for%% (%color=#cc6600%int%% thisPin = 2; thisPin < 8; thisPin++) {
%color=#cc6600%pinMode%%(thisPin, %color=#006699%OUTPUT%%);
}
}

%color=#cc6600%void%% %color=#cc6600%'''loop'''%%() {
%color=#7e7e7e%// loop from the lowest pin to the highest:
%color=#cc6600%for%% (%color=#cc6600%int%% thisPin = 0; i < 8; thisPin++) {
%color=#7e7e7e%// turn the pin on:
%color=#cc6600%digitalWrite%%(thisPin, %color=#006699%HIGH%%);
%color=#cc6600%delay%%(timer);
%color=#7e7e7e%// turn the pin off:
%color=#cc6600%digitalWrite%%(thisPin, %color=#006699%LOW%%);
}

%color=#7e7e7e%// loop from the highest pin to the lowest:
%color=#cc6600%for%% (thisPin = 7; thisPin >= 2; thisPin--) {
%color=#7e7e7e%// turn the pin on:
%color=#cc6600%digitalWrite%%(thisPin, %color=#006699%HIGH%%);
%color=#cc6600%delay%%(timer);
%color=#7e7e7e%// turn the pin off:
%color=#cc6600%digitalWrite%%(thisPin, %color=#006699%LOW%%);
}
}

(:divend:)

Share