Hide minor edits - Show changes to markup
(:source http://github.com/arduino/Arduino/raw/master/build/shared/examples/5.Control/Arrays/Arrays.ino lang=arduino tabwidth=4:)
(:source http://github.com/arduino/Arduino/raw/master/build/shared/examples/05.Control/Arrays/Arrays.ino lang=arduino tabwidth=4:)
(:source http://github.com/arduino/Arduino/raw/new-extension/build/shared/examples/5.Control/Arrays/Arrays.ino lang=arduino tabwidth=4:)
(:source http://github.com/arduino/Arduino/raw/master/build/shared/examples/5.Control/Arrays/Arrays.ino lang=arduino tabwidth=4:)
(:source http://github.com/arduino/Arduino/raw/master/build/shared/examples/5.Control/Arrays/Arrays.pde lang=arduino tabwidth=4:)
(:source http://github.com/arduino/Arduino/raw/new-extension/build/shared/examples/5.Control/Arrays/Arrays.ino lang=arduino tabwidth=4:)
This variation on the For Loop example shows how to use an Array. An array is a variable with multiple parts. If you think of a variable as a cup that holds values, you might think of an array as an ice cube tray. It's like a series of linked cups, all of which can hold the same maximum value.
The For Loop example shows you how to light up a series of LEDs attached to pins 2 through 7 of the Arduino. But the limitation is that the pins have to be numbered contiguously, and they have to be turned on in sequence. This example shows you how you can turn on a sequence of pins whose numbers are neither contiguous nor necessarily sequential. To do this is, you can put the pin numbers in an array and then use for loops to iterate over the array.
The For Loop example shows you how to light up a series of LEDs attached to pins 2 through 7 of the Arduino, with certain limitations (the pins have to be numbered contiguously, and the LEDs have to be turned on in sequence).
This example shows you how you can turn on a sequence of pins whose numbers are neither contiguous nor necessarily sequential. To do this is, you can put the pin numbers in an array and then use for loops to iterate over the array.
This variation on the For Loop example shows how to use an Array. An array is a variable with multiple parts. If you think of a variable as a cup that holds values, you might think of an array as an ice cube tray. It's like a series of linked cups, all of which can hold the same maximum value.
This variation on the For Loop example shows how to use an Array. An array is a variable with multiple parts. If you think of a variable as a cup that holds values, you might think of an array as an ice cube tray. It's like a series of linked cups, all of which can hold the same maximum value.
(:divend:)
(:divend:)
(:source http://github.com/arduino/Arduino/raw/master/build/shared/examples/5.Control/Arrays/Arrays.pde lang=arduino tabwidth=4:)
(:source http://github.com/arduino/Arduino/raw/master/build/shared/examples/5.Control/Arrays/Arrays.pde lang=arduino tabwidth=4:)
This variation on the For Loop example shows how to use an array. An array is a variable with multiple parts. If you think of a variable as a cup that holds values, you might think of an array as an ice cube tray. It's like a series of linked cups, all of which can hold the same maximum value.
Connect six LEDS, with 220 ohm resistors in series, to digital pins 2-7 on your Arduino.
(:source http://github.com/arduino/Arduino/raw/master/build/shared/examples/Control/Arrays/Arrays.pde lang=arduino tabwidth=4:)
(:source http://github.com/arduino/Arduino/raw/master/build/shared/examples/5.Control/Arrays/Arrays.pde lang=arduino tabwidth=4:)
/* Arrays Demonstrates the use of an array to hold pin numbers in order to iterate over the pins in a sequence. Lights multiple LEDs in sequence, then in reverse. Unlike the For Loop tutorial, where the pins have to be contiguous, here the pins can be in any random order. 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/Array */
(:source http://github.com/arduino/Arduino/raw/master/build/shared/examples/Control/Arrays/Arrays.pde lang=arduino tabwidth=4:)
int timer = 100; // The higher the number, the slower the timing. int ledPins[] = { 2, 7, 4, 6, 5, 3 }; // 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 < pinCount; thisPin++) { pinMode(ledPins[thisPin], OUTPUT); } }
void loop() { // loop from the lowest pin to the highest: for (int thisPin = 0; thisPin < 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 (int thisPin = pinCount - 1; thisPin >= 0; thisPin--) { // turn the pin on: digitalWrite(ledPins[thisPin], HIGH); delay(timer); // turn the pin off: digitalWrite(ledPins[thisPin], LOW); } }
image developed using Fritzing. For more circuit examples, see the Fritzing project page
for (int thisPin = 0; i < pinCount; thisPin++) {
for (int thisPin = 0; thisPin < pinCount; thisPin++) {
for (thisPin = pinCount - 1; thisPin >= 0; thisPin--) {
for (int thisPin = pinCount - 1; thisPin >= 0; thisPin--) {
Examples > Control Structures
This variation on the For Loop example shows how to use an array. An array is a variable with multiple parts. If you think of a variable as a cup that holds values, you might think of an array as an ice cube tray. It's like a series of linked cups, all of which can hold the same maximum value.
The For Loop example shows you how to light up a series of LEDs attached to pins 2 through 7 of the Arduino. But the limitation is that the pins have to be numbered contiguously, and they have to be turned on in sequence. This example shows you how you can turn on a sequence of pins whose numbers are neither contiguous nor necessarily sequential. To do this is, you can put the pin numbers in an array and then use for loops to iterate over the array.
This example makes use of 6 LEDs connected to the pins 2 - 7 on the board using 220 Ohm resistors, just like in the For Loop. However, here the order of the LEDs is determined by their order in the array, not by their physical order.
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.
click the image to enlarge
Schematic:
click the image to enlarge
(:div class=code :)
/* Arrays Demonstrates the use of an array to hold pin numbers in order to iterate over the pins in a sequence. Lights multiple LEDs in sequence, then in reverse. Unlike the For Loop tutorial, where the pins have to be contiguous, here the pins can be in any random order. 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/Array */
int timer = 100; // The higher the number, the slower the timing. int ledPins[] = { 2, 7, 4, 6, 5, 3 }; // 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 < pinCount; 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); } }
(:divend:)