PROJECT: Brightness Wave
Step 1 - Build the Project

Brightness Wave Hookup: LED Strip with Red in 5V, Green in 13, Blue in 12, Black in ground.

Topics Covered:


Use an array to change the brightness of the pixels on the LED strip in a wave formation.

Step 2 - Upload the Code

/* * Make the LED strip fade up and down along its length */ //Include the LEDStrip library to use the commands #include "LEDStrip.h" const byte numPixels = 15; /* * Make the LED strip object LEDStrip strip * = LEDStrip(numPixels, dataPin (DI), clockPin (CI)); */ LEDStrip strip = LEDStrip(numPixels, 13, 12); //A list of the brightnesses, values picked for aesthetics int brightnesses[] = {2,9,16,23,31,37,44,51,44,37,31,23,16,9,2}; int pixel = 7; //Initial pixel value is 7 void setup() { } void loop() { //Loop for all 15 pixels for (int i = 0;i<15;i++){ //Set the ith pixel past the head to 200 (green) at the // brightness from the array above. strip.setPixel((pixel + i)%numPixels,200,brightnesses[i]); //(pixel + i)%15 wraps pixel+i around the strip in a loop } strip.draw(); //Draw what was written to the strip pixel++; //Increase pixel delay(200); //A delay for visibility } //Put the brightness[i] element in set pixel into the color spot // (replace 200) and set the brightness instead to 100. // (c) 2017 Let's Start Coding. License:

Step 3 - Read the Walkthrough

First include the LEDStrip library. This library gives you access to a new set of commands (called methods) that make using the LED Strip easier. Without including the library, the Arduino software won’t recognize the new methods.

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 15 pixels, the data pin is number 13, and the clock pin is number 12 on your carrier board.

Next, create an array called brightness and fill it with 15 values. An array is like a list of variables you can pick from by calling their number in line. The first value in this array is brightness[0] and it equals 2. 

Create an integer variable called pixel. This holds the number of the pixel that is currently being updated.

In the loop, you’ll use a for loop to continually rotate through each pixel on the strip. In this for loop, you create a variable called i and give it a value of 0. As long as i is less than 15, it will increment up (++) each time it runs the code in the loop.

This for loop is using the .setPixel method from the LEDStrip library. This method only needs three inputs: pixel, color, and brightness. But here, you are using math and arrays as the inputs to the method. It helps to break it into its three pieces.

(pixel+i)%15 is the pixel value. First, the value of pixel is added to the value of i. Then that sum is ‘modded’ by 15. That means the sum is divided by 15, but the result is the remainder of the division, not the quotient. An example: You know that the value of pixel shouldn’t exceed 15. Imagine pixel is equal to 7 and i is 9. 7+9=16.  16/15 is 1 with a remainder of 1. So in this case, the color value now updates to 1. Next time through the loop, i will update to 10. 17/15 is 0 with a remainder of 2, so the pixel that is updated is pixel 2. 

200 is a color value. The LEDStrip library defines the color values, so there is no magic here. 200 is green.

brightness[i] looks for the value that is the space “i” in the array brightness. It pulls down that value and plugs it in to the brightness input. 

The for loop runs 15 times before the strip draws any pixel values. So each time you see a pixel’s brightness change, the for loop has run 15 times. The strip.draw() command shows all of those changes at once.

Finally, pixel is incremented (++) by one before the next run of the loop. Because it is always modded by 15, the value of pixel never needs to be reset to 0.