Tutorial.Array History

Hide minor edits - Show changes to output

May 02, 2012, at 04:07 PM by Scott Fitzgerald -
Changed line 43 from:
(:source http://arduino.cc/en/pub/code/master/build/shared/examples/5.Control/Arrays/Arrays.ino lang=arduino tabwidth=4:)
to:
(:source http://arduino.cc/en/pub/code/master/build/shared/examples/05.Control/Arrays/Arrays.ino lang=arduino tabwidth=4:)
November 16, 2011, at 04:17 AM by Scott Fitzgerald -
Changed line 43 from:
(:source http://arduino.cc/en/pub/code/new-extension/build/shared/examples/5.Control/Arrays/Arrays.ino lang=arduino tabwidth=4:)
to:
(:source http://arduino.cc/en/pub/code/master/build/shared/examples/5.Control/Arrays/Arrays.ino lang=arduino tabwidth=4:)
September 30, 2011, at 03:19 AM by Scott Fitzgerald -
Changed line 43 from:
(:source http://arduino.cc/en/pub/code/master/build/shared/examples/5.Control/Arrays/Arrays.pde lang=arduino tabwidth=4:)
to:
(:source http://arduino.cc/en/pub/code/new-extension/build/shared/examples/5.Control/Arrays/Arrays.ino lang=arduino tabwidth=4:)
September 23, 2010, at 10:40 PM by Christian Cerrito -
Changed lines 3-4 from:
!! Arrays
to:
!!! Arrays
September 18, 2010, at 12:08 AM by Christian Cerrito -
Changed lines 5-6 from:
This variation on the [[For Loop]] example shows how to use an [[Reference/'''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.
to:
This variation on the [[For Loop]] example shows how to use an [[Reference/'''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.
September 18, 2010, at 12:07 AM by Christian Cerrito -
Changed lines 7-8 from:
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.
to:
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.
Deleted line 12:
September 18, 2010, at 12:03 AM by Christian Cerrito -
Changed lines 5-6 from:
This variation on the [[For Loop]] example shows how to use an [[Reference/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.
to:
This variation on the [[For Loop]] example shows how to use an [[Reference/'''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.
Changed lines 44-58 from:
(:divend:)
to:
(:divend:)


!!!See Also:


* [[Reference/PinMode | pinMode()]]
* [[Reference/DigitalWrite | digitalWrite()]]
* [[Reference/For | for()]]
* [[Reference/Delay | delay()]]

* [[ForLoop | For Loop]] - Control multiple LEDs with a For Loop.
* [[WhileLoop | While Loop]] - Use a While Loop to calibrate a sensor while a button is being pressed.
* [[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.
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/Arrays/Arrays.pde lang=arduino tabwidth=4:)
to:
(:source http://arduino.cc/en/pub/code/master/build/shared/examples/5.Control/Arrays/Arrays.pde lang=arduino tabwidth=4:)
September 16, 2010, at 08:03 AM by Christian Cerrito -
Changed lines 5-6 from:
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.
to:
This variation on the [[For Loop]] example shows how to use an [[Reference/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.
September 16, 2010, at 07:55 AM by Christian Cerrito -
Changed lines 37-38 from:
%width=400px%[[Attach:forLoop_schem.png | Attach:forLoop_schem.png]]
to:
%width=400px%[[Attach:forLoop2_schem.png | Attach:forLoop2_schem.png]]
September 16, 2010, at 07:55 AM by Christian Cerrito -
Added lines 24-25:
Connect six LEDS, with 220 ohm resistors in series, to digital pins 2-7 on your Arduino.
September 16, 2010, at 07:54 AM by Christian Cerrito -
Added lines 14-21:
!!!Hardware Required
* Arduino Board
* (6) 220 ohm resistors
* (6) LEDs
* hook-up wire
* breadboard

September 15, 2010, at 11:28 PM by Christian Cerrito -
Changed line 32 from:
(:source http://arduino.cc/en/pub/code/master/build/shared/examples/Control/Arrays/Arrays.pde lang=arduino tabwidth=4:)
to:
(:source http://arduino.cc/en/pub/code/master/build/shared/examples/5.Control/Arrays/Arrays.pde lang=arduino tabwidth=4:)
February 24, 2010, at 04:07 AM by Tom Igoe -
Changed lines 32-51 from:
%color=#7e7e7e%/*
%color=#7e7e7e% Arrays
%color=#7e7e7e%
%color=#7e7e7e% Demonstrates the use of an array to hold pin numbers
%color=#7e7e7e% in order to iterate over the pins in a sequence.
%color=#7e7e7e% Lights multiple LEDs in sequence, then in reverse.
%color=#7e7e7e%
%color=#7e7e7e% Unlike the For Loop tutorial, where the pins have to be
%color=#7e7e7e% contiguous, here the pins can be in any random order.
%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/Array
%color=#7e7e7e% */
to:
(:source http://arduino.cc/en/pub/code/master/build/shared/examples/Control/Arrays/Arrays.pde lang=arduino tabwidth=4:)
Deleted lines 33-67:
%color=#cc6600%int%% timer = 100; %color=#7e7e7e%// The higher the number, the slower the timing.
%color=#cc6600%int%% ledPins[] = {
2, 7, 4, 6, 5, 3 }; %color=#7e7e7e%// an array of pin numbers to which LEDs are attached
%color=#cc6600%int%% pinCount = 6; %color=#7e7e7e%// the number of pins (i.e. the length of the array)

%color=#cc6600%void%% %color=#cc6600%'''setup'''%%() {
%color=#cc6600%int%% thisPin;
%color=#7e7e7e%// the array elements are numbered from 0 to (pinCount - 1).
%color=#7e7e7e%// use a for loop to initialize each pin as an output:
%color=#cc6600%for%% (%color=#cc6600%int%% thisPin = 0; thisPin < pinCount; thisPin++) {
%color=#cc6600%pinMode%%(ledPins[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; thisPin < pinCount; thisPin++) {
%color=#7e7e7e%// turn the pin on:
%color=#cc6600%digitalWrite%%(ledPins[thisPin], %color=#006699%HIGH%%);
%color=#cc6600%delay%%(timer);
%color=#7e7e7e%// turn the pin off:
%color=#cc6600%digitalWrite%%(ledPins[thisPin], %color=#006699%LOW%%);

}

%color=#7e7e7e%// loop from the highest pin to the lowest:
%color=#cc6600%for%% (%color=#cc6600%int%% thisPin = pinCount - 1; thisPin >= 0; thisPin--) {
%color=#7e7e7e%// turn the pin on:
%color=#cc6600%digitalWrite%%(ledPins[thisPin], %color=#006699%HIGH%%);
%color=#cc6600%delay%%(timer);
%color=#7e7e7e%// turn the pin off:
%color=#cc6600%digitalWrite%%(ledPins[thisPin], %color=#006699%LOW%%);
}
}
August 27, 2009, at 08:47 PM by Tom Igoe -
Added lines 20-22:
[-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 66 from:
%color=#cc6600%for%% (%color=#cc6600%int%% thisPin = 0; i < pinCount; thisPin++) {
to:
%color=#cc6600%for%% (%color=#cc6600%int%% thisPin = 0; thisPin < pinCount; thisPin++) {
Changed line 76 from:
%color=#cc6600%for%% (thisPin = pinCount - 1; thisPin >= 0; thisPin--) {
to:
%color=#cc6600%for%% (%color=#cc6600%int%% thisPin = pinCount - 1; thisPin >= 0; thisPin--) {
July 05, 2009, at 09:04 PM by Tom Igoe -
Added lines 1-85:
''Examples > Control Structures''

!! Arrays

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.

!!!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% Arrays
%color=#7e7e7e%
%color=#7e7e7e% Demonstrates the use of an array to hold pin numbers
%color=#7e7e7e% in order to iterate over the pins in a sequence.
%color=#7e7e7e% Lights multiple LEDs in sequence, then in reverse.
%color=#7e7e7e%
%color=#7e7e7e% Unlike the For Loop tutorial, where the pins have to be
%color=#7e7e7e% contiguous, here the pins can be in any random order.
%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/Array
%color=#7e7e7e% */

%color=#cc6600%int%% timer = 100; %color=#7e7e7e%// The higher the number, the slower the timing.
%color=#cc6600%int%% ledPins[] = {
2, 7, 4, 6, 5, 3 }; %color=#7e7e7e%// an array of pin numbers to which LEDs are attached
%color=#cc6600%int%% pinCount = 6; %color=#7e7e7e%// the number of pins (i.e. the length of the array)

%color=#cc6600%void%% %color=#cc6600%'''setup'''%%() {
%color=#cc6600%int%% thisPin;
%color=#7e7e7e%// the array elements are numbered from 0 to (pinCount - 1).
%color=#7e7e7e%// use a for loop to initialize each pin as an output:
%color=#cc6600%for%% (%color=#cc6600%int%% thisPin = 0; thisPin < pinCount; thisPin++) {
%color=#cc6600%pinMode%%(ledPins[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 < pinCount; thisPin++) {
%color=#7e7e7e%// turn the pin on:
%color=#cc6600%digitalWrite%%(ledPins[thisPin], %color=#006699%HIGH%%);
%color=#cc6600%delay%%(timer);
%color=#7e7e7e%// turn the pin off:
%color=#cc6600%digitalWrite%%(ledPins[thisPin], %color=#006699%LOW%%);

}

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

(:divend:)

Share