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
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.