Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Adafruit_NeoPixel.h>
- #include <FastLED.h>
- #include <JC_Button.h>
- #define COLOR_ORDER GRB
- #define CHIPSET WS2812
- #define FIRE_TOTAL_LEDS 46
- #define FIRE_PIN 14
- #define FIRE_BRIGHTNESS_NOMINAL 16
- #define FIRE_BRIGHTNESS_MAX 255
- #define FIRE_SPEED 75// ms between fire refresh rate, higher is slower
- #define COOLING 70
- #define SPARKING 270
- // #define FIRE_ROW_LEDS 11
- // #define FIRE_ROW_COUNT 5
- #define BRIGHTBUTTON_PIN 16
- #define BRIGHT_DELAY_LONG 60000 // how many ms to stay in bright mode after bright button is pressed
- #define BRIGHT_DELAY_SHORT 10000 // how many ms to stay in bright mode after color button is pressed
- #define COLORBUTTON_PIN 17
- Adafruit_NeoPixel FireStrip = Adafruit_NeoPixel(FIRE_TOTAL_LEDS, FIRE_PIN, NEO_GRB + NEO_KHZ800);
- Button BrightButton(BRIGHTBUTTON_PIN); // define the button that increases brightness
- Button ColorButton(COLORBUTTON_PIN); // define the button that toggles color
- CRGBPalette16 gPal; // Palette color for flames
- unsigned long TimerMaster = 0; // master clock timer
- unsigned long TimerFire = 0; // fire update timer
- unsigned long TimerBright = 0; // bright-mode timer
- int FireBrightness = FIRE_BRIGHTNESS_NOMINAL; // brightness value to be changed on button press
- bool FireFade = 0; // to fade fire from MAX to NOMINAL
- byte heat[FIRE_TOTAL_LEDS]; // global array of heat values for all five rows
- bool ColorToggle = 0; // remember the current flame color 0=blue 1=red
- void setup()
- {
- delay(1500); // sanity delay
- gPal = CRGBPalette16( CRGB::Black, CRGB::Blue, CRGB::Aqua, CRGB::White);
- //gPal = HeatColors_p;
- FireStrip.begin(); // initialize fire strip
- FireStrip.setBrightness(FireBrightness);
- BrightButton.begin();
- ColorButton.begin();
- }
- void loop()
- {
- TimerMaster = millis();
- BrightButton.read();
- if (BrightButton.wasPressed())
- {
- // If it's not already bright, crank up the brightness for a certain amount of time
- if (TimerMaster >= TimerBright)
- {
- FireBrightness = FIRE_BRIGHTNESS_MAX;
- FireFade = 0;
- FireStrip.show();
- TimerBright = (TimerMaster + BRIGHT_DELAY_LONG);
- }
- // Otherwise, if it's already bright, go ahead and start fading
- else
- {
- TimerBright = TimerMaster;
- }
- }
- ColorButton.read();
- if (ColorButton.wasPressed())
- {
- // If the button was pressed, toggle the flame color
- if (ColorToggle == 0)
- {
- gPal = HeatColors_p;
- }
- else
- {
- gPal = CRGBPalette16( CRGB::Black, CRGB::Blue, CRGB::Aqua, CRGB::White);
- }
- ColorToggle = !ColorToggle;
- // and crank up the brightness
- FireBrightness = FIRE_BRIGHTNESS_MAX;
- FireFade = 0;
- FireStrip.show();
- TimerBright = (TimerMaster + BRIGHT_DELAY_SHORT);
- }
- if (TimerMaster >= TimerBright) // If the brightness timer has aged out
- {
- // Turn on toggle to slowly fade Fire brightness to nominal value
- FireFade = 1;
- //FireBrightness = FIRE_BRIGHTNESS_NOMINAL;
- }
- if (TimerMaster >= TimerFire)
- {
- if (FireFade == 1 && FireBrightness > FIRE_BRIGHTNESS_NOMINAL)
- {
- FireBrightness = FireBrightness -2;
- if (FireBrightness <= FIRE_BRIGHTNESS_NOMINAL)
- {
- FireBrightness = FIRE_BRIGHTNESS_NOMINAL;
- FireFade = 0;
- // Always reset the color to blue when brightness fade completes
- gPal = CRGBPalette16( CRGB::Black, CRGB::Blue, CRGB::Aqua, CRGB::White);
- ColorToggle = 0;
- }
- }
- FireStrip.setBrightness(FireBrightness);
- Fire2012(0, 6, 0, random(COOLING * 0.8, COOLING * 1.2), random(SPARKING * 0.8, SPARKING * 1.2));
- Fire2012(6, 5, 0, random(COOLING * 0.8, COOLING * 1.2), random(SPARKING * 0.8, SPARKING * 1.2));
- Fire2012(11, 5, 0, random(COOLING * 0.8, COOLING * 1.2), random(SPARKING * 0.8, SPARKING * 1.2));
- Fire2012(16, 5, 0, random(COOLING * 0.8, COOLING * 1.2), random(SPARKING * 0.8, SPARKING * 1.2));
- Fire2012(21, 6, 0, random(COOLING * 0.8, COOLING * 1.2), random(SPARKING * 0.8, SPARKING * 1.2));
- Fire2012(27, 6, 0, random(COOLING * 0.8, COOLING * 1.2), random(SPARKING * 0.8, SPARKING * 1.2));
- Fire2012(33, 6, 0, random(COOLING * 0.8, COOLING * 1.2), random(SPARKING * 0.8, SPARKING * 1.2));
- Fire2012(39, 7, 0, random(COOLING * 0.8, COOLING * 1.2), random(SPARKING * 0.8, SPARKING * 1.2));
- TimerFire = (TimerMaster + FIRE_SPEED);
- FireStrip.show();
- }
- }
- void Fire2012(int LED_START_NUM, int FIRE_ROW_LEDS, bool gReverseDirection, int cool, int spark)
- {
- // Step 1. Cool down every cell a little
- for ( int i = LED_START_NUM; i < (FIRE_ROW_LEDS + LED_START_NUM); i++)
- {
- heat[i] = qsub8( heat[i], random8(0, ((cool * 10) / FIRE_ROW_LEDS) + 2));
- }
- // Step 2. Heat from each cell drifts 'up' and diffuses a little
- for ( int k = (FIRE_ROW_LEDS + LED_START_NUM) - 1; k >= (LED_START_NUM + 2); k--)
- {
- heat[k] = (heat[k - 1] + heat[k - 2] + heat[k - 2] ) / 3;
- }
- // Step 3. Randomly ignite new 'sparks' of heat near the bottom
- if ( random8() < spark )
- {
- int y = random8(3);
- heat[LED_START_NUM + y] = qadd8( heat[LED_START_NUM + y], random8(160, 255) );
- }
- // Step 4. Map from heat cells to LED colors
- for ( int j = LED_START_NUM; j < (FIRE_ROW_LEDS + LED_START_NUM); j++)
- {
- // CRGB color = HeatColor(min(150, heat[j]));
- CRGB color = ColorFromPalette(gPal, min(150, heat[j]));
- int pixelnumber;
- if ( gReverseDirection )
- {
- pixelnumber = ((FIRE_ROW_LEDS + LED_START_NUM) - 1) - (j - LED_START_NUM);
- }
- else
- {
- pixelnumber = j;
- }
- FireStrip.setPixelColor(pixelnumber, color.r, color.g, color.b);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement