PROJECT: Color Party
Step 1 - Build the Project

Use the random() function and the .setPixel method to have a party on the LED strip!

Step 2 - Upload the Code

/* * Display random ever changing colors on the LED strip, button * changes the mode between full strip and individual pixels */ //Include the LEDStrip library to use the commands #include "LEDStrip.h" const byte numPixels = 15; //number of pixels on a strip int color = 0; //color values on the strip range from -1 to 300. //0 is individual pixel mode, 1 is whole strip mode int stripMode = 0; //Which display mode the strip is in int pixel = 7; //Current pixel if in individual pixel color mode /* * Make the LED strip object LEDStrip strip * = LEDStrip(numPixels, dataPin (DI), clockPin(CI)); */ LEDStrip strip = LEDStrip(numPixels, 13, 12); void setup() { //Button pinMode(A5,INPUT_PULLUP); } void loop() { //If the button is pressed if (digitalRead(A5) == LOW){ delay(100); //Delay 10 ms to debounce //Wait until the button is released to register a press while( digitalRead(A5) == LOW){ delay(1); //Wait while button is held } stripMode = 1 - stripMode; //Change the mode } //If in single pixel mode if (stripMode == 0){ //Change the current pixel to a random location pixel = random(0,numPixels); //Change the color by 50x a random amount //Color values will be 0-250 and be divisible by fifty color = 50*random(0,6); //Write the new pixel to the new color strip.setPixel(pixel,color); } //In whole strip mode if (stripMode == 1){ //Change the color by 50x a random amount //Color values will be 0-250 and be divisible by fifty color = 50*random(0,6); //Write the strip to the new color strip.setPixel(strip.ALL,color); delay(100); //Visibility delay } strip.draw(); //Draw what was written to the strip } //Change the limits of the random function to change the color // range you draw // (c) 2017 Let's Start Coding. License: www.letsstartcoding.com/bsdlicense
 

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.

Next, create an integer variable called numPixels. Its value should be equal to the number of LEDs on your strip. 

The integer variable named color holds a value from -1 to 300. 

stripMode is a variable that changes between 0 and 1 when you press down the button. It’s a software switch. 

Your pixel variable holds the number of the active pixel that the code is modifying.

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.

In setup, you make a set a pin to be a button by making it an INPUT_PULLUP. Now when the button is pressed, it will send a LOW signal in to the Maker Board.

The first if statement checks whether the button is pressed. If it is, the code repeats the while loop until the button is released. As soon as the button is released, stripMode changes from 0 to 1 or from 1 to 0. 

Next, the if statements make a decision based on the newly changed value of stripMode. 

If the value is 0, the pixel variable is randomly chosen as a number between 0 and (numPixels-1). Then, the color value is chose as a random number between 0 and 5. That number is then multiplied by 50 so you can have the colors 0,50,100,150,200,250 on the pixel. Those two random values are then plugged in to the strip.setPixel method.

If the value of stripMode is 1, only the color is randomly chosen. Then the strip.setPixel method sets the entire strip to that color. A short delay ensures that you can see the different colors as they change.

The strip.draw() method then takes the .setPixel values and writes them to the strip. This method is outside of the if statements, so it doesn’t matter what the stripMode is, the .draw() method will always run.