LED Strip Brightness & Color Tuner
 

Step 1 - Build the Project

Use button A5 to update the color value of the LED strip. Use button 2 to update the brightness of the LED strip.

Step 2 - Upload the Code

/* * LED color and brightness tuner- color changes while one button is * held, and brightness while the other is held. */ //Include the LEDStrip library to use the commands #include "LEDStrip.h" const byte numPixels = 15; //number of pixels on a strip /* * Make the LED strip object LEDStrip strip * = LEDStrip(numPixels, dataPin (DI), clockPin(CI)); */ LEDStrip strip = LEDStrip(numPixels, 13, 12); int color = 0; //Variable to hold the color of the strip int brightness = 50; //Current brightness level void setup() { //Buttons pinMode(A5,INPUT_PULLUP); pinMode(2,INPUT_PULLUP); } void loop() { if (digitalRead(A5) == LOW){ color = (color + 1)%300; //Increase color, looping 0 - 299 delay(20); } if (digitalRead(2) == LOW){ brightness = (brightness + 1)%100; //Increase color, looping 0 - 99 delay(20); } strip.setPixel(strip.ALL,color,brightness); //Set all pixels to current color strip.draw(); //Draw the strip } //Change the delays in each 'if' statement to see the effect on the strip // (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 variable called numPixels. Its value should be equal to the number of LEDs on your strip. 

To tell the LED Strip library (already included in your program) 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 variable called color refers to a value -1 to 300.

Your other variable is brightness. It’s the percentage brightness for the strip.

In the setup() of the code, set both the color tuning button and the brightness tuning button to INPUT_PULLUP pin modes. When you are pressing the button down, it will send a LOW signal to that pin. 

In the loop, use the first if statement so check if the A5 button is pressed. If it is, update the color value. This looks tricky, but it’s actually a useful tool called modulo. You know that the value of color can only reach 300. Imagine color is equal to 300. 300+1 = 301.  301/300 is 1 with a remainder of 1. So in this case, the color value now updates to 1. Next time through the loop, color will update to 2. 2/300 is 0 with a remainder of 2, so that color is not modified by the modulo.

 

Next, check the status of the brightness button. If it is pressed, update brightness using the same modulo method you used for color.

This loop is repeating very quickly. Each time through the loop, the color value can only update by 1. But the amount of time it takes to complete the color cycle is under 10 seconds.