## **For循環迭代(霹靂游俠)**
> 你可能經常需要對一系列引腳進行**迭代**,來實現對這些引腳逐個進行操作。例如:本例中使用for循環來讓連接到Arduino/Genuino板2-7號引腳的6個LED逐個、來回地點亮、熄滅(開始由2-7,后來由7-2)。LED的開關通過使**digitalWrite()**和**delay()**來實現。
本例的另一個名字叫做 “**霹靂游俠**“(Knight Rider)。霹靂游俠是80年代的一個電視劇,在劇中,David Hasselhoff 擁有一套人工智能系統 KITT 來駕駛他的Pontiac跑車。片方在他的Pontiac跑車上裝配了很多不同形狀和尺寸的LED來打造一種科幻效果。更值得一提的是:跑車有一個炫酷的顯示屏,顯示屏上有一組線性排列的LED燈來回閃爍。感興趣朋友可以看看這一條視頻:[KITT與KARR的戰斗](https://www.youtube.com/watch?v=PO5E5mQIy_Q)。本例的創作靈感就源于KITT的顯示屏。?

### **所需硬件**
* Arduino板或Genuino板
* 6個220Ω電阻
* 6個LED
* 跳線
* 面包板
* 連接線
### **電路**
?
將六個LED分別串接一個220Ω電阻,連接到Arduino板上的2-7引腳。
### **原理圖**

### **代碼**
下列代碼最開頭就是用了一個**for()**循環來設置2-7引腳為輸出模式。
在**loop**中,有兩個for循環。我們使用for循環來對LED進行迭代,從2-7一個接一個的點亮、熄滅。當最后一個LED亮起時,就反向開始迭代,從7-2。
~~~
/*
For循環迭代(霹靂游俠)
本例展示for循環的使用。
將LED逐個點亮熄滅、然后反向
電路搭建:
* LED連接2-7引腳與GND
代碼公開。
*/
int timer = 100; // 延遲時間
void setup() {
// 用for循環迭代初始化LED的引腳為輸出模式:
for (int thisPin = 2; thisPin < 8; thisPin++) {
pinMode(thisPin, OUTPUT);
}
}
void loop() {
// 從最低引腳迭代到最高引腳:
for (int thisPin = 2; thisPin < 8; thisPin++) {
// 將LED打開:
digitalWrite(thisPin, HIGH);
delay(timer);
// 將LED關閉:
digitalWrite(thisPin, LOW);
}
// 從7-2:
for (int thisPin = 7; thisPin >= 2; thisPin--) {
// 將LED打開:
digitalWrite(thisPin, HIGH);
delay(timer);
// 將LED關閉:
digitalWrite(thisPin, LOW);
}
}
~~~
### **相關資料**
[for()](https://www.arduino.cc/en/Reference/For)?
[digitalWrite()](https://www.arduino.cc/en/Reference/DigitalWrite)?
[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