Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Demo of using a boolean state to control fill method for LEDs.
- //This does not have a safeguard against accessing elements past the end of array in colors; be wary.
- //Jarrod Hiscock-Wagner 2016
- #include "FastLED.h"
- // required for creating an instance of FastLED
- #define NUMPIXELS 32
- #define LEDTYPE WS2812
- #define DATAPIN 5
- #define MAXBRIGHT 100
- // 8 defined colors
- #define DEFINEDCOLORS 8
- //number of pixels per "block"
- #define NUMPIXELSPERBLOCK 4
- // our CRGB array for FastLED to operate on
- CRGB leds[NUMPIXELS];
- // list of predefined colors
- CRGB colors[DEFINEDCOLORS] = { CRGB::Red, CRGB::OrangeRed, CRGB::Yellow, CRGB::Lime, CRGB::Green, CRGB::Aqua, CRGB::Blue, CRGB::Purple };
- //change your desired palette here
- CRGBPalette16 myPalette = RainbowColors_p;
- bool usingPalette = false;
- //change this line to adjust time spent in each mode
- unsigned long timePerMode = 10000;
- unsigned long prevUpdate = -99;
- void setup()
- {
- //sanity delay
- delay(2000);
- //define our parameters for the template
- FastLED.addLeds<LEDTYPE, DATAPIN>(leds, NUMPIXELS);
- //set max brightness
- FastLED.setBrightness(MAXBRIGHT);
- }
- void loop()
- {
- while (usingPalette)
- {
- //copied from Palette demo in samples
- static uint8_t startIndex = 0;
- startIndex = startIndex + 1; /* motion speed */
- FillLEDsFromPaletteColors(startIndex);
- FastLED.show();
- //code for changing state of 'usingPalette'
- unsigned long current = millis();
- if (current - prevUpdate > timePerMode) //if our current time is more than timePerMode greater than our last update...
- {
- //set our boolean state to the opposite of what it is (either true or false)
- usingPalette = !usingPalette;
- //update our timer
- prevUpdate = current;
- //blank the display for the incoming stripes
- fill_solid(leds, NUMPIXELS, CRGB::Black);
- }
- }
- while (!usingPalette)
- {
- /*
- function for setting pixels rather than typing them out individually.
- now, if you'd like to change a color, instead of writing out 64+ lines of code every time, you just
- update the color in the CRGB array 'colors' up top and you're off running.
- I put it up top, and I'll say it again here... this does not safeguard against accessing elements past
- the end of colors. There is a safeguard against overwriting the LED array in the 'setPixelBlock' function
- but not here. Make sure that your NUMPIXELS/NUMPIXELSPERBLOCK is less than or equal to 'DEFINEDCOLORS' using
- integer math.
- */
- for (int i = 0; i < NUMPIXELS / NUMPIXELSPERBLOCK; i++)
- {
- setPixelBlock(i, colors[i]);
- FastLED.show();
- delay(100);
- setPixelBlock(i, CRGB::Black);
- }
- //this will end abruptly and will potentially cut off in the middle of an animation
- unsigned long current = millis();
- if(current - prevUpdate > timePerMode)
- {
- usingPalette = !usingPalette;
- prevUpdate = current;
- }
- //uncomment the following lines to have it count how many times you'd like to go through the block sequence
- //change this next line for the number of times through you'd like it to go
- /*
- uint8_t numRepitions = 10;
- static uint8_t count = 0;
- count++;
- if (count > numRepitions-1)
- {
- usingPalette = !usingPalette;
- count=0;
- //make sure we're in pallete for defined time
- prevUpdate = millis();
- }
- */
- }
- }
- //standard FillLEDsFromPaletteColors
- void FillLEDsFromPaletteColors(uint8_t colorIndex)
- {
- uint8_t brightness = 255;
- for (int i = 0; i < NUMPIXELS; i++) {
- leds[i] = ColorFromPalette(myPalette, colorIndex, brightness);
- colorIndex += 3;
- }
- }
- //functionized version of setting each pixel block individually with color.
- void setPixelBlock(int blocknum, CRGB color)
- {
- for (int i = 0; i < NUMPIXELSPERBLOCK && (NUMPIXELSPERBLOCK*blocknum) + i<NUMPIXELS; i++)
- {
- leds[(NUMPIXELSPERBLOCK*blocknum) + i]= color;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment