LED Strip Fade

Step 1 - Build the Project

Fade the entire LED strip's brightness up and back down using the .setPixel method().

Step 2 - Upload the Code

/* * Fades the brightness of the entire LED strip up and down */ //Include the LEDStrip library to use the commands #include "LEDStrip.h" /* * Make the LED strip object LEDStrip strip * = LEDStrip(numPixels, dataPin (DI), clockPin (CI)); */ LEDStrip strip = LEDStrip(15, 13, 12); int brightness = 2; //The level of illumination int brightnessChange = 1; //The rate of change in brightness void setup() { } void loop() { //Set the brightness of all pixels, color of 200 (green) strip.setPixel(strip.ALL,200,brightness); //Change direction at either limit (0% or 100%) if ((brightness <= 0)|(brightness >= 100)){ brightnessChange = (-1)*brightnessChange; } //Change the brightness level brightness = brightness + brightnessChange; strip.draw(); //Draw the pixel delay(10); //Stability delay } //Change brightnessChange or the delay to make it fade faster or // slower // (c) 2017 Let's Start Coding. License: www.letsstartcoding.com/bsdlicense

Step 3 - Read the Walkthrough

The LED Strip library is a separate file of code. It contains functions and methods that are specific to controlling a strip of LEDs. To use the library, you use the #include statement. Then, you have to give the library some information, similar to setting pinMode() for components.

To tell the LEDStrip library the name & attributes of your LED Strip, you create an object. In this case, the object is called ‘strip’ and it has 15 pixels, the data pin is number 13, and the clock pin is number 12 on your carrier board.

Next, you’ll use two variables for the values that will be changing throughout your program. 

The first variable is an integer for the brightness value. It is basically a percentage of power being sent to the strip.

The brightnessChange value is the size of the steps between brightness levels that your code will take. You can modify this to anything below 100.

In the loop, the brightness variable is constantly updating for the entire strip. The .setPixel method takes in which pixel to address—in this case, all of them — what color to write those pixels , and what the brightness should be.

Next, check if the brightness level has passed or reached 0 or 100. If either of those things is true, multiple brightnessChange by -1 so that if the light was dimming, it will start brightening and vice versa.

After the if statement, the brightness level is updated by brightnessChange. Depending on whether brightnessChange is positive or negative, the brightness value will grow or shrink. 

Now the strip.draw() command pushes the pixel, color, and brightness value to the strip. This is the first time in the loop that you should actually see the effects of your .setPixel values. 

The delay affects how fast the brightness values update. Change this to see how the delay affects the program differently than how the brightnessChange variable affects the program.