Show minor edits - Show changes to markup
(:source http://arduino.cc/en/pub/code/master/build/shared/examples/5.Control/ForLoopIteration/ForLoopIteration.ino lang=arduino tabwidth=4:)
(:source http://arduino.cc/en/pub/code/master/build/shared/examples/05.Control/ForLoopIteration/ForLoopIteration.ino lang=arduino tabwidth=4:)
(:source https://raw.github.com/arduino/Arduino/build/shared/examples/5.Control/ForLoopIteration/ForLoopIteration.ino lang=arduino tabwidth=4:)
(:source http://arduino.cc/en/pub/code/master/build/shared/examples/5.Control/ForLoopIteration/ForLoopIteration.ino lang=arduino tabwidth=4:)
(:source https://raw.github.com/arduino/Arduino/master/build/shared/examples/5.Control/ForLoopIteration/ForLoopIteration.ino lang=arduino tabwidth=4:)
(:source https://raw.github.com/arduino/Arduino/build/shared/examples/5.Control/ForLoopIteration/ForLoopIteration.ino lang=arduino tabwidth=4:)
(:source https://raw.github.com/arduino/Arduino/master/build/shared/examples/5.Control/ForLoopIteration/ForLoopIteration.pde lang=arduino tabwidth=4:)
(:source https://raw.github.com/arduino/Arduino/master/build/shared/examples/5.Control/ForLoopIteration/ForLoopIteration.ino lang=arduino tabwidth=4:)
(:includeurl https://raw.github.com/arduino/Arduino/master/build/shared/examples/5.Control/ForLoopIteration/ForLoopIteration.pde border=0:)
(:source https://raw.github.com/arduino/Arduino/master/build/shared/examples/5.Control/ForLoopIteration/ForLoopIteration.pde lang=arduino tabwidth=4:)
(:includeurl http://arduino.cc/en/pub/code/master/build/shared/examples/Control/ForLoopIteration/ForLoopIteration.pde border=0:)
(:includeurl https://raw.github.com/arduino/Arduino/master/build/shared/examples/5.Control/ForLoopIteration/ForLoopIteration.pde border=0:)
/* 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 */
(:includeurl http://arduino.cc/en/pub/code/master/build/shared/examples/Control/ForLoopIteration/ForLoopIteration.pde border=0:)
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:)
(:divend:)
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.
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.
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.
Demonstrates the use of a for() loop and arrays.
Demonstrates the use of a for() loop.
modified 17 Jan 2009
modified 5 Jul 2009
http://www.arduino.cc/en/Tutorial/Loop
http://www.arduino.cc/en/Tutorial/ForLoop
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)
int thisPin; // the array elements are numbered from 0 to (pinCount - 1).
for (int thisPin = 0; thisPin < thisPin; thisPin++) { pinMode(ledPins[thisPin], OUTPUT);
for (int thisPin = 2; thisPin < 8; thisPin++) { pinMode(thisPin, OUTPUT);
for (int thisPin = 0; i < pinCount; thisPin++) {
for (int thisPin = 0; i < 8; thisPin++) {
digitalWrite(ledPins[thisPin], HIGH);
digitalWrite(thisPin, HIGH);
digitalWrite(ledPins[thisPin], LOW);
digitalWrite(thisPin, LOW); }
}
for (thisPin = pinCount - 1; thisPin >= 0; thisPin--) {
for (thisPin = 7; thisPin >= 2; thisPin--) {
digitalWrite(ledPins[thisPin], HIGH);
digitalWrite(thisPin, HIGH);
digitalWrite(ledPins[thisPin], LOW);
digitalWrite(thisPin, LOW);
(:div class=code :)
Lights multiple LEDs in sequence, then in recolor=erse.
Lights multiple LEDs in sequence, then in reverse.
(:divend:)
[@ /*
For Loop Iteration
/* 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 */
Demonstrates the use of a for() loop and arrays.
Lights multiple LEDs in sequence, then in reverse.
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)
The circuit:
* LEDs from pins 2 through 7 to ground
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); } }
created 2006 by David A. Mellis modified 17 Jan 2009 by Tom Igoe
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);
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);
}
}
@]
}
// 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); } }
Examples > Digital I/O
Examples > Control Structures
http://static.flickr.com/27/61933851_3b9a25ab42.jpg
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 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).
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.
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.
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
/*
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);
}
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.
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);
for (i = num_pins - 1; i >= 0; i--) {
digitalWrite(pins[i], HIGH);
// loop from the highest pin to the lowest:
for (thisPin = pinCount - 1; thisPin >= 0; thisPin--) {
// turn the pin on:
digitalWrite(ledPins[thisPin], HIGH);
digitalWrite(pins[i], LOW);
// turn the pin off:
digitalWrite(ledPins[thisPin], LOW);
digitalWrite(i, HIGH);
digitalWrite(pins[i], HIGH);
digitalWrite(i, LOW);
digitalWrite(pins[i], LOW);
int timer = 100;
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)
for (i = 2; i <= 7; i++)
pinMode(i, OUTPUT);
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
for (i = 2; i <= 7; 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.
}
for (i = num_pins - 1; i >= 0; i--) {
delay(timer);
for (i = 7; i >= 2; i--) {
digitalWrite(i, HIGH);
delay(timer);
digitalWrite(i, LOW);
delay(timer);
}
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.
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.
Examples > Digital I/O
Examples > Digital I/O
Examples > Digital I/O
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
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);
}
}