Loading...

Tutorial.ForLoop History

Hide minor edits - Show changes to markup

May 02, 2012, at 10:07 AM 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 15, 2011, at 10:17 PM 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 29, 2011, at 09:18 PM 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 07:30 AM by Tom Igoe -
Deleted lines 34-35:

(:div class=code :)

Added lines 39-40:

(:div class=code :)

September 23, 2010, at 11:59 PM by Tom Igoe -
Changed lines 31-32 from:
to:
September 23, 2010, at 04: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 04:39 PM by Christian Cerrito -
Changed line 3 from:

For Loop

to:

For Loop

September 17, 2010, at 06:11 PM by Christian Cerrito -
Changed lines 46-47 from:
to:

See Also:

September 17, 2010, at 06:10 PM 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 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 digitalWrite() and 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 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 digitalWrite() and delay() functions .

September 17, 2010, at 04:44 PM by Tom Igoe -
Changed lines 44-46 from:

(:divend:

to:

(:divend:)

September 16, 2010, at 04: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 02: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 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 digitalWrite() and 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 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 digitalWrite() and delay() functions .

September 16, 2010, at 01: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 01:56 AM by Christian Cerrito -
September 16, 2010, at 01: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 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 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 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 digitalWrite() and delay() functions .

September 16, 2010, at 01: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 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 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 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 digitalWrite() and delay() .

September 16, 2010, at 01: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 * digitalWrite() and * 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 digitalWrite() and delay() .

September 16, 2010, at 01: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 * digitalWrite() and * delay() .

September 16, 2010, at 01: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 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 for() loop to cycle back and forth through digital pins 2-7.

September 16, 2010, at 01: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 for() loop to cycle back and forth through digital pins 2-7.

September 16, 2010, at 01:43 AM by Christian Cerrito -
Changed lines 49-51 from:
to:
Changed lines 51-54 from:
to:
September 16, 2010, at 01:41 AM by Christian Cerrito -
Changed lines 56-58 from:
  • Forloop? - Control multiple LEDs with a For Loop.
  • Whileloop? - Use a While Loop to calibrate a sensor while a button is being pressed.
  • 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:
  • ForLoop - Control multiple LEDs with a For Loop.
  • WhileLoop - Use a While Loop to calibrate a sensor while a button is being pressed.
  • 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 01:40 AM by Christian Cerrito -
Changed lines 56-58 from:
  • 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.
to:
  • Forloop? - Control multiple LEDs with a For Loop.
  • Whileloop? - Use a While Loop to calibrate a sensor while a button is being pressed.
  • 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 01:38 AM by Christian Cerrito -
September 16, 2010, at 01:38 AM by Christian Cerrito -
September 16, 2010, at 01: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 01: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:

  • if()
  • 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.
September 16, 2010, at 01:24 AM by Christian Cerrito -
Changed lines 25-26 from:
to:
September 15, 2010, at 05: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 23, 2010, at 10:06 PM by Tom Igoe -
Changed lines 31-46 from:
 /*
   For Loop Iteration
  
  Demonstrates the use of a for() loop. 
  Lights multiple LEDs in sequence, then in reverse.
  
  The circuit:
  * LEDs from pins 2 through 7 to ground
  
  created 2006
  by David A. Mellis
  modified 5 Jul 2009
  by Tom Igoe 
  
  http://www.arduino.cc/en/Tutorial/ForLoop
  */
to:
 (:source http://arduino.cc/en/pub/code/master/build/shared/examples/Control/ForLoopIteration/ForLoopIteration.pde language=arduino tabwidth=4:)
Deleted lines 32-60:
 int timer = 100;           // The higher the number, the slower the timing.

 void setup() {
   // use a for loop to initialize each pin as an output:
   for (int thisPin = 2; thisPin < 8; thisPin++)  {
     pinMode(thisPin, OUTPUT);      
   }
 }

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

   // loop from the highest pin to the lowest:
   for (int thisPin = 7; thisPin >= 2; thisPin--) { 
     // turn the pin on:
     digitalWrite(thisPin, HIGH);
     delay(timer);
     // turn the pin off:
     digitalWrite(thisPin, LOW);
   }
 }
September 26, 2009, at 12:43 PM by David A. Mellis -
Changed line 59 from:
   for (int thisPin = 0; thisPin < 8; thisPin++) { 
to:
   for (int thisPin = 2; thisPin < 8; thisPin++) { 
August 27, 2009, at 02:47 PM by Tom Igoe -
Added lines 18-20:

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

July 06, 2009, at 02:12 PM by Tom Igoe -
Changed line 3 from:

For Loop and Arrays

to:

For Loop

July 06, 2009, at 02:11 PM by Tom Igoe -
Added line 25:
Changed line 56 from:
   for (int thisPin = 0; i < 8; thisPin++) { 
to:
   for (int thisPin = 0; thisPin < 8; thisPin++) { 
Changed line 65 from:
   for (thisPin = 7; thisPin >= 2; thisPin--) { 
to:
   for (int thisPin = 7; thisPin >= 2; thisPin--) { 
Changed line 74 from:

(:divend:)

to:

(:divend:)

July 05, 2009, at 02: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 "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 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

Schematic:

click the image to enlarge

Code

(:div class=code :)

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

 int timer = 100;           // The higher the number, the slower the timing.

 void setup() {
   // use a for loop to initialize each pin as an output:
   for (int thisPin = 2; thisPin < 8; thisPin++)  {
     pinMode(thisPin, OUTPUT);      
   }
 }

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

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

(:divend:)




Bookmark and Share