LED Strip Color Sweep
 

Step 1 - Build the Project

Color Sweep Hookup: LED Strip with Red in 5V, Green in 13, Blue in 12, Black in ground.

Topics Covered:

 

Sweep colors across the flexible LED strip with a couple of incrementing statements.

Step 2 - Upload the Code

/* * Sweep a color and brightness-changing pixel back and forth * across the LED strip, without clearing previously drawn pixels */ #include "LEDStrip.h" const byte numPixels = 15; //number of pixels on a strip /* * Make the LED strip object LEDStrip strip * = LEDStrip(numPixels, dataPin, clockPin); */ LEDStrip strip = LEDStrip(numPixels, 13, 12); /* * A counter to watch for when to adjust the rates * of change of individual colors */ int color = 0; /* * An incrementor to hold the position of the 'head' * of the color bar */ int pixel = 2; int Direction = -1; //A variable to hold the direction of travel int brightness = 100; //A variable to hold the brightenss level void setup() { } void loop() { strip.setPixel(pixel, color, brightness); strip.draw(); delay(1); //Stability delay pixel = pixel + Direction; //Increment the head's position //Change direction at either end if ((pixel == numPixels-1)||(pixel == 0)){ Direction = -1*Direction;} color+=1; //Increment the color brightness+=1; //Increment brightness if (brightness >= 100){ //Loop the brightness brightness = 0; } if (color >= 300){ //Loop the color color = 0; } } //Change the rate at which color or brightness changes. // (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.

First, create an integer called numPixels. Its value should be equal to the number of LEDs on your strip. 

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

Next, you’ll set up four variables that will have updating values throughout the loop. 

Color refers to a value -1 to 300. 

Pixel is the ‘index’ of the pixel the code is modifying. Keep in mind the first pixel is pixel 0. 

Direction refers to whether the code is modifying a higher pixel address next or a lower pixel address next. If Direction is 1, then the code is moving up the strip toward the tip. If Direction is -1, the code is moving toward the base of the strip.

The final variable is brightness. It’s percentage brightness for each pixel. 

In the loop, you use the .setPixel method of the library to tell the LED Strip the which pixel, which color, and which brightness to set. That information doesn’t actually get passed to the strip until you use the strip.draw() method. 

This code is moving extremely quickly, so you can increase the delay here to see more clearly what is happening. 

 

Next, the pixel value is updated by either 1 or -1, depending on the value of Direction. 

The if statement is checking “Is the pixel at either end of the strip?” by comparing its value to 0 or to numPixels-1. If that is true, then Direction is multiplied by -1,  changing the direction.

After the if statement is passed (whether direction changed or not), the color value is increased by 1. Then the brightness value is increased by 1. 

The second if statement checks the brightness variable. If it has reached 100% brightness, the variable is reset to 0.

The third if statement checks the color variable. If it has reached or exceeded 300, the variable is reset to 0.