Tutorial.Loop History

Hide minor edits - Show changes to markup

September 06, 2012, at 12:44 PM by David Cuartielles -
Changed line 27 from:
to:
January 16, 2012, at 03:19 PM by David Cuartielles -
Changed line 27 from:
to:
August 02, 2011, at 09:17 AM by Davide Gomba -
Changed line 27 from:
to:
August 02, 2011, at 09:15 AM by Davide Gomba -
Changed line 27 from:
to:
February 28, 2010, at 08:32 PM by Tom Igoe -
Changed line 3 from:

For Loop and Arrays

to:

For Loop

Changed lines 27-42 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:
Changed lines 29-58 from:
to:
July 05, 2009, at 08:54 PM by Tom Igoe -
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. The simplest way to do this is to put the pin numbers in an array and then use for loops to iterate over the array.

to:

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.

Deleted lines 11-12:

This technique of putting the pins in an array is very handy. You don't have to have the pins sequential to one another, or even in the same order. You can rearrange them however you want.

Changed line 30 from:
  Demonstrates the use of a for() loop and arrays. 
to:
  Demonstrates the use of a for() loop. 
Changed line 38 from:
  modified 17 Jan 2009
to:
  modified 5 Jul 2009
Changed line 41 from:
  http://www.arduino.cc/en/Tutorial/Loop
to:
  http://www.arduino.cc/en/Tutorial/ForLoop
Deleted lines 44-46:
 int ledPins[] = { 
   2, 3, 4, 5, 6, 7 };       // an array of pin numbers to which LEDs are attached
 int pinCount = 6;           // the number of pins (i.e. the length of the array)
Deleted lines 46-47:
   int thisPin;
   // the array elements are numbered from 0 to (pinCount - 1).
Changed lines 48-49 from:
   for (int thisPin = 0; thisPin < thisPin; thisPin++)  {
     pinMode(ledPins[thisPin], OUTPUT);      
to:
   for (int thisPin = 2; thisPin < 8; thisPin++)  {
     pinMode(thisPin, OUTPUT);      
Changed line 55 from:
   for (int thisPin = 0; i < pinCount; thisPin++) { 
to:
   for (int thisPin = 0; i < 8; thisPin++) { 
Changed line 57 from:
     digitalWrite(ledPins[thisPin], HIGH);   
to:
     digitalWrite(thisPin, HIGH);   
Changed lines 60-61 from:
     digitalWrite(ledPins[thisPin], LOW);    
to:
     digitalWrite(thisPin, LOW);    
   }
Deleted lines 62-63:
   }
Changed line 64 from:
   for (thisPin = pinCount - 1; thisPin >= 0; thisPin--) { 
to:
   for (thisPin = 7; thisPin >= 2; thisPin--) { 
Changed line 66 from:
     digitalWrite(ledPins[thisPin], HIGH);
to:
     digitalWrite(thisPin, HIGH);
Changed line 69 from:
     digitalWrite(ledPins[thisPin], LOW);
to:
     digitalWrite(thisPin, LOW);
July 05, 2009, at 07:23 PM by Tom Igoe -
Changed lines 27-28 from:
to:
Changed line 33 from:
  Lights multiple LEDs in sequence, then in recolor=erse.
to:
  Lights multiple LEDs in sequence, then in reverse.
Added lines 80-81:
July 05, 2009, at 06:46 PM by Tom Igoe -
Changed lines 4-5 from:

(aka Kinght Rider)

to:

(aka Knight Rider)

July 04, 2009, at 02:32 AM by Tom Igoe -
Changed lines 28-30 from:

[@ /*

  For Loop Iteration
to:
 /*
   For Loop Iteration
  
  Demonstrates the use of a for() loop and arrays. 
  Lights multiple LEDs in sequence, then in recolor=erse.
  
  The circuit:
  * LEDs from pins 2 through 7 to ground
  
  created 2006
  by David A. Mellis
  modified 17 Jan 2009
  by Tom Igoe 
  
  http://www.arduino.cc/en/Tutorial/Loop
  */
Changed lines 45-46 from:
 Demonstrates the use of a for() loop and arrays. 
 Lights multiple LEDs in sequence, then in reverse.
to:
 int timer = 100;           // The higher the number, the slower the timing.
 int ledPins[] = { 
   2, 3, 4, 5, 6, 7 };       // an array of pin numbers to which LEDs are attached
 int pinCount = 6;           // the number of pins (i.e. the length of the array)
Changed lines 50-51 from:
 The circuit:
 * LEDs from pins 2 through 7 to ground
to:
 void setup() {
   int thisPin;
   // the array elements are numbered from 0 to (pinCount - 1).
   // use a for loop to initialize each pin as an output:
   for (int thisPin = 0; thisPin < thisPin; thisPin++)  {
     pinMode(ledPins[thisPin], OUTPUT);      
   }
 }
Changed lines 59-62 from:
 created 2006
 by David A. Mellis
 modified 17 Jan 2009
 by Tom Igoe 
to:
 void loop() {
   // loop from the lowest pin to the highest:
   for (int thisPin = 0; i < pinCount; thisPin++) { 
     // turn the pin on:
     digitalWrite(ledPins[thisPin], HIGH);   
     delay(timer);                  
     // turn the pin off:
     digitalWrite(ledPins[thisPin], LOW);    
Changed lines 68-106 from:
 http://www.arduino.cc/en/Tutorial/Loop
 */

int timer = 100; // The higher the number, the slower the timing. int ledPins[] = {

  2, 3, 4, 5, 6, 7 };       // an array of pin numbers to which LEDs are attached

int pinCount = 6; // the number of pins (i.e. the length of the array)

void setup() {

  int thisPin;
  // the array elements are numbered from 0 to (pinCount - 1).
  // use a for loop to initialize each pin as an output:
  for (int thisPin = 0; thisPin < thisPin; thisPin++)  {
    pinMode(ledPins[thisPin], OUTPUT);      
  }

}

void loop() {

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

  }

  // loop from the highest pin to the lowest:
  for (thisPin = pinCount - 1; thisPin >= 0; thisPin--) { 
    // turn the pin on:
    digitalWrite(ledPins[thisPin], HIGH);
    delay(timer);
    // turn the pin off:
    digitalWrite(ledPins[thisPin], LOW);
  }

}

@]

to:
   }

   // loop from the highest pin to the lowest:
   for (thisPin = pinCount - 1; thisPin >= 0; thisPin--) { 
     // turn the pin on:
     digitalWrite(ledPins[thisPin], HIGH);
     delay(timer);
     // turn the pin off:
     digitalWrite(ledPins[thisPin], LOW);
   }
 }
June 25, 2009, at 09:09 PM by Tom Igoe -
Changed lines 1-2 from:

Examples > Digital I/O

to:

Examples > Control Structures

June 25, 2009, at 07:26 PM by Tom Igoe -
Changed lines 16-17 from:

http://static.flickr.com/27/61933851_3b9a25ab42.jpg

to:

click the image to enlarge

Schematic:

click the image to enlarge

June 25, 2009, at 12:19 AM by Tom Igoe -
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(pinNumner,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(pinNumber,HIGH/LOW) and delay(time).

June 25, 2009, at 12:19 AM by Tom Igoe -
Changed lines 3-10 from:

Loop

We also call this example "Knight Rider" in memory to a TV-series from the 80's where the famous David Hasselhoff had an AI machine driving his Pontiac. The car had been augmented with plenty of LEDs in all possible sizes performing flashy effects.

Thus we decided that in order to learn more about sequential programming and good programming techniques for the I/O board, it would be interesting to use the Knight Rider as a metaphor.

This example makes use of 6 LEDs connected to the pins 2 - 7 on the board using 220 Ohm resistors. The first code example will make the LEDs blink in a sequence, one by one using only digitalWrite(pinNum,HIGH/LOW) and delay(time). The second example shows how to use a for(;;) construction to perform the very same thing, but in fewer lines. The third and last example concentrates in the visual effect of turning the LEDs on/off in a more softer way.

to:

For Loop and Arrays

(aka Kinght 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. The simplest way to do this is to put the pin numbers in an array and then use for loops to iterate over the array.

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(pinNumner,HIGH/LOW) and delay(time).

This technique of putting the pins in an array is very handy. You don't have to have the pins sequential to one another, or even in the same order. You can rearrange them however you want.

Changed lines 21-30 from:

int timer = 100; // The higher the number, the slower the timing. int pins[] = { 2, 3, 4, 5, 6, 7 }; // an array of pin numbers int num_pins = 6; // the number of pins (i.e. the length of the array)

void setup() {

  int i;

  for (i = 0; i < num_pins; i++)   // the array elements are numbered from 0 to num_pins - 1
    pinMode(pins[i], OUTPUT);      // set each pin as an output
to:

/*

  For Loop Iteration

 Demonstrates the use of a for() loop and arrays. 
 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 17 Jan 2009
 by Tom Igoe 

 http://www.arduino.cc/en/Tutorial/Loop
 */

int timer = 100; // The higher the number, the slower the timing. int ledPins[] = {

  2, 3, 4, 5, 6, 7 };       // an array of pin numbers to which LEDs are attached

int pinCount = 6; // the number of pins (i.e. the length of the array)

void setup() {

  int thisPin;
  // the array elements are numbered from 0 to (pinCount - 1).
  // use a for loop to initialize each pin as an output:
  for (int thisPin = 0; thisPin < thisPin; thisPin++)  {
    pinMode(ledPins[thisPin], OUTPUT);      
  }
Changed lines 52-59 from:

void loop() {

  int i;

  for (i = 0; i < num_pins; i++) { // loop through each pin...
    digitalWrite(pins[i], HIGH);   // turning it on,
    delay(timer);                  // pausing,
    digitalWrite(pins[i], LOW);    // and turning it off.
to:

void loop() {

  // loop from the lowest pin to the highest:
  for (int thisPin = 0; i < pinCount; thisPin++) { 
    // turn the pin on:
    digitalWrite(ledPins[thisPin], HIGH);   
    delay(timer);                  
    // turn the pin off:
    digitalWrite(ledPins[thisPin], LOW);    
Changed lines 62-63 from:
  for (i = num_pins - 1; i >= 0; i--) { 
    digitalWrite(pins[i], HIGH);
to:
  // loop from the highest pin to the lowest:
  for (thisPin = pinCount - 1; thisPin >= 0; thisPin--) { 
    // turn the pin on:
    digitalWrite(ledPins[thisPin], HIGH);
Changed lines 68-69 from:
    digitalWrite(pins[i], LOW);
to:
    // turn the pin off:
    digitalWrite(ledPins[thisPin], LOW);
Added line 72:
September 24, 2007, at 03:37 AM by David A. Mellis -
Changed line 40 from:
    digitalWrite(i, HIGH);
to:
    digitalWrite(pins[i], HIGH);
Changed line 42 from:
    digitalWrite(i, LOW);
to:
    digitalWrite(pins[i], LOW);
April 22, 2007, at 07:20 PM by David A. Mellis -
Changed lines 18-19 from:

int timer = 100;

to:

int timer = 100; // The higher the number, the slower the timing. int pins[] = { 2, 3, 4, 5, 6, 7 }; // an array of pin numbers int num_pins = 6; // the number of pins (i.e. the length of the array)

Changed lines 26-27 from:
  for (i = 2; i <= 7; i++)
    pinMode(i, OUTPUT);
to:
  for (i = 0; i < num_pins; i++)   // the array elements are numbered from 0 to num_pins - 1
    pinMode(pins[i], OUTPUT);      // set each pin as an output
Changed lines 34-39 from:
  for (i = 2; i <= 7; i++) {
to:
  for (i = 0; i < num_pins; i++) { // loop through each pin...
    digitalWrite(pins[i], HIGH);   // turning it on,
    delay(timer);                  // pausing,
    digitalWrite(pins[i], LOW);    // and turning it off.
  }
  for (i = num_pins - 1; i >= 0; i--) { 
Deleted line 42:
    delay(timer);
Deleted lines 43-48:
  for (i = 7; i >= 2; i--) {
    digitalWrite(i, HIGH);
    delay(timer);
    digitalWrite(i, LOW);
    delay(timer);
  }
April 15, 2007, at 05:33 PM by David A. Mellis -
Changed lines 3-6 from:

Knight Rider

We have named this example in memory to a TV-series from the 80's where the famous David Hasselhoff had an AI machine driving his Pontiac. The car had been augmented with plenty of LEDs in all possible sizes performing flashy effects.

to:

Loop

We also call this example "Knight Rider" in memory to a TV-series from the 80's where the famous David Hasselhoff had an AI machine driving his Pontiac. The car had been augmented with plenty of LEDs in all possible sizes performing flashy effects.

January 29, 2007, at 06:39 PM by David A. Mellis -
Changed lines 3-4 from:

Loop

to:

Knight Rider

January 28, 2007, at 12:01 PM by David A. Mellis -
Changed lines 1-4 from:

Examples > Digital I/O

loop

to:

Examples > Digital I/O

Loop

January 28, 2007, at 11:40 AM by David A. Mellis -
Added lines 11-12:

Circuit

Added lines 15-16:

Code

January 28, 2007, at 11:40 AM by David A. Mellis -
Changed lines 1-2 from:

Knight Rider

to:

Examples > Digital I/O

loop

January 28, 2007, at 11:28 AM by David A. Mellis -
Added lines 1-10:

Knight Rider

We have named this example in memory to a TV-series from the 80's where the famous David Hasselhoff had an AI machine driving his Pontiac. The car had been augmented with plenty of LEDs in all possible sizes performing flashy effects.

Thus we decided that in order to learn more about sequential programming and good programming techniques for the I/O board, it would be interesting to use the Knight Rider as a metaphor.

This example makes use of 6 LEDs connected to the pins 2 - 7 on the board using 220 Ohm resistors. The first code example will make the LEDs blink in a sequence, one by one using only digitalWrite(pinNum,HIGH/LOW) and delay(time). The second example shows how to use a for(;;) construction to perform the very same thing, but in fewer lines. The third and last example concentrates in the visual effect of turning the LEDs on/off in a more softer way.

http://static.flickr.com/27/61933851_3b9a25ab42.jpg

January 14, 2007, at 03:25 PM by David A. Mellis -
Added lines 1-29:
int timer = 100;

void setup()
{
  int i;

  for (i = 2; i <= 7; i++)
    pinMode(i, OUTPUT);
}

void loop()
{
  int i;

  for (i = 2; i <= 7; i++) {
    digitalWrite(i, HIGH);
    delay(timer);
    digitalWrite(i, LOW);
    delay(timer);
  }
  for (i = 7; i >= 2; i--) {
    digitalWrite(i, HIGH);
    delay(timer);
    digitalWrite(i, LOW);
    delay(timer);
  }
}

Share