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 go to our official website www.sunfounder.com to download related code by clicking LEARN -> Get Tutorials)
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
//Flowing LED Lights/* Eight LEDs will light up one by one from left to right, and then go out one by one from right to left. After that, the LEDs will light up one by one from right to left, and then go out one by one from left to right. This process will repeat indefinitely.*/ //Email:support@sunfounder.com //Website:www.sunfounder.com //2015.5.7 /**************************************/ const int lowestPin = 2;//the lowest one attach to pin 2 const int highestPin = 9;//the highest one attach to pin 9 /**************************************/ void setup() { //set pins 2 through 9 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 ms } //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 ms } for(int thisPin = highestPin;thisPin>=lowestPin;thisPin–) { digitalWrite(thisPin,HIGH);//turn this led on delay(100);//wait for 100 ms } for(int thisPin = lowestPin;thisPin <= highestPin;thisPin++) { digitalWrite(thisPin,LOW);//turn this led off delay(100);//wait for 100 ms } } |
Video