## **數組**
> 本例中我們使用for循環配合一個數組變量向你展示Arduino中數組的使用。數組是一個有多個”部分”的變量。如果我們把普通變量看做一個盛裝值的“杯子”,那么數組就好像是一個“制冰器”。數組就好像是一連串“杯子”,每個杯子都可以存儲一個值(不過這些值要為相同類型)。?
之前使用**for**循環的例子會對Arduino或Genuino板上2號到7號引腳的LED進行操作,不過有一些限制:LED的引腳必須是連續的,否則就無法完成按照要求一個接一個的亮起。
而在本例中,我們會向你展示如何讓無序的LED燈一個接一個的亮起。為了做到這一點,你需要將引腳的編號放置在一個數組里,然后再for循環中迭代這個數組。(原來是讓一個變量自加來達到引腳編號的切換,而本例是按照有引腳編號的數組進行迭代操作。)
本例使用了6個LED,分別通過220Ω電阻連接到板子上的2-7引腳。不過,LED亮起的順序是由數組中定義的先后順序決定的,因而不一定是2-7(把引腳編號放入數組其實是很簡單的,下面你就會看到,不過要注意,數組元素序號是從0開始的,而且數組的容量是固定的而非可變的)。你可以任意調整引腳順序,不連續的引腳也可以。只要你在數組中將你連接的LED定義進去就可以了。
### **所需硬件**
* Arduino板或Genuino板
* 6個LED
* 6個220Ω電阻
* 跳線
* 面包板
* 連接線
### **電路**
將六個LED用220Ω電阻分別連接到2-7引腳。?

### **原理圖**

### **代碼**
~~~
/*
數組
展示讓無序的LED一個接一個按順序亮起的方法。燈會按照順序先從數組頭
的亮到數組尾,然后反過來由尾到頭。
和從前例子不同的是,本例的LED不是必須連續的。你可以任意改變LED連
接的引腳和順序。只要在數組中定義即可。
電路連接:
* LED從2號引腳連接到GND
代碼公開
*/
int timer = 100; // 數字越大間隔時間越長
int ledPins[] = {2, 7, 4, 6, 5, 3}; // LED引腳編號數組
int pinCount = 6; // 引腳個數(應和LED引腳編號數組相同)
void setup() {
// 數組元素取出時應該是從0開始到pinCount - 1結束。(數組元素是從0開始編號的)
//用for循環初始化引腳:
for (int thisPin = 0; thisPin < pinCount; thisPin++) {
pinMode(ledPins[thisPin], OUTPUT);
}
}
void loop() {
// 從第零個到最后一個:
for (int thisPin = 0; thisPin < pinCount; thisPin++) {
// 給引腳輸出高電平:
digitalWrite(ledPins[thisPin], HIGH);
delay(timer);
// 將引腳輸出低電平:
digitalWrite(ledPins[thisPin], LOW);
}
// 從最后一個到第零個:
for (int thisPin = pinCount - 1; thisPin >= 0; thisPin--) {
// 給引腳輸出高電平:
digitalWrite(ledPins[thisPin], HIGH);
delay(timer);
// 將引腳輸出低電平:
digitalWrite(ledPins[thisPin], LOW);
}
}
~~~
### **相關資料**
[pinMode()](https://www.arduino.cc/en/Reference/PinMode)?
[digitalWrite()](https://www.arduino.cc/en/Reference/DigitalWrite)?
[for()](https://www.arduino.cc/en/Reference/For)?
[delay()](https://www.arduino.cc/en/Reference/Delay)?
- 說明
- 系統示例文件目錄結構及說明
- 01.Basics
- AnalogReadSerial
- BareMinimum
- Blink
- DigitalReadSerial
- Fade
- ReadAnalogVoltage
- 02.Digital
- BlinkWithoutDelay
- Button
- Debounce
- DigitalInputPullup
- StateChangeDetection
- toneKeyboard
- toneMelody
- toneMultiple
- tonePitchFollower
- 03.Analog
- AnalogInOutSerial
- AnalogInput
- AnalogWriteMega
- Calibration
- Fading
- Smoothing
- 04.Communication
- ASCIITable
- Dimmer
- Graph
- Midi
- MultiSerial
- PhysicalPixel
- ReadASCIIString
- SerialCallResponse
- SerialCallResponseASCII
- SerialEvent
- SerialPassthrough
- VirtualColorMixer
- 05.Control
- Arrays
- ForLoopIteration
- IfStatementConditional
- switchCase
- switchCase2
- WhileStatementConditional
- 06.Sensors
- ADXL3xx
- Knock
- Memsic2125
- Ping
- 07.Display
- barGraph
- RowColumnScanning
- 08.Strings
- CharacterAnalysis
- StringAdditionOperator
- StringAppendOperator
- StringCaseChanges
- StringCharacters
- StringComparisonOperators
- StringConstructors
- StringIndexOf
- StringLength
- StringLengthTrim
- StringReplace
- StringStartsWithEndsWith
- StringSubstring
- StringToInt
- 09.USB
- Keyboard
- KeyboardLogout
- KeyboardMessage
- KeyboardReprogram
- KeyboardSerial
- KeyboardAndMouseControl
- Mouse
- ButtonMouseControl
- JoystickMouseControl
- 10.StarterKit_BasicKit (與特定硬件相關,暫無)
- p02_SpaceshipInterface
- p03_LoveOMeter
- p04_ColorMixingLamp
- p05_ServoMoodIndicator
- p06_LightTheremin
- p07_Keyboard
- p08_DigitalHourglass
- p09_MotorizedPinwheel
- p10_Zoetrope
- p11_CrystalBall
- p12_KnockLock
- p13_TouchSensorLamp
- p14_TweakTheArduinoLogo
- p15_HackingButtons
- 11.ArduinoISP(暫無)
- ArduinoISP