BLINK LEDS IN AN ORDER USING ARDUINO

To blink LEDs in an Order using Arduino, it means, blink first led then blink second and so on. To do this we have to use code we already used for blinking a single LED, but here we use it inside for loop, so that one by one LEDs will blink.

Required components for Lighting up LED in an order using Arduino

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

CIRCUIT DIAGRAM : BLINK LEDS IN AN ORDER USING ARDUINO

Circuit diagram blink LEDs in an Order using Arduino
Circuit diagram Arduino and many Led’s

CONNECTION TABLE

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

ARDUINO CODE : TO BLINK LEDS IN AN ORDER USING ARDUINO

#define del 500
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(del);
    digitalWrite(i, LOW);
    delay(del);
  }
}

CODE EXPLANATION

Step-1:

#define is a useful C component that allows the programmer to give a name to a constant value before the program is compiled. Defined constants in Arduino don’t take up any program memory space on the chip. The compiler will replace references to these constants with the defined value at compile time.

Here we define a variable del (it can be any word del/mark/etc) to 500. So that where we use del it means 500. This will give us an advantage if you want to change the delay time, you do not need to change it everywhere in the program instead we can change it at first line, isn’t it cool?

Then we define an integer variable i.

#define del 500
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 two to five. 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:

  1. What we need to do is to nest the blinking code inside for-loop with a condition that is started with an LED connected at pin number 2 of the Arduino board to blink it.
  2. Then for-loop increases the value of i by 1 to blink led connected at pin number 3 of the Arduino board. 3. Then for-loop increases the value of i by 1 to blink led connected at pin number 4 of the Arduino board. 4. Then for-loop increases the value of i by 1 to blink led connected at pin number 5 of the Arduino board.
void loop() {
  for (i = 2; i <= 5 ; i++) {
    digitalWrite(i, HIGH);
    delay(del);
    digitalWrite(i, LOW);
    delay(del);
  }
}

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


READ NEXT – DIM AND BRIGHT LED GRADUALLY : LED INTENSITY VARIATION (PWM) USING ARDUINO

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x