Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // candle for Adafruit NeoPixel
- // 1 pixel version
- // by Tim Bartlett, December 2013
- // current settings for 5v Trinket
- #include <FastLED.h>
- #define LED_PIN 2
- #define NUM_LEDS 1
- #define BRIGHTNESS 100 // set at 25 for power saving
- struct CRGB leds[NUM_LEDS];
- byte onLedNumber = 0;
- unsigned long previousMillis = 0;
- // color variables: mix RGB (0-255) for desired yellow
- int redPx = 255;
- int grnHigh = 110; //110-120 for 5v, 135 for 3.3v
- int bluePx = 10; //10 for 5v, 15 for 3.3v
- // animation time variables, with recommendations
- int burnDepth = 20; //10 for 5v, 14 for 3.3v -- how much green dips below grnHigh for normal burn
- int flutterDepth = 30; //25 for 5v, 30 for 3.3v -- maximum dip for flutter
- int cycleTime = 120; //120 -- duration of one dip in milliseconds
- // pay no attention to that man behind the curtain
- unsigned int fDelay;
- unsigned int fRep;
- int flickerDepth;
- int burnDelay;
- int burnLow;
- int flickDelay;
- int flickLow;
- int flutDelay;
- int flutLow;
- void flutter(unsigned int f);
- void flicker(unsigned int f);
- void burn(unsigned int f);
- void on(unsigned int f);
- void fire(int grnLow);
- void setup() {
- FastLED.addLeds<WS2811, LED_PIN, RGB>(leds, NUM_LEDS);
- FastLED.setBrightness( BRIGHTNESS );
- flickerDepth = (burnDepth + flutterDepth) / 2.4;
- burnLow = grnHigh - burnDepth;
- burnDelay = (cycleTime / 2) / burnDepth;
- flickLow = grnHigh - flickerDepth;
- flickDelay = (cycleTime / 2) / flickerDepth;
- flutLow = grnHigh - flutterDepth;
- flutDelay = ((cycleTime / 2) / flutterDepth);
- }
- // In loop, call CANDLE STATES, with duration in seconds
- // 1. on() = solid yellow
- // 2. burn() = candle is burning normally, flickering slightly
- // 3. flicker() = candle flickers noticeably
- // 4. flutter() = the candle needs air!
- void loop() {
- burn(10);
- flicker(5);
- burn(8);
- flutter(6);
- burn(3);
- on(10);
- burn(10);
- flicker(10);
- }
- // basic fire function - not called in main loop
- void fire(int grnLow) {
- for (int grnPx = grnHigh; grnPx > grnLow; grnPx--) {
- previousMillis = 0;
- unsigned long currentMillis = millis();
- if (currentMillis - previousMillis > fRep) {
- previousMillis = currentMillis;
- leds[onLedNumber] = CRGB(redPx, grnPx, bluePx);
- FastLED.show();
- }
- }
- for (int grnPx = grnLow; grnPx < grnHigh; grnPx++) {
- previousMillis = 0;
- unsigned long currentMillis = millis();
- if (currentMillis - previousMillis > fRep) {
- previousMillis = currentMillis;
- leds[onLedNumber] = CRGB(redPx, grnPx, bluePx);
- FastLED.show();
- }
- }
- }
- // fire animation
- void on(unsigned int f) {
- previousMillis = 0;
- unsigned long currentMillis = millis();
- if (currentMillis - previousMillis > fRep) {
- previousMillis = currentMillis;
- fRep = f * 1000;
- int grnPx = grnHigh - 5;
- leds[onLedNumber] = CRGB(redPx, grnPx, bluePx);
- FastLED.show();
- }
- }
- void burn(unsigned int f) {
- fRep = f * 8;
- fDelay = burnDelay;
- for (unsigned int var = 0; var < fRep; var++) {
- fire(burnLow);
- }
- }
- void flicker(unsigned int f) {
- fRep = f * 8;
- fDelay = burnDelay;
- fire(burnLow);
- fDelay = flickDelay;
- for (unsigned int var = 0; var < fRep; var++) {
- fire(flickLow);
- }
- fDelay = burnDelay;
- fire(burnLow);
- fire(burnLow);
- fire(burnLow);
- }
- void flutter(unsigned int f) {
- fRep = f * 8;
- fDelay = burnDelay;
- fire(burnLow);
- fDelay = flickDelay;
- fire(flickLow);
- fDelay = flutDelay;
- for (unsigned int var = 0; var < fRep; var++) {
- fire(flutLow);
- }
- fDelay = flickDelay;
- fire(flickLow);
- fire(flickLow);
- fDelay = burnDelay;
- fire(burnLow);
- fire(burnLow);
- }
Advertisement
Add Comment
Please, Sign In to add comment