Sie sind auf Seite 1von 4

Search the Arduino Website

Learning Examples (http://arduino.cc/en/Tutorial/HomePage) | Foundations (http://arduino.cc/en/Tutorial/Foundations) | Hacking


(http://arduino.cc/en/Hacking/HomePage) | Links (http://arduino.cc/en/Tutorial/Links)

Examples > Control Structures

Arrays
This variation on the For Loop (http://arduino.cc/en/Tutorial/ForLoop) example shows how to use an array
(http://arduino.cc/en/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.
The For Loop (http://arduino.cc/en/Tutorial/ForLoop) 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 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.

Hardware Required
- Arduino Board
- (6) 220 ohm resistors
- (6) LEDs
- hook-up wire
- breadboard

Circuit
Connect six LEDS, with 220 ohm resistors in series, to digital pins 2-7 on your Arduino.
click the image to enlarge

(http://arduino.cc/en/uploads/Tutorial/forLoop_bb.png)

image developed using Fritzing (http://www.fritzing.org). For more circuit examples, see the Fritzing project page (http://fritzing.org/projects/)

Schematic:
click the image to enlarge

(http://arduino.cc/en/uploads/Tutorial/forLoop2_schem.png)

Code
/*
Arrays

Demonstratestheuseofanarraytoholdpinnumbers
inordertoiterateoverthepinsinasequence.
LightsmultipleLEDsinsequence,theninreverse.

UnliketheForLooptutorial,wherethepinshavetobe
contiguous,herethepinscanbeinanyrandomorder.

Thecircuit:
*LEDsfrompins2through7toground

created2006
byDavidA.Mellis
modified30Aug2011
byTomIgoe
Thisexamplecodeisinthepublicdomain.

http://www.arduino.cc/en/Tutorial/Array
*/
inttimer=100;//Thehigherthenumber,theslowerthetiming.
intledPins[]={
2,7,4,6,5,3};//anarrayofpinnumberstowhichLEDsareattached
intpinCount=6;//thenumberofpins(i.e.thelengthofthearray)
voidsetup(){
//thearrayelementsarenumberedfrom0to(pinCount1).
//useaforlooptoinitializeeachpinasanoutput:
for(intthisPin=0;thisPin<pinCount;thisPin++){
pinMode(ledPins[thisPin],OUTPUT);
}
}
voidloop(){
//loopfromthelowestpintothehighest:
for(intthisPin=0;thisPin<pinCount;thisPin++){
//turnthepinon:
digitalWrite(ledPins[thisPin],HIGH);
delay(timer);
//turnthepinoff:
digitalWrite(ledPins[thisPin],LOW);
}
//loopfromthehighestpintothelowest:
for(intthisPin=pinCount1;thisPin>=0;thisPin){
//turnthepinon:
digitalWrite(ledPins[thisPin],HIGH);
delay(timer);
//turnthepinoff:
digitalWrite(ledPins[thisPin],LOW);
}
}

[GetCode](http://arduino.cc/en/Tutorial/Array?action=sourceblock&num=1)

See Also:
- pinMode() (http://arduino.cc/en/Reference/PinMode)
- digitalWrite() (http://arduino.cc/en/Reference/DigitalWrite)
- for() (http://arduino.cc/en/Reference/For)
- delay() (http://arduino.cc/en/Reference/Delay)
- For Loop (http://arduino.cc/en/Tutorial/ForLoop) - Control multiple LEDs with a For Loop.
- While Loop (http://arduino.cc/en/Tutorial/WhileLoop) - Use a While Loop to calibrate a sensor while a button is being pressed.
- Switch Case (http://arduino.cc/en/Tutorial/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.

Share

NEWSLETTER

Enter your email to sign up

2015 Arduino

Copyright Notice (http://arduino.cc/en/Main/CopyrightNotice)

About us (http://arduino.cc/en/Main/AboutUs)

(https://twitter.com/arduino)

Contact us (http://arduino.cc/en/Main/ContactUs)

Careers (http://arduino.cc/Careers)

(http://www.facebook.com/official.arduino)

(https://plus.google.com/+Arduino)
(http://youtube.com/arduinoteam)

(http://www.flickr.com/photos/arduino_cc)

Das könnte Ihnen auch gefallen