led chaser

LED Chaser With 4 Different Methods

Posted on

Chaser LED Using Arduino – LED chasers are very widely used to beautify the environment. Various microcontrollers can be used such as PIC, AVR, ARM and others. If you like working with an AVR microcontroller, Arduino is the choice. In making this LED chaser the most important thing is to make a simple program, this allows with several lines, can make a lot of LED Chasers.

In this project case is how to make the 5 LEDs live alternately, chaser or running LED. At first the LED 1 is on, besides it off. Secondly, the second LED is on, otherwise it off. Third, the third LED is on, besides it of and so on. With a delay, the turn on will take turns.

In this project I will make a loop to turn on LED with 4 methods.

1. The first is the manual method, meaning that we write the code entirely manually and it will recur by default by Arduino, without using the while, do-while and for functions.

2. While Method

3. Do-While methods

4. For Method

Please connect 5 LEDs to arduino as shown in the following picture:

menghidupkan led dengan arduino

 

1. Manual Method

Progam:

int led_1 = 8;
int led_2 = 9;
int led_3 = 10;
int led_4 = 11;
int led_5 = 12;
int times = 200;

void setup() {  
  for (int a = 7; a<13; a++){
    pinMode(a, OUTPUT);
  }
}

void loop() {
  //turn_on LED 1
  digitalWrite(led_1, HIGH);
  digitalWrite(led_2, LOW);
  digitalWrite(led_3, LOW);
  digitalWrite(led_4, LOW);
  digitalWrite(led_5, LOW);
  delay (times);

  //turn_on LED 2
  digitalWrite(led_1, LOW);
  digitalWrite(led_2, HIGH);
  digitalWrite(led_3, LOW);
  digitalWrite(led_4, LOW);
  digitalWrite(led_5, LOW);
  delay (times);

  //turn_on LED 3
  digitalWrite(led_1, LOW);
  digitalWrite(led_2, LOW);
  digitalWrite(led_3, HIGH);
  digitalWrite(led_4, LOW);
  digitalWrite(led_5, LOW);
  delay (times);

  //turn_on LED 4
  digitalWrite(led_1, LOW);
  digitalWrite(led_2, LOW);
  digitalWrite(led_3, LOW);
  digitalWrite(led_4, HIGH);
  digitalWrite(led_5, LOW);
  delay (times);

  //turn_on LED 5
  digitalWrite(led_1, LOW);
  digitalWrite(led_2, LOW);
  digitalWrite(led_3, LOW);
  digitalWrite(led_4, LOW);
  digitalWrite(led_5, HIGH);
  delay (times);
}

2. “While” Method

Program:

int led = 8;
int ledOld;
int times = 200;

void setup() {
  for (int a = 7; a < 13; a++) {
    pinMode(a, OUTPUT);
  }
}

void loop() {
  while (1) {
    digitalWrite(led, HIGH);
    ledOld = led;
    led++;
    delay(times);
    digitalWrite(ledOld, LOW);
    if (led == 13){
      led = 8;
    }
  }
}

3. “Do-While” Method

Program:

int led = 8;
int ledOld;
int times = 200;

void setup() {
  for (int a = 7; a < 13; a++) {
    pinMode(a, OUTPUT);
  }
}

void loop() {
  do {
    digitalWrite(led, HIGH);
    ledOld = led;
    led++;
    delay(times);
    digitalWrite(ledOld, LOW);
    if (led == 13){
      led = 8;
    }
  }
  while(1);
}

4. “For” Method

Program:

int led;
int ledOld;
int times = 200;

void setup() {
  for (int a = 7; a < 13; a++) {
    pinMode(a, OUTPUT);
  }
}

void loop() {
  for (led = 8; led < 13; led++) {
    digitalWrite(led, HIGH);
    ledOld = led;
    delay(times);
    digitalWrite(ledOld, LOW);
    if (led == 13) {
      led = 8;
    }
  }
}

In general functions, there are not many different programs, but the 4 programs above give you to be able to choose which method is simpler and easier to use.  If you need more command reference used on Arduino, please visit Arduino reference here. Hopefully the above article is useful.

Leave a Reply

Your email address will not be published. Required fields are marked *