Blink LEDs in Stack Form Using for loop

To blink LEDs in stack form using for loop we first know about “Stack  (click) and basic program of LEDs stack pattern (previous topic)“.

Required hardware or components for Lighting up LED in Stack form using Arduino

S.N.ComponentQuantity
1.Arduino Uno1
2.Breadboard1
3.LED4
4.Resistor 220 or 280 ohm4

CIRCUIT DIAGRAM : BLINK LEDS IN STACK FORM USING FOR LOOP

BLINK LEDS IN STACK FORM USING FOR LOOP
Circuit diagram Arduino and many leds

CONNECTION TABLE

S.N.ArduinoLED
1.GNDCathode (-)
2.2LED 1 - Anode (+)
3.3LED 2 - Anode (+)
4.4LED 3 - Anode (+)
5.5LED 4 - Anode (+)

ARDUINO PROGRAMMING CODE : BLINK LEDS IN STACK FORM USING FOR LOOP

int i;
void setup() {
  for (i = 2 ; i <= 5; i++) {
    pinMode(i, OUTPUT);
  }
}

void loop() {
  for (i = 2; i <= 5 ; i++) {
    digitalWrite(i, HIGH);
    delay(500);
  }
  for (i = 5; i >= 2 ; i--) {
    digitalWrite(i, LOW);
    delay(500);
  }
}

CODE EXPLANATION 

Step-1: We define an integer variable i.

int i;

Step-2: In this block we use a for-loop because we want to do the same work again and again i.e. We want to set the mode of the pin number as output in an order from number 2 to 5. Loop will run 4 times as the loop runs in the first cycle it assigns pin 2 as an output then in the second cycle it assigns pin 3 as an output and same in third cycle pin 4 and in forth cycle pin 5.

void setup() {
  for (i = 2 ; i <= 5; i++) {
    pinMode(i, OUTPUT);
  }
}

Step-3:

In first for-loop we want to set the state of pin number 2, 3, 4 & 5 as HIGH after a delay of 0.5 second next to each other to turn ON led connected on pin number 2, 3, 4 & 5.

In the second for-loop we want to set the state of pin number 5, 4, 3, & 2 as LOW after a delay of 0.5 second next to each other to turn OFF led connected on pin number 5, 4, 3 & 2.

void loop() {
  for (i = 2; i <= 5 ; i++) {
    digitalWrite(i, HIGH);
    delay(500);
  }
  for (i = 5; i >= 2 ; i--) {
    digitalWrite(i, LOW);
    delay(500);
  }
}

And the void loop() gets executed continuously and follows the same pattern.

Now let’s try a new pattern


READ NEXT : BLINK LEDS IN AN ORDER USING ARDUINO


 

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x