PROJECT: Pixel Bounce
Step 1 - Build the Project
Send a single pixel up and down the strip, changing direction at each end.
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.
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.