Created
August 31, 2018 23:23
-
-
Save chemdoc77/bbf6baee03e1f491e75c556145959724 to your computer and use it in GitHub Desktop.
Candle Effect for NeoPixel Jewel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Candle Effect for a NeoPixel Jewel by Chemdoc77 | |
/* This sketch is based on: | |
* Code from Christopher Smolinski: | |
* | |
* https://plus.google.com/+ChristopherSmolinski/posts/a8bEKwtYhjQ | |
* | |
* which is based on code from Mark Kriegsman: | |
* | |
* https://gist.github.com/kriegsman/5408ecd397744ba0393e | |
* | |
*/ | |
#include "FastLED.h" | |
#define LED_PIN 6 | |
#define LED_TYPE NEOPIXEL | |
#define NUM_LEDS 7 | |
CRGB leds[NUM_LEDS]; | |
#define MASTER_BRIGHTNESS 140 | |
#define STARTING_BRIGHTNESS 40 | |
#define FADE_IN_SPEED 26//32 or 30 | |
#define FADE_OUT_SPEED 14//20 0r 18 | |
#define DENSITY 255 | |
//CRGBPalette16 gPalette; | |
CRGB r(CRGB::Red), b(CRGB::Blue), w(85,85,85), g(CRGB::Green), W(CRGB::White), l(0xE1A024), y(CRGB::Yellow), o(CRGB::Orange), orr(CRGB::OrangeRed); | |
CRGBPalette16 gPalette = CRGBPalette16( r,r,o,o, orr,r,r,r, o,o,r,orr, r,o,orr,r ); ; | |
void setup() { | |
delay(1000); | |
FastLED.addLeds<LED_TYPE,LED_PIN>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip); | |
FastLED.setBrightness(MASTER_BRIGHTNESS); | |
FastLED.setMaxPowerInVoltsAndMilliamps(5,1500); | |
set_max_power_indicator_LED(13); | |
fill_solid(leds, NUM_LEDS, CRGB::Black); | |
FastLED.show(); | |
} | |
void loop() | |
{ | |
colortwinkles(); | |
FastLED.delay(20); | |
} | |
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() < DENSITY ) { | |
int pos = random16(NUM_LEDS); | |
if( !leds[pos]) { | |
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) { | |
// This pixel is getting darker | |
leds[i] = makeDarker( leds[i], fadeDownAmount); | |
} 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment