Guest User

Untitled

a guest
Jan 11th, 2016
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.84 KB | None | 0 0
  1. //Demo of using a boolean state to control fill method for LEDs.
  2. //This does not have a safeguard against accessing elements past the end of array in colors; be wary.
  3. //Jarrod Hiscock-Wagner 2016
  4.  
  5. #include "FastLED.h"
  6.  
  7. // required for creating an instance of FastLED
  8. #define NUMPIXELS 32
  9. #define LEDTYPE WS2812
  10. #define DATAPIN 5
  11. #define MAXBRIGHT 100
  12.  
  13. // 8 defined colors
  14. #define DEFINEDCOLORS 8
  15. //number of pixels per "block"
  16. #define NUMPIXELSPERBLOCK 4
  17.  
  18. // our CRGB array for FastLED to operate on
  19. CRGB leds[NUMPIXELS];
  20. // list of predefined colors
  21. CRGB colors[DEFINEDCOLORS] = { CRGB::Red, CRGB::OrangeRed, CRGB::Yellow, CRGB::Lime, CRGB::Green, CRGB::Aqua, CRGB::Blue, CRGB::Purple };
  22.  
  23. //change your desired palette here
  24. CRGBPalette16 myPalette = RainbowColors_p;
  25.  
  26. bool usingPalette = false;
  27. //change this line to adjust time spent in each mode
  28. unsigned long timePerMode = 10000;
  29. unsigned long prevUpdate = -99;
  30.  
  31.  
  32. void setup()
  33. {
  34.     //sanity delay
  35.     delay(2000);
  36.     //define our parameters for the template
  37.     FastLED.addLeds<LEDTYPE, DATAPIN>(leds, NUMPIXELS);
  38.     //set max brightness
  39.     FastLED.setBrightness(MAXBRIGHT);
  40. }
  41.  
  42. void loop()
  43. {
  44.     while (usingPalette)
  45.     {
  46.         //copied from Palette demo in samples
  47.         static uint8_t startIndex = 0;
  48.         startIndex = startIndex + 1; /* motion speed */
  49.  
  50.         FillLEDsFromPaletteColors(startIndex);
  51.         FastLED.show();
  52.  
  53.         //code for changing state of 'usingPalette'
  54.         unsigned long current = millis();
  55.         if (current - prevUpdate > timePerMode) //if our current time is more than timePerMode greater than our last update...
  56.         {
  57.             //set our boolean state to the opposite of what it is (either true or false)
  58.             usingPalette = !usingPalette;
  59.             //update our timer
  60.             prevUpdate = current;
  61.             //blank the display for the incoming stripes
  62.             fill_solid(leds, NUMPIXELS, CRGB::Black);
  63.         }
  64.     }
  65.  
  66.     while (!usingPalette)
  67.     {
  68.         /*
  69.          function for setting pixels rather than typing them out individually.
  70.          now, if you'd like to change a color, instead of writing out 64+ lines of code every time, you just
  71.          update the color in the CRGB array 'colors' up top and you're off running.
  72.  
  73.          I put it up top, and I'll say it again here... this does not safeguard against accessing elements past
  74.          the end of colors. There is a safeguard against overwriting the LED array in the 'setPixelBlock' function
  75.          but not here. Make sure that your NUMPIXELS/NUMPIXELSPERBLOCK is less than or equal to 'DEFINEDCOLORS' using
  76.          integer math.
  77.          */
  78.  
  79.         for (int i = 0; i < NUMPIXELS / NUMPIXELSPERBLOCK; i++)
  80.         {
  81.             setPixelBlock(i, colors[i]);
  82.             FastLED.show();
  83.             delay(100);
  84.             setPixelBlock(i, CRGB::Black);
  85.         }
  86.  
  87.         //this will end abruptly and will potentially cut off in the middle of an animation
  88.         unsigned long current = millis();
  89.         if(current - prevUpdate > timePerMode)
  90.         {
  91.             usingPalette = !usingPalette;
  92.             prevUpdate = current;
  93.         }
  94.  
  95.         //uncomment the following lines to have it count how many times you'd like to go through the block sequence
  96.         //change this next line for the number of times through you'd like it to go
  97.  
  98.         /*
  99.         uint8_t numRepitions = 10;
  100.         static uint8_t  count = 0;
  101.         count++;
  102.         if (count > numRepitions-1)
  103.         {
  104.             usingPalette = !usingPalette;
  105.             count=0;
  106.             //make sure we're in pallete for defined time
  107.             prevUpdate = millis();
  108.         }
  109.         */
  110.        
  111.     }
  112.  
  113. }
  114.  
  115. //standard FillLEDsFromPaletteColors
  116. void FillLEDsFromPaletteColors(uint8_t colorIndex)
  117. {
  118.     uint8_t brightness = 255;
  119.  
  120.     for (int i = 0; i < NUMPIXELS; i++) {
  121.         leds[i] = ColorFromPalette(myPalette, colorIndex, brightness);
  122.         colorIndex += 3;
  123.     }
  124. }
  125.  
  126. //functionized version of setting each pixel block individually with color.
  127. void setPixelBlock(int blocknum, CRGB color)
  128. {
  129.     for (int i = 0; i < NUMPIXELSPERBLOCK && (NUMPIXELSPERBLOCK*blocknum) + i<NUMPIXELS;    i++)
  130.     {
  131.         leds[(NUMPIXELSPERBLOCK*blocknum) + i]= color;
  132.     }
  133. }
Advertisement
Add Comment
Please, Sign In to add comment