Advertisement
marmil

holiday_server_rack_300

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