PROJECT: Brightness Wave
Step 1 - Build the Project
Use an array to change the brightness of the pixels on the LED strip in a wave formation.
Step 2 - Upload the Code
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 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.