LED Strip Changing Color Pixel Bounce

Step 1 - Build the Project

Changing Color Pixel Bounce 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 and changing color along the way!

Step 2 - Upload the Code

/* * Bouncing a pixel up and down the LED Strip, changing color along the way */ #include "LEDStrip.h" const byte numPixels = 15; //number of pixels on a strip int color = 200; //the initial color value (ranges from 0-300) /* * Make the LED strip object LEDStrip strip * = LEDStrip(numPixels, dataPin, clockPin); */ LEDStrip strip = LEDStrip(numPixels, 13, 12); int pixel = 0; //Position of the pixel on the strip int Direction = 1; //Direction of travel of the pixel void setup() { } void loop() { strip.setPixel(pixel,-1); //Clear the old pixel pixel = pixel + Direction; //Move the pixel strip.setPixel(pixel,color); //Set the new position and color strip.draw(); //Draw what you set up with .setPixel //If at either end of the strip if ((pixel == 0)||(pixel == numPixels)){ Direction = -1*Direction; //Direction will be 1 or -1 //Change color by 77, %300 to keep within 0-299 range color = (color + 77)%300; } delay(100); //controls the speed at which the pixel moves } //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. 

Next, you’ll set up three 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.

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.

Keep in mind that when the program runs, it runs from top to bottom. If you try to use a variable in the object-creation line, that variable has to be declared above the object creation line.

The setup() section has your button’s setup. It should be an INPUT_PULLUP pin mode so that it’s listening for signal, not generating signal. 

In the loop, erase pixel by writing color value -1 to it. Next, update the value of pixel by “Direction”. The pixel will either be moving up or down the strip, depending on if Direction is positive or negative. Now use setPixel to set the color of your latest pixel address and your latest color value. strip.draw() displays the result on the strip.

Then, check if the pixel value is at either end of the LED strip. If it is, reverse the direction by multiplying that variable by -1. 

Now update the color. This looks tricky, but it’s actually a useful tool called modulo. You know that the value of color can only reach 300. Imagine color is equal to 250. 250+77 = 327.  327/300 is 1 with a remainder of 27. So in this case, the color value now updates to 27. Next time through the loop, color will update to 104. 104/300 is 0 with a remainder of 104, so that color is not modified by the modulo.

The final delay controls the speed at which the color and pixel update.