Lesson 6 LED Flowing Lights

3562

Overview

In the first lesson, we have learned how to make an LED blink by programming the Arduino. Today, we will use the Arduino to control 8 LEDs to make the LEDs show the effect of flowing.


Components

- 1 * Arduino UNO

- 1 * USB Cable

- 8 * LED

- 8 * 220Ω Resistor

- 1 * Breadboard

- Several jumper wires


Principle

The principle of this experiment is very simple and is quite similar with that in the first lesson.

Key function:

●for statements

The for statement is used to repeat a block of statements enclosed in curly braces. An increment counter is usually used to increment and terminate the loop. The for statement is useful for any repetitive operation, and is often used in combination with arrays to operate on collections of data/pins.

There are three parts to the for loop header:

for (initialization; condition; increment) {

//statement(s);

}

a.png


The initialization happens first and exactly once. Each time through the loop, the condition is tested; if it's true, the statement block, and the increment is executed, then the condition is tested again. When the condition becomes false, the loop ends.


Procedures

Step 1: Build the circuit

b.png



Step 2: Program

_06_flowingLed.ino

/***********************************************************
File name: 06_flowingLed.ino
Description: LED turn lighting control
Website: www.adeept.com
E-mail: support@adeept.com
Author: Tom
Date: 2015/05/02 
***********************************************************/

void setup()
{ 
  unsigned char ledPin;           //ledPin will be set to 1,2,3,4,5,6, 7 and 8.
  for(ledPin=1;ledPin<=8;ledPin++)//In turn set 1 ~ 8 digital pins to output mode 
  pinMode(ledPin,OUTPUT);         //Set the  ledPin pin to output mode 
}

void loop()
{   
  unsigned char ledPin;           //ledPin will be set to 1,2,3,4,5,6, 7 and 8.
  for(ledPin=1;ledPin<=8;ledPin++)//Every 200ms on in order LED1 ~ 8 
  {
    digitalWrite(ledPin,HIGH);    //led on
    delay(200);                   //Delay 200 ms
  }
  for(ledPin=1;ledPin<=8;ledPin++)//Every 200ms off in order LED1 ~ 8 
  {
    digitalWrite(ledPin,LOW);     //led off
    delay(200);                   //Delay 200 ms
  } 
}



Step 3: Compile the program and upload to Arduino UNO board

Now, you can see 8 LEDs light up in sequence from the green one on the right side to others on the left, and next from the left to the right. The LEDs flash like flowing water repeatedly in a circular way.

c.jpg


Summary

Through this simple but fun experiment, you should have learned more skills in programming on Arduino. In addition, you can also modify the circuit and code provided to achieve even more dazzling effects.