Lesson 3 Interactive LED Flowing Lights

Share for us

Introduction

You  have learnt how to make flowing LED lights in Super Kit. In this lesson, a potentiometer is added on that basis to change the interval of LED flashing by adjusting the potentiometer.

Components

– 1* SunFounder Uno board

– 1 * Breadboard

– 8 * LED

– 8 * Resistor (220Ω)

– 1 * Potentiometer

– 1 * USB cable

– Several jumper wires

Experimental Principle

The principle is quite simple, that is, to turn eight LEDs on in turn. And then change the time interval of LED on and off by adjusting the potentiometer.

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

Here you should see eight LEDs light up one by one in turn. Adjust the potentiometer, and you will find the time interval of LED lighting up changes. 

Code

/********************************
* name:Interactive LED Flowing Lights
* function:Here you should see eight LEDs light up one by one in turn. Adjust the potentiometer, and you will find the time interval of LED lighting up changes.
********************************/
//Email:support@sunfounder.com
//Website:www.sunfounder.comint ledNum = 8; //the number of the led you attach
byte ledPin[] = { 2, 3, 4, 5, 6, 7, 8, 9}; // Create array for LED pins
int ledDelay; // delay between changes
int direction = 1;
int currentLED = 0;
unsigned long changeTime;
int potPin = 0; // select the input pin for the potentiometervoid setup()
{
for (int x = 0; x < ledNum; x++)
// set all pins to output
{
pinMode(ledPin[x], OUTPUT);
}
changeTime = millis();//
}void loop() {
ledDelay = analogRead(potPin); // read the value from the pot
if ((millis() – changeTime) > ledDelay)
{ // if it has been ledDelay ms since last change
changeLED();
changeTime = millis();
}
}void changeLED()
{
for (int x=0; x < ledNum; x++)
{ // turn off all LED’s
digitalWrite(ledPin[x], LOW);
}
digitalWrite(ledPin[currentLED], HIGH); // turn on the current LED
currentLED += direction; // increment by the direction value
// change direction if we reach the end
if (currentLED == ledNum-1)
{
direction = -1;
}
if (currentLED == 0)
{
direction = 1;
}

Video