LED Strip Pixel Bounce
 

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:

 

Send a single pixel up and down the strip, changing direction at each end. 

Step 2 - Upload the Code

/* * A pixel bouncing back and forth on the strip */ //Include the LEDStrip library to use the commands #include "LEDStrip.h" const byte numPixels = 15; //number of pixels on a strip /* * Make the LED strip object LEDStrip strip * = LEDStrip(numPixels, dataPin (DI), clockPin(CI)); */ LEDStrip strip = LEDStrip(numPixels, 13, 12); int pixel = 0; //Position of the pixel on the strip //Direction will be 1 or -1 int Direction = 1; //Direction of travel of the pixel void setup() { } void loop() { strip.setPixel(pixel,-1); //Clear the old pixel (-1 is off) pixel = pixel + Direction; //Move the pixel //Color range is 0 - 300, 200 is green strip.setPixel(pixel,200); //Write the pixel strip.draw(); //Draw what we wrote to the strip //If at either end of the strip if ((pixel == 0)||(pixel == numPixels-1)){ Direction = -1*Direction; //Change direction } delay(100); //Timing delay controls pixel speed } //Change the if statement to make the pixel bounce between different // limits // (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.

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

The Direction variable 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.

In loop, use the .setPixel method (included in the LEDStrip library) to set the active pixel (using pixel variable) and the color value to 200. This information isn’t actually shown on the strip until you use the strip.draw() method. 

Next, you’re immediately setting the pixel value to clear. Because you don’t use strip.draw, the pixel does not turn off. 

Now pixel is updated by one move in “Direction”. If Direction holds a value of 1, the pixel is now 1 greater than it was. If Direction holds a value of -1, the pixel is now 1 less than it was.

The if statement checks whether the pixel value is equal to 0 OR equal to the last pixel on your strip. If either of those is true, Direction is multiplied by -1. 

The delay affects how quickly the pixel bounces. Think about the second time the loop runs. When the loop reaches the strip.draw() command, it will do two things: draw the previous pixel as clear and draw the current pixel as 200. The strip.draw() method can execute many strip.SetPixel settings at once.