PROJECT: Pixel Bounce with Button
Step 1 - Build the Project
Holding down a button bounces a pixel up and down the flexible LED strip with an if statement.
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.
In setup, the button pin is an INPUT_PULLUP so that Maker Board is listening for a signal from pin A5.
In loop, check if the button is pressed (LOW). If it is, then erase the pixel’s color with a color value of -1 and move the pixel location in the direction specified by the Direction variable.
If button is not pressed, the pixel value doesn’t change, so pixel is just redrawn with the .setPixel() and .draw() methods.
The final if statements check to see if pixel has reached either the base or the tip of the strip. If the pixel is at position 0 (or less), Direction is set to 1. If pixel is at position numPixel-1 (or greater), Direction is set to -1.