Skip to content
  • 5 Votes
    29 Posts
    21k Views
    BulldogLowellB
    Wow this is an old topic! But... I was working on a project and thought that anyone who was looking for a NeoPixel candle example that isn't blocking, well this would be the place. It flickers and flutters like a real candle. The only thing is that the flame isn't illuminated like these nice Luminaras, but I'm too cheap to buy them! Keep in mind that my example here uses GRB LEDs, but you merely need to reorder the variables in order to get this working for RGB. Plus the Time Library is not Arduino, so you will have to play with that too for a good randomSeed(). It looks pretty realistic with even a single neoPixel in a sheet of A4 paper rolled into a cylinder. I'll post a video when I can. Have fun with it: #include "neopixel.h" enum CandleStates{ BURN_CANDLE, FLICKER_CANDLE, FLUTTER_CANDLE, MODES_MAX_CANDLE }; enum PixelSelect{ EVERY_PIXEL, SINGLE_PIXEL, }; class Candle : public Adafruit_NeoPixel { public: Candle(uint16_t count, uint8_t pin, uint8_t type); Candle(uint16_t count, uint8_t pin, uint8_t type, PixelSelect pixel, uint32_t pixNum = 0); ~Candle(){}; void update(); private: bool fire(uint8_t greenDropValue, uint32_t cycleTime); PixelSelect _pixelMode = EVERY_PIXEL; uint32_t _pixNum = 0; CandleStates _mode; uint32_t _lastModeChange; uint32_t _modeDuration; uint8_t _redPx = 255; uint8_t _bluePx = 10; //10 for 5v, 15 for 3.3v uint8_t _grnHigh = 100; //110-120 for 5v, 135 for 3.3v uint8_t _grnPx = 100; uint32_t _lastBurnUpdate = 0; int _direction = 1; }; Candle::Candle(uint16_t count, uint8_t pin, uint8_t type) : Adafruit_NeoPixel(count, pin, type) { randomSeed(Time.now() + micros()); _mode = BURN_CANDLE; } Candle::Candle(uint16_t count, uint8_t pin, uint8_t type, PixelSelect pixel, uint32_t pixNum) : Adafruit_NeoPixel(count, pin, type) { _pixelMode = pixel; _pixNum = pixNum; } void Candle::update() { if(millis() - _lastModeChange > _modeDuration) { _mode = static_cast<CandleStates>(random(MODES_MAX_CANDLE)); _modeDuration = random(1000, 8000); _lastModeChange = millis(); //Serial.printlnf("New state: %d\tTime: %d", static_cast<int>(_mode), _modeDuration); } switch(_mode) { case BURN_CANDLE: this->fire(10, 120); break; case FLICKER_CANDLE: this->fire(15, 120); break; case FLUTTER_CANDLE: this->fire(30, 120); break; }; } bool Candle::fire(uint8_t greenDropValue, uint32_t cycleTime) { int currentMillis = millis(); if(currentMillis - _lastBurnUpdate > (cycleTime / greenDropValue / 2)) { _grnPx = constrain(_grnPx += _direction, _grnHigh - greenDropValue, _grnHigh); if(_grnPx == _grnHigh - greenDropValue or _grnPx == _grnHigh) { _direction *= -1; } switch (_pixelMode) { case EVERY_PIXEL: for(int i = 0; i < this->numPixels(); i++) { this->setPixelColor(i, _grnPx, _redPx, _bluePx); } break; case SINGLE_PIXEL: this->setPixelColor(_pixNum, _grnPx, _redPx, _bluePx); break; } this->show(); _lastBurnUpdate = currentMillis; } } #define PIXEL_COUNT 2 #define PIXEL_PIN D2 #define PIXEL_TYPE WS2812B // I'M USING GRB WS2821B's here Candle candle = Candle(PIXEL_COUNT, PIXEL_PIN, PIXEL_TYPE, SINGLE_PIXEL); void setup(void) { Serial.begin(115200); pinMode(13, OUTPUT); candle.begin(); candle.show(); Serial.println("Started program"); } void loop(void) { candle.update(); static uint32_t lastFlashMillis = 0; if(millis() - lastFlashMillis > 250) { digitalWrite(13, !digitalRead(13)); lastFlashMillis = millis(); } }
  • 2 Votes
    5 Posts
    6k Views
    Z
    @steets250 said: Instead of storing raw codes, you could also try using pronto codes for some of your IR devices. The pronto codes will take up less space than the longer raw codes. First, the long form Pronto codes and the irRemote library raw buffer both use 2 byte values to store on and off times, so they should in most cases be approximately the same length (Pronto codes have 8 bytes of additional header). (Of course if you stored Pronto codes in hex format they'd be much longer). And in order to use irRemote to send the pulses, it will need the raw buffer values in its expected format. I suspect one could translate stored Pronto codes into raw values in the sensuator node before calling the library, but if the Pronto code is no shorter, why not simply store the raw codes instead? It might even be possible to modify the irRemote library to send values using timing directly from PROGMEM, and not even need the RAM to buffer the raw codes (when only sending).

10

Online

11.7k

Users

11.2k

Topics

113.1k

Posts