Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Adafruit_NeoPixel.h>
- #include <FastLED.h>
- #define COLOR_ORDER GRB
- #define CHIPSET WS2812
- #define SABER_NUM_LEDS 20
- #define SABER_PIN 18
- #define SABER_COLOR 0x44b0db // use RGB (hex) color picker
- #define SABER_SPEED 20 //ms delay between lights, higher is slower
- #define SABER_PAUSE_TOP 3000
- #define SABER_PAUSE_BOTTOM 1000
- #define SABER_BRIGHTNESS_NOMINAL 8
- #define SABER_BRIGHTNESS_MAX 255
- #define FIRE_TOTAL_LEDS 55
- #define FIRE_PIN 19
- #define FIRE_BRIGHTNESS 200
- #define FIRE_SPEED 500// ms between fire refresh rate, higher is slower
- #define COOLING 70
- #define SPARKING 270
- #define FIRE_ROW_LEDS 11
- #define FIRE_ROW_COUNT 5
- CRGB fireleds[FIRE_ROW_LEDS];
- CRGB firestrip[FIRE_TOTAL_LEDS];
- Adafruit_NeoPixel SaberStrip = Adafruit_NeoPixel(SABER_NUM_LEDS, SABER_PIN, NEO_GRB + NEO_KHZ800);
- //Adafruit_NeoPixel FireStrip = Adafruit_NeoPixel(FIRE_TOTAL_LEDS, FIRE_PIN, NEO_GRB + NEO_KHZ800);
- unsigned long TimerMaster = 0; // master clock timer
- unsigned long TimerSaber = 0; // saber loop timer
- unsigned long TimerFire = 0; // fire update timer
- int SaberLoopCounter = 0; // loop counter for Saber
- bool SaberToggle = 0; // wipe to color or wipe to off
- int SaberBrightness = SABER_BRIGHTNESS_NOMINAL; // brightness value to be randomized
- void setup()
- {
- delay(3000); // sanity delay
- SaberStrip.begin(); // initialize saber strip
- SaberStrip.setBrightness(SaberBrightness);
- FastLED.addLeds<CHIPSET, FIRE_PIN, COLOR_ORDER>(firestrip, FIRE_TOTAL_LEDS).setCorrection( TypicalLEDStrip );
- }
- void loop()
- {
- TimerMaster = millis();
- if (TimerMaster >= TimerSaber)
- {
- saberPower();
- }
- if (TimerMaster >= TimerFire)
- {
- Fire2012(0, 0, random(COOLING * 0.8, COOLING * 1.2), random(SPARKING * 0.8, SPARKING * 1.2));
- Fire2012(11, 1, random(COOLING * 0.8, COOLING * 1.2), random(SPARKING * 0.8, SPARKING * 1.2));
- Fire2012(22, 0, random(COOLING * 0.8, COOLING * 1.2), random(SPARKING * 0.8, SPARKING * 1.2));
- Fire2012(33, 1, random(COOLING * 0.8, COOLING * 1.2), random(SPARKING * 0.8, SPARKING * 1.2));
- Fire2012(44, 0, random(COOLING * 0.8, COOLING * 1.2), random(SPARKING * 0.8, SPARKING * 1.2));
- TimerFire = (TimerMaster + FIRE_SPEED);
- FastLED.show();
- //FireStrip.show();
- }
- }
- void saberPower()
- {
- // Check to see if we're turning on (0) or turning off (1)
- if (SaberToggle == 0)
- {
- // Count from bottom to top, turning each one on
- // Wait for SABER_SPEED ms before turning the next one on
- if (SaberLoopCounter < SABER_NUM_LEDS)
- {
- SaberStrip.setPixelColor(SaberLoopCounter, SABER_COLOR);
- SaberStrip.show();
- TimerSaber = (TimerMaster + SABER_SPEED);
- SaberLoopCounter++;
- }
- // When top is reached, toggle the on/off
- // Then wait for SABER_PAUSE_TOP ms before turning off
- else
- {
- SaberToggle = !SaberToggle;
- TimerSaber = (TimerMaster + SABER_PAUSE_TOP);
- }
- }
- else
- {
- // Count from top to bottom, turning each one off
- // Wait for SABER_SPEED ms before turning the next one off
- if (SaberLoopCounter > 0)
- {
- SaberLoopCounter--;
- SaberStrip.setPixelColor(SaberLoopCounter, 0);
- SaberStrip.show();
- TimerSaber = (TimerMaster + SABER_SPEED);
- }
- // When top is reached, toggle the on/off
- // Then wait for SABER_PAUSE_TOP ms before turning off
- else
- {
- SaberToggle = !SaberToggle;
- TimerSaber = (TimerMaster + SABER_PAUSE_BOTTOM);
- }
- }
- }
- void Fire2012(int LED_START_NUM, bool gReverseDirection, int cool, int spark)
- {
- // Array of temperature readings at each simulation cell
- static byte heat[FIRE_ROW_LEDS];
- // randomSeed(analogRead(0));
- // Step 1. Cool down every cell a little
- for ( int i = 0; i < FIRE_ROW_LEDS; 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 - 1; k >= 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(7);
- heat[y] = qadd8( heat[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 - LED_START_NUM]));
- int pixelnumber;
- if ( gReverseDirection )
- {
- pixelnumber = ((FIRE_ROW_LEDS + LED_START_NUM) - 1) - (j - LED_START_NUM);
- }
- else
- {
- pixelnumber = j;
- }
- firestrip[pixelnumber] = color;
- //FireStrip.setPixelColor(pixelnumber, color);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement