LED Strip Color Sweep
Step 1 - Build the Project
Sweep colors across the flexible LED strip with a couple of incrementing statements.
Step 2 - Upload the Code
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.