LED Chaser With Arduino
Hello friends. What's up? Hope doing well. Today we are going to make a simple but very interesting LED Chaser with Arduino.
So let's started............
Required Components:
1. Arduino
2. LED * 4 Pcs { you can take more }
3. Push Button
4. 100 ohm Resistor [ 1/4 watt ] * 4 Pcs
5. Breadboard
6. Some jumper wires.
7. 10k ohm Resistor
Circuit Diagram:
Here is the circuit diagram.......
Code:
int buttonstate = 0;
int led1 = 3;
int led2 = 5;
int led3 = 6;
int led4 = 9;
int button = 8;
int brightness = 0;
void setup()
{
pinMode(button, INPUT);
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
pinMode(led4, OUTPUT);
Serial.begin(9600);
}
void mode1(){
digitalWrite(led1,HIGH);
digitalWrite(led2,HIGH);
digitalWrite(led3,HIGH);
digitalWrite(led4,HIGH);
delay(100);
digitalWrite(led1,LOW);
digitalWrite(led2,LOW);
digitalWrite(led3,LOW);
digitalWrite(led4,LOW);
delay(100);
}
void mode2(){
digitalWrite(led1,HIGH);
delay(100);
digitalWrite(led2,HIGH);
delay(100);
digitalWrite(led3,HIGH);
delay(100);
digitalWrite(led4,HIGH);
delay(100);
digitalWrite(led1,LOW);
delay(100);
digitalWrite(led2,LOW);
delay(100);
digitalWrite(led3,LOW);
delay(100);
digitalWrite(led4,LOW);
delay(100);
}
void mode3(){
digitalWrite(led4,HIGH);
delay(100);
digitalWrite(led3,HIGH);
delay(100);
digitalWrite(led2,HIGH);
delay(100);
digitalWrite(led1,HIGH);
delay(100);
digitalWrite(led4,LOW);
delay(100);
digitalWrite(led3,LOW);
delay(100);
digitalWrite(led2,LOW);
delay(100);
digitalWrite(led1,LOW);
delay(100);
}
void mode4(){
for (brightness = 0; brightness <= 255; brightness += 5) {
analogWrite(led1, brightness);
analogWrite(led2, brightness);
analogWrite(led3, brightness);
analogWrite(led4, brightness);
delay(30); // Wait for 30 millisecond(s)
}
for (brightness = 255; brightness >= 0; brightness -= 5) {
analogWrite(led1, brightness);
analogWrite(led2, brightness);
analogWrite(led3, brightness);
analogWrite(led4, brightness);
delay(30); // Wait for 30 millisecond(s)
}
digitalWrite(led4,HIGH);
delay(100);
digitalWrite(led3,HIGH);
delay(100);
digitalWrite(led2,HIGH);
delay(100);
digitalWrite(led1,HIGH);
delay(100);
digitalWrite(led4,LOW);
delay(100);
digitalWrite(led3,LOW);
delay(100);
digitalWrite(led2,LOW);
delay(100);
digitalWrite(led1,LOW);
delay(100);
}
void mode5(){
mode1();
mode2();
mode3();
mode4();
}
void loop()
{
int value = digitalRead(button);
if(value == 1){
delay(1000);
buttonstate = buttonstate + 1 ;
}
Serial.println(buttonstate);
if(buttonstate > 5){
buttonstate = 0;
}
if(buttonstate == 1){
mode1();
}
if(buttonstate == 2){
mode2();
}
if(buttonstate == 3){
mode3();
}
if(buttonstate == 4){
mode4();
}
if(buttonstate == 5){
mode5();
}
}
Hurray...Our chaser is ready.
This has 5 different function. Pressing the button you can change the chasing style.
Enjoy : )