How to make Led sequencer use Arduino part 2
In this Arduino tutorial we will learn how to make a Led sequencer and how it works. Here we are used 8 Leds for sequentially blink. This is the second part of the Led sequencer tutorial.
You con watch the following video or read the written tutorial below.
The circuit diagram is very quick simple. The Leds are sequentially connected to pin 1, 2, 3, 4, 5, 6, 7 and 8. Here we are used 100 R resistor for limit the Led forward current.
Arduino Code:
int led1 =1 ; // Led1 is connected to pin 1
int led2 =2 ; // Led2 is connected to pin 2
int led3 =3 ; // Led3 is connected to pin 3
int led4 =4 ; // Led4 is connected to pin 4
int led5 =5; // Led5 is connected to pin 5
int led6 =6 ; // Led6 is connected to pin 6
int led7 =7 ; // Led7 is connected to pin 7
int led8 = 8; // Led8 is connected to pin 8
char i=0;
void setup() {
pinMode(led1,OUTPUT);
pinMode(led2,OUTPUT);
pinMode(led3,OUTPUT);
pinMode(led4,OUTPUT);
pinMode(led5,OUTPUT);
pinMode(led6,OUTPUT);
pinMode(led7,OUTPUT);
pinMode(led8,OUTPUT);
}
void loop() {
for(i=0;i<4;i++){
digitalWrite((4-i),HIGH); // Led is active
digitalWrite((5+i),HIGH); // Led is active
delay(100);
digitalWrite((4-i),LOW); // Led is active
digitalWrite((5+i),LOW); // Led is active
}
for(i=3;i>=0;i--){
digitalWrite((4-i),HIGH); // Led is active
digitalWrite((5+i),HIGH); // Led is active
delay(100);
digitalWrite((4-i),LOW); // Led is active
digitalWrite((5+i),LOW); // Led is active
}
}
Code Explain:
This is our example code. First we have to define Leds pin connections. In this case the pin number 1 of Arduino board is connected to Led1, pin 2 is connected to Led2 and so one.
We have defined a character variable which mane is i.
In the setup we have to define the Led pin mode. In this case pin 1 , 2, 3, 4, 5, 6, 7 and 8 of the Arduino board is an output pin.
In the loop section we have to create two for loop for Leds are sequentially Blinking. In the first for loop we have to set character variable i which value is 0 and we have to create a condition. Here we have wrote digitalWrite function for active two Leds at a time. We can easily understand which LED will be activated first by the simple calculation.
The next for loop works inverse as the first for loop.
Source file:
No comments