Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //***************************************************************
- // Holiday Server Rack (blinking), for Michael Sime
- // By Marc Miller
- // Based on a heavily modified version of Mark's Holiday lights
- // Dec 2015
- //***************************************************************
- #include "FastLED.h"
- #define LED_PIN 6
- #define LED_TYPE NEOPIXEL
- #define COLOR_ORDER RGB
- #define NUM_LEDS 12
- CRGB leds[NUM_LEDS];
- #define MASTER_BRIGHTNESS 50
- #define STARTING_BRIGHTNESS 120
- #define FADE_IN_SPEED 220 // 0-255 smaller number is slower
- #define FADE_OUT_SPEED 160 // 0-255 smaller number is slower
- #define DENSITY 150 // 0-255 smaller is less dense
- #define ACTIVITY 6 // Use a slghtly larger number for a more "activity"
- #define TWINKLE_CHANCE 85 // 0-100 How often to add a twinkle
- #define REPEATE_CHANCE 60 // 0-100 Percent chance of changing the same pixel
- void setup() {
- //delay(3000);
- FastLED.addLeds<NEOPIXEL,LED_PIN>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
- FastLED.setBrightness(MASTER_BRIGHTNESS);
- }
- //---------------------------------------------------------------
- void loop()
- {
- chooseColorPalette();
- colortwinkles();
- FastLED.show();
- FastLED.delay(20); // Slow the whole display down a bit.
- }
- //---------------------------------------------------------------
- CRGBPalette16 gPalette;
- void chooseColorPalette()
- {
- uint8_t numberOfPalettes = 13;
- uint8_t secondsPerPalette = random(2,5);
- uint8_t whichPalette = (millis()/(1000*secondsPerPalette)) % numberOfPalettes;
- //whichPalette = 3; // To temporarily locked a palette choice to work on a color
- // Palette colors
- CRGB r(CRGB::Red), b(CRGB::Blue), w(85,85,85), g(CRGB::Green), W(CRGB::White), l(0xE1A024);
- // The general basic color (and a similar basic color) that display most of the time
- CRGB z(CHSV(50,255,210)), zg(CHSV(70,245,55));
- // Other custom colors
- CRGB dr(CHSV(0,220,35)), rw(CHSV(130,80,100)), dg(CRGB::DarkGreen), yg(CHSV(55,255,40)), bk(CRGB::Black);
- switch( whichPalette) {
- case 0:
- gPalette = CRGBPalette16( z,z,z,z, z,z,z,z, z,z,z,z, zg,zg,zg,zg ); // basic
- break;
- case 1:
- gPalette = CRGBPalette16( b,z,z,z, z,z,z,z, z,z,z,z, z,z,z,z ); // basic + BLUE
- break;
- case 2:
- gPalette = CRGBPalette16( dr,z,z,z, z,z,z,z, z,z,z,z, z,z,z,z ); // basic + Dark RED
- break;
- case 3:
- gPalette = CRGBPalette16( rw,z,z,z, z,z,z,z, zg,z,z,z, zg,z,z,z ); // basic + blue-WHITE
- break;
- case 4:
- gPalette = CRGBPalette16( dg,z,z,z, z,z,z,z, z,z,z,z, z,z,z,z ); // basic + Dark GREEN
- break;
- case 5:
- gPalette = CRGBPalette16( yg,yg,yg,yg, z,z,z,z, z,z,z,z, z,z,z,z ); // basic + YELLOWGREEN
- break;
- case 6:
- gPalette = CRGBPalette16( b,z,z,z, dr,z,z,z, z,z,z,z, zg,zg,zg,zg ); // basic + BLUE + Dark RED
- break;
- case 7:
- gPalette = CRGBPalette16( b,z,z,z, yg,z,z,z, z,z,z,z, zg,zg,zg,zg ); // basic + BLUE + YELLOWGREEN
- break;
- case 8:
- gPalette = CRGBPalette16( b,z,z,z, dg,z,z,z, z,z,z,z, zg,zg,zg,zg ); // basic + BLUE + Dark GREEN
- break;
- case 9:
- gPalette = CRGBPalette16( b,z,z,z, b,z,z,z, z,z,z,z, zg,zg,zg,zg ); // basic + BLUE + BLUE
- break;
- case 10:
- gPalette = CRGBPalette16( bk,z,z,z, bk,bk,z,z, bk,z,z,z, zg,zg,zg,zg ); // basic + Blacked Out
- break;
- // Below are just repeats of the basic colors to make it slightly more likely to show up.
- case 11:
- gPalette = CRGBPalette16( z,z,z,z, z,z,z,z, z,z,z,z, zg,zg,zg,zg ); // basic
- break;
- case 12:
- gPalette = CRGBPalette16( z,z,z,z, z,z,z,z, z,z,z,z, zg,zg,zg,zg ); // basic
- break;
- }
- }
- enum { GETTING_DARKER = 0, GETTING_BRIGHTER = 1 };
- //---------------------------------------------------------------
- void colortwinkles()
- {
- // Make each pixel brighter or darker, depending on its 'direction' flag.
- brightenOrDarkenEachPixel( FADE_IN_SPEED, FADE_OUT_SPEED);
- // Now consider adding a new random twinkle
- if (random8(100) < TWINKLE_CHANCE) {
- if (random8() < DENSITY) {
- int pos = random16(NUM_LEDS);
- if (random8(100) < REPEATE_CHANCE) {
- if( !leds[pos]) {
- leds[pos] = ColorFromPalette( gPalette, random8(), STARTING_BRIGHTNESS, NOBLEND);
- setPixelDirection(pos, GETTING_BRIGHTER);
- }
- } else {
- leds[pos] = ColorFromPalette( gPalette, random8(), STARTING_BRIGHTNESS, NOBLEND);
- setPixelDirection(pos, GETTING_BRIGHTER);
- }
- }
- }
- }
- void brightenOrDarkenEachPixel( fract8 fadeUpAmount, fract8 fadeDownAmount)
- {
- for( uint16_t i = 0; i < NUM_LEDS; i++) {
- if( getPixelDirection(i) == GETTING_DARKER) {
- int pick = 0;
- int minCutoff = 10;
- // This pixel is getting darker
- if (random8() < ACTIVITY) {
- pick = random8(NUM_LEDS);
- leds[pick] = makeDarker( leds[pick], fadeDownAmount);
- }
- if( leds[pick].r == STARTING_BRIGHTNESS || leds[pick].g == STARTING_BRIGHTNESS || leds[pick].b == STARTING_BRIGHTNESS ) {
- setPixelDirection(pick, GETTING_BRIGHTER);
- }
- if( leds[pick].r < minCutoff || leds[pick].g < minCutoff || leds[pick].b < minCutoff ) { // turn dim pixels completely off/black.
- leds[pick] = CRGB::Black;
- setPixelDirection(pick, GETTING_BRIGHTER);
- }
- } else {
- // This pixel is getting brighter
- leds[i] = makeBrighter( leds[i], fadeUpAmount);
- // now check to see if we've maxxed out the brightness
- if( leds[i].r == 255 || leds[i].g == 255 || leds[i].b == 255) {
- // if so, turn around and start getting darker
- setPixelDirection(i, GETTING_DARKER);
- }
- }
- }
- }
- //---------------------------------------------------------------
- CRGB makeBrighter( const CRGB& color, fract8 howMuchBrighter)
- {
- CRGB incrementalColor = color;
- incrementalColor.nscale8( howMuchBrighter);
- return color + incrementalColor;
- }
- CRGB makeDarker( const CRGB& color, fract8 howMuchDarker)
- {
- CRGB newcolor = color;
- newcolor.nscale8( 255 - howMuchDarker);
- return newcolor;
- }
- // For illustration purposes, there are two separate implementations
- // provided here for the array of 'directionFlags':
- // - a simple one, which uses one byte (8 bits) of RAM for each pixel, and
- // - a compact one, which uses just one BIT of RAM for each pixel.
- // Set this to 1 or 8 to select which implementation
- // of directionFlags is used. 1=more compact, 8=simpler.
- #define BITS_PER_DIRECTION_FLAG 1
- #if BITS_PER_DIRECTION_FLAG == 8
- // Simple implementation of the directionFlags array,
- // which takes up one byte (eight bits) per pixel.
- uint8_t directionFlags[NUM_LEDS];
- bool getPixelDirection( uint16_t i) {
- return directionFlags[i];
- }
- void setPixelDirection( uint16_t i, bool dir) {
- directionFlags[i] = dir;
- }
- #endif
- #if BITS_PER_DIRECTION_FLAG == 1
- // Compact (but more complicated) implementation of
- // the directionFlags array, using just one BIT of RAM
- // per pixel. This requires a bunch of bit wrangling,
- // but conserves precious RAM. The cost is a few
- // cycles and about 100 bytes of flash program memory.
- uint8_t directionFlags[ (NUM_LEDS+7) / 8];
- bool getPixelDirection( uint16_t i) {
- uint16_t index = i / 8;
- uint8_t bitNum = i & 0x07;
- // using Arduino 'bitRead' function; expanded code below
- return bitRead( directionFlags[index], bitNum);
- // uint8_t andMask = 1 << bitNum;
- // return (directionFlags[index] & andMask) != 0;
- }
- void setPixelDirection( uint16_t i, bool dir) {
- uint16_t index = i / 8;
- uint8_t bitNum = i & 0x07;
- // using Arduino 'bitWrite' function; expanded code below
- bitWrite( directionFlags[index], bitNum, dir);
- // uint8_t orMask = 1 << bitNum;
- // uint8_t andMask = 255 - orMask;
- // uint8_t value = directionFlags[index] & andMask;
- // if( dir ) {
- // value += orMask;
- // }
- // directionFlags[index] = value;
- }
- #endif
- //---------------------------------------------------------------
- //EOF
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement