Lesson 2 Flowing LED Lights

Share for us

Introduction

In this lesson, you will conduct a simple yet interesting experiment – using LEDs to create flowing LED lights. As the name implies, these flowing lights are made up of eight LEDs in a row which successively light up and dim one after another, just like flowing water.

Components

– 1 * SunFounder Uno board

– 1 * Breadboard

– Jumper wires

– 8 * LED

– 8 * Resistor (220Ω)

– 1 * USB cable

Principle

The principle of this experiment is simply to turn on eight LEDs in turn.

Experimental Procedures

Step 1: Build the circuit

The schematic diagram

Step 2: Program (Please refer to the example code in LEARN -> Get Tutorials on our website)

Step 3: Compile the code

Step 4: Upload the sketch to the SunFounder Uno board

Now, you should see eight LEDs brighten one by one from left to right, and then dim in turn from right to left. After that, the LEDs will light up from right to left and dim from left to right. This whole process will repeat until the circuit is power off.

Code

/*****************************************
* name: Flowing LED Lights
* function:you should see eight LEDs brighten one by one from left to right,
* and then dim in turn from right to left.
* After that, the LEDs will light up from right to left and dim from left to right.
* This whole process will repeat until the circuit is power off.
**************************************/
//Email:support@sunfounder.com
//Website:www.sunfounder.com/**************************************/
const int lowestPin = 2;//the lowest one attach to
const int highestPin = 9;//the highest one attach to
/**************************************/
void setup()
{
  //set pins 1 through 6 as output
  for(int thisPin = lowestPin;thisPin <= highestPin;thisPin++)
  {
    pinMode(thisPin,OUTPUT); //initialize thisPin as an output
  }
}
/****************************************/
void loop()
{
  //iterate over the pins
  //turn the led on from lowest to the highest
  for(int thisPin = lowestPin;thisPin <= highestPin;thisPin++)
  {
    digitalWrite(thisPin,HIGH);//turn this led on
    delay(100);//wait for 100 microseconds
  }
  //fade from the highest to the lowest
  for(int thisPin = highestPin;thisPin>=lowestPin;thisPin–)
  {
    digitalWrite(thisPin,LOW);//turn this led off
    delay(100);//wait for 100 microseconds
  }
  for(int thisPin = highestPin;thisPin>=lowestPin;thisPin–)
  {
    digitalWrite(thisPin,HIGH);//turn this led on
    delay(100);//wait for 100 microseconds
  }
  for(int thisPin = lowestPin;thisPin <= highestPin;thisPin++)
  {
    digitalWrite(thisPin,LOW);//turn this led off
    delay(100);//wait for 100 microseconds
  }
}

Video