LED Strip 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

/* * Bouncing a pixel along the strip when the button is pressed */ #include "LEDStrip.h" const byte numPixels = 15; //number of pixels on a strip /* * 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() { pinMode(A5,INPUT_PULLUP); } void loop() { //If the button is pressed if (digitalRead(A5) == LOW){ strip.setPixel(pixel,-1); //Erase the previous pixel pixel = pixel + Direction; //Move the pixel } strip.setPixel(pixel,200); //Write the pixel strip.draw(); //Draw what we wrote to the strip //If at either end of the strip, reverse Direction if (pixel <= 0){ Direction = 1; } if (pixel >= numPixels-1){ Direction = -1; } 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.

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.