Advertisement
marmil

holiday_server_rack

Dec 11th, 2015
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.10 KB | None | 0 0
  1. //***************************************************************
  2. // Holiday Server Rack (blinking), for Michael Sime
  3. // By Marc Miller
  4. // Based on a heavily modified version of Mark's Holiday lights
  5. // Dec 2015
  6. //***************************************************************
  7.  
  8. #include "FastLED.h"
  9. #define LED_PIN     6
  10. #define LED_TYPE    NEOPIXEL
  11. #define COLOR_ORDER RGB
  12. #define NUM_LEDS    12
  13. CRGB leds[NUM_LEDS];
  14.  
  15. #define MASTER_BRIGHTNESS   50
  16.  
  17. #define STARTING_BRIGHTNESS 120
  18. #define FADE_IN_SPEED       220  // 0-255  smaller number is slower
  19. #define FADE_OUT_SPEED      160  // 0-255  smaller number is slower
  20. #define DENSITY             150  // 0-255  smaller is less dense
  21. #define ACTIVITY            6    // Use a slghtly larger number for a more "activity"
  22. #define TWINKLE_CHANCE      85   // 0-100  How often to add a twinkle
  23. #define REPEATE_CHANCE      60   // 0-100  Percent chance of changing the same pixel
  24.  
  25. void setup() {
  26.   //delay(3000);
  27.   FastLED.addLeds<NEOPIXEL,LED_PIN>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
  28.   FastLED.setBrightness(MASTER_BRIGHTNESS);
  29. }
  30.  
  31. //---------------------------------------------------------------
  32. void loop()
  33. {
  34.   chooseColorPalette();
  35.   colortwinkles();
  36.   FastLED.show();  
  37.   FastLED.delay(20);  // Slow the whole display down a bit.
  38. }
  39.  
  40.  
  41. //---------------------------------------------------------------
  42. CRGBPalette16 gPalette;
  43.  
  44. void chooseColorPalette()
  45. {
  46.   uint8_t numberOfPalettes = 13;
  47.   uint8_t secondsPerPalette = random(2,5);
  48.   uint8_t whichPalette = (millis()/(1000*secondsPerPalette)) % numberOfPalettes;
  49.   //whichPalette = 3;  // To temporarily locked a palette choice to work on a color
  50.  
  51.   // Palette colors
  52.   CRGB r(CRGB::Red), b(CRGB::Blue), w(85,85,85), g(CRGB::Green), W(CRGB::White), l(0xE1A024);
  53.  
  54.   // The general basic color (and a similar basic color) that display most of the time
  55.   CRGB z(CHSV(50,255,210)), zg(CHSV(70,245,55));
  56.  
  57.   // Other custom colors
  58.   CRGB dr(CHSV(0,220,35)), rw(CHSV(130,80,100)), dg(CRGB::DarkGreen), yg(CHSV(55,255,40)), bk(CRGB::Black);
  59.  
  60.   switch( whichPalette) {  
  61.     case 0:
  62.       gPalette = CRGBPalette16(  z,z,z,z, z,z,z,z, z,z,z,z, zg,zg,zg,zg ); // basic
  63.       break;
  64.     case 1:
  65.       gPalette = CRGBPalette16(  b,z,z,z, z,z,z,z, z,z,z,z, z,z,z,z );  // basic + BLUE
  66.       break;
  67.     case 2:
  68.       gPalette = CRGBPalette16( dr,z,z,z, z,z,z,z, z,z,z,z, z,z,z,z );  // basic + Dark RED
  69.       break;
  70.     case 3:
  71.       gPalette = CRGBPalette16( rw,z,z,z, z,z,z,z, zg,z,z,z, zg,z,z,z );  // basic + blue-WHITE
  72.       break;
  73.     case 4:
  74.       gPalette = CRGBPalette16( dg,z,z,z, z,z,z,z, z,z,z,z, z,z,z,z );  // basic + Dark GREEN
  75.       break;
  76.     case 5:
  77.       gPalette = CRGBPalette16(  yg,yg,yg,yg, z,z,z,z, z,z,z,z, z,z,z,z );  // basic + YELLOWGREEN
  78.       break;
  79.     case 6:
  80.       gPalette = CRGBPalette16(  b,z,z,z, dr,z,z,z, z,z,z,z, zg,zg,zg,zg );  // basic + BLUE + Dark RED
  81.       break;
  82.     case 7:
  83.       gPalette = CRGBPalette16(  b,z,z,z, yg,z,z,z, z,z,z,z, zg,zg,zg,zg );  // basic + BLUE + YELLOWGREEN
  84.       break;
  85.     case 8:
  86.       gPalette = CRGBPalette16(  b,z,z,z, dg,z,z,z, z,z,z,z, zg,zg,zg,zg );  // basic + BLUE + Dark GREEN
  87.       break;
  88.     case 9:
  89.       gPalette = CRGBPalette16(  b,z,z,z, b,z,z,z, z,z,z,z, zg,zg,zg,zg ); // basic + BLUE + BLUE
  90.       break;
  91.     case 10:
  92.       gPalette = CRGBPalette16(  bk,z,z,z, bk,bk,z,z, bk,z,z,z, zg,zg,zg,zg ); // basic + Blacked Out
  93.       break;
  94.  
  95.     // Below are just repeats of the basic colors to make it slightly more likely to show up.
  96.     case 11:
  97.       gPalette = CRGBPalette16(  z,z,z,z, z,z,z,z, z,z,z,z, zg,zg,zg,zg ); // basic
  98.       break;
  99.     case 12:
  100.       gPalette = CRGBPalette16(  z,z,z,z, z,z,z,z, z,z,z,z, zg,zg,zg,zg ); // basic
  101.       break;
  102.   }
  103. }
  104.  
  105. enum { GETTING_DARKER = 0, GETTING_BRIGHTER = 1 };
  106.  
  107.  
  108. //---------------------------------------------------------------
  109. void colortwinkles()
  110. {
  111.   // Make each pixel brighter or darker, depending on its 'direction' flag.
  112.   brightenOrDarkenEachPixel( FADE_IN_SPEED, FADE_OUT_SPEED);
  113.  
  114.   // Now consider adding a new random twinkle
  115.   if (random8(100) < TWINKLE_CHANCE) {
  116.     if (random8() < DENSITY) {
  117.       int pos = random16(NUM_LEDS);
  118.       if (random8(100) < REPEATE_CHANCE) {
  119.         if( !leds[pos]) {
  120.           leds[pos] = ColorFromPalette( gPalette, random8(), STARTING_BRIGHTNESS, NOBLEND);
  121.           setPixelDirection(pos, GETTING_BRIGHTER);
  122.         }
  123.       } else {
  124.         leds[pos] = ColorFromPalette( gPalette, random8(), STARTING_BRIGHTNESS, NOBLEND);
  125.         setPixelDirection(pos, GETTING_BRIGHTER);
  126.       }
  127.     }
  128.   }
  129. }
  130.  
  131. void brightenOrDarkenEachPixel( fract8 fadeUpAmount, fract8 fadeDownAmount)
  132. {
  133.   for( uint16_t i = 0; i < NUM_LEDS; i++) {
  134.    
  135.     if( getPixelDirection(i) == GETTING_DARKER) {
  136.       int pick = 0;
  137.       int minCutoff = 10;
  138.       // This pixel is getting darker
  139.       if (random8() < ACTIVITY) {
  140.         pick = random8(NUM_LEDS);
  141.         leds[pick] = makeDarker( leds[pick], fadeDownAmount);
  142.       }
  143.       if( leds[pick].r == STARTING_BRIGHTNESS || leds[pick].g == STARTING_BRIGHTNESS || leds[pick].b == STARTING_BRIGHTNESS ) {
  144.         setPixelDirection(pick, GETTING_BRIGHTER);
  145.       }
  146.       if( leds[pick].r < minCutoff || leds[pick].g < minCutoff || leds[pick].b < minCutoff ) {  // turn dim pixels completely off/black.
  147.         leds[pick] = CRGB::Black;
  148.         setPixelDirection(pick, GETTING_BRIGHTER);
  149.       }
  150.  
  151.     } else {
  152.       // This pixel is getting brighter
  153.       leds[i] = makeBrighter( leds[i], fadeUpAmount);
  154.       // now check to see if we've maxxed out the brightness
  155.       if( leds[i].r == 255 || leds[i].g == 255 || leds[i].b == 255) {
  156.         // if so, turn around and start getting darker
  157.         setPixelDirection(i, GETTING_DARKER);
  158.       }
  159.     }
  160.    
  161.   }
  162. }
  163.  
  164.  
  165. //---------------------------------------------------------------
  166. CRGB makeBrighter( const CRGB& color, fract8 howMuchBrighter)
  167. {
  168.   CRGB incrementalColor = color;
  169.   incrementalColor.nscale8( howMuchBrighter);
  170.   return color + incrementalColor;
  171. }
  172.  
  173. CRGB makeDarker( const CRGB& color, fract8 howMuchDarker)
  174. {
  175.   CRGB newcolor = color;
  176.   newcolor.nscale8( 255 - howMuchDarker);
  177.   return newcolor;
  178. }
  179.  
  180.  
  181. // For illustration purposes, there are two separate implementations
  182. // provided here for the array of 'directionFlags':
  183. // - a simple one, which uses one byte (8 bits) of RAM for each pixel, and
  184. // - a compact one, which uses just one BIT of RAM for each pixel.
  185.  
  186. // Set this to 1 or 8 to select which implementation
  187. // of directionFlags is used.  1=more compact, 8=simpler.
  188. #define BITS_PER_DIRECTION_FLAG 1
  189.  
  190.  
  191. #if BITS_PER_DIRECTION_FLAG == 8
  192. // Simple implementation of the directionFlags array,
  193. // which takes up one byte (eight bits) per pixel.
  194. uint8_t directionFlags[NUM_LEDS];
  195.  
  196. bool getPixelDirection( uint16_t i) {
  197.   return directionFlags[i];
  198. }
  199.  
  200. void setPixelDirection( uint16_t i, bool dir) {
  201.   directionFlags[i] = dir;
  202. }
  203. #endif
  204.  
  205.  
  206. #if BITS_PER_DIRECTION_FLAG == 1
  207. // Compact (but more complicated) implementation of
  208. // the directionFlags array, using just one BIT of RAM
  209. // per pixel.  This requires a bunch of bit wrangling,
  210. // but conserves precious RAM.  The cost is a few
  211. // cycles and about 100 bytes of flash program memory.
  212. uint8_t  directionFlags[ (NUM_LEDS+7) / 8];
  213.  
  214. bool getPixelDirection( uint16_t i) {
  215.   uint16_t index = i / 8;
  216.   uint8_t  bitNum = i & 0x07;
  217.   // using Arduino 'bitRead' function; expanded code below
  218.   return bitRead( directionFlags[index], bitNum);
  219.   // uint8_t  andMask = 1 << bitNum;
  220.   // return (directionFlags[index] & andMask) != 0;
  221. }
  222.  
  223. void setPixelDirection( uint16_t i, bool dir) {
  224.   uint16_t index = i / 8;
  225.   uint8_t  bitNum = i & 0x07;
  226.   // using Arduino 'bitWrite' function; expanded code below
  227.   bitWrite( directionFlags[index], bitNum, dir);
  228.   //  uint8_t  orMask = 1 << bitNum;
  229.   //  uint8_t andMask = 255 - orMask;
  230.   //  uint8_t value = directionFlags[index] & andMask;
  231.   //  if( dir ) {
  232.   //    value += orMask;
  233.   //  }
  234.   //  directionFlags[index] = value;
  235. }
  236. #endif
  237.  
  238.  
  239. //---------------------------------------------------------------
  240. //EOF
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement