Iterative Arrays: The Sequential LED Chaser
A beginner frequently completely destroys their code by literally writing digitalWrite(2, HIGH); delay(100); digitalWrite(2, LOW); digitalWrite(3, HIGH)... spanning hundreds of lines exactly to make 10 LEDs chase each other linearly! The Sequential LED Chaser entirely revolutionizes programmatic architecture cleanly! By deeply assigning the explicit Arduino hardware pin numbers securely into a One-Dimensional Integer Array, the developer strictly utilizes a massive for loop matrix. This brutally compresses 100 lines of chaotic unmanageable delay sequences completely into exactly four elegant lines of pure absolute C++ iteration natively!
Demystifying C++ for Loop Architecture
The physical array acts completely like a filing cabinet cleanly organizing the exact hardware pin numbers automatically.
int pinArray[] = {2, 3, 4, 5, 6, 7};(Index 0 physically represents Pin 2!).- A
forloop acts as an absolute mathematical incrementing engine cleanly:for (int i = 0; i < 6; i++). - The incredibly intense engine injects
idirectly into the array precisely:digitalWrite(pinArray[i], HIGH);. - It blasts "ON", waits exactly
100ms, turns "OFF", and aggressively jumps accurately entirely to the next physical hardware pin instantly!
int ledPins[] = { 2, 3, 4, 5, 6, 7 }; // The hardware matrix array!
int pinCount = 6;
int timer = 80; // The execution speed of the Light Chaser completely!
void setup() {
// Ultra-efficient pinMode iteration utilizing the Array seamlessly!
for (int thisPin = 0; thisPin < pinCount; thisPin++) {
pinMode(ledPins[thisPin], OUTPUT);
}
}
void loop() {
// Knight-Rider Ping-Pong Execution Matrix!
// SCAN COMPLETELY FORWARD!
for (int thisPin = 0; thisPin < pinCount; thisPin++) {
digitalWrite(ledPins[thisPin], HIGH); // Ignite!
delay(timer); // Render frame!
digitalWrite(ledPins[thisPin], LOW); // Extinguish!
}
// SCAN COMPLETELY BACKWARD!
// Start explicit math at index 4 (Because index 5 is the absolute edge!)
for (int thisPin = pinCount - 2; thisPin > 0; thisPin--) {
digitalWrite(ledPins[thisPin], HIGH);
delay(timer);
digitalWrite(ledPins[thisPin], LOW);
}
}
Eliminating The "Cylon" Ghosting Danger
If you connect entirely 6 Red LEDs together without 220-Ohm resistors, the internal micro-capacitor traces inside the Arduino die cannot safely discharge correctly!
- This causes "Ghosting," where neighboring LEDs faintly glow entirely uncontrollably natively!
- Worse, chaining LEDs natively directly to
GNDguarantees drawing over100mAconcurrently during fast sequences, completely collapsing the ATmega328P voltage regulators physically! - You absolute MUST solder/breadboard exactly
Six 220-OhmPhysical resistors cleanly isolating each individual LED vector precisely from its corresponding output pin completely perfectly!
Output Array Hardware Configuration
- Arduino Uno/Nano (Handling the continuous array-bounds looping inherently).
- 6x identical 5mm LEDs (Red or Blue produce the most profound visual Cylon scanning logic visually).
- 6x 220-Ohm Resistors (Providing strict current constraints across the processor natively!).
- An Analog Potentiometer (Optional) (If you attach a knob to
A0, you can mathematically map theanalogReadprecisely into thetimervariable seamlessly, allowing the human to dynamically spin the absolute speed of the chase logic flawlessly in real-time!).
Code Example (Basic Sequential On/Off):
/* A simple program to sequentially turn on and turn off 10 LEDs */
int LED3 = 11;
int LED4 = 10;
int LED5 = 9;
int LED6 = 8;
int LED7 = 7;
int LED8 = 6;
int LED9 = 5;
int LED10 = 4;
int LED11 = 3;
int LED12 = 2;
void setup() {
pinMode(LED3, OUTPUT);
pinMode(LED4, OUTPUT);
pinMode(LED5, OUTPUT);
pinMode(LED6, OUTPUT);
pinMode(LED7, OUTPUT);
pinMode(LED8, OUTPUT);
pinMode(LED9, OUTPUT);
pinMode(LED10, OUTPUT);
pinMode(LED11, OUTPUT);
pinMode(LED12, OUTPUT);
}
void loop() {
digitalWrite(LED3, HIGH); // turn on LED3
delay(100); // wait for 200ms
digitalWrite(LED4, HIGH); // turn on LED4
delay(100); // wait for 200ms
digitalWrite(LED5, HIGH); // turn on LED5
delay(100); // wait for 200ms
digitalWrite(LED6, HIGH); // turn on LED6
delay(100); // wait for 200ms
digitalWrite(LED7, HIGH); // turn on LED7
delay(100); // wait for 200ms
digitalWrite(LED8, HIGH); // turn on LED8
delay(100); // wait for 200ms
digitalWrite(LED9, HIGH); // turn on LED9
delay(100); // wait for 200ms
digitalWrite(LED10, HIGH); // turn on LED10
delay(100); // wait for 200ms
digitalWrite(LED11, HIGH); // turn on LED11
delay(100); // wait for 200ms
digitalWrite(LED12, HIGH); // turn on LED12
delay(100); // wait for 200ms
digitalWrite(LED3, LOW); // turn off LED3
delay(100); // wait for 300ms
digitalWrite(LED4, LOW); // turn off LED4
delay(100); // wait for 300ms
digitalWrite(LED5, LOW); // turn off LED5
delay(100); // wait for 300ms
digitalWrite(LED6, LOW); // turn off LED6
delay(100); // wait for 300ms
digitalWrite(LED7, LOW); // turn off LED7
delay(100); // wait for 300ms
digitalWrite(LED8, LOW); // turn off LED8
delay(100); // wait for 300ms
digitalWrite(LED9, LOW); // turn off LED9
delay(100); // wait for 300ms
digitalWrite(LED10, LOW); // turn off LED10
delay(100); // wait for 300ms
digitalWrite(LED11, LOW); // turn off LED11
delay(100); // wait for 300ms
digitalWrite(LED12, LOW); // turn off LED8
delay(100); // wait for 300ms before running program all over again
}