Go Down

Topic: Help starting Fire code in middle of strip going out to both ends (Read 370 times) previous topic - next topic

damadscientist

Hello All,

I am just picking this up and am trying to re-write the code below to start from the middle and run out to both sides. Basically mirror out from the middle of the strip instead of only from LED 0. I'm using a 60LED per meter strip (Actually 75 LEDs, a little over a meter). Ideally, the fire would start around LED38 towards LED74 and LED37 towards LED0 at the same time.

Any help would be greatly appreciated!!

Originial code and video of effect is at the link below:

https://www.tweaking4all.com/hardware/arduino/adruino-led-strip-effects/#LEDStripEffectFire

I basically want a 75LED ring where both ends meet at the top. The bottom of the ring is where the fire starts and burns upwards around each side of the ring towards the top.

Code: [Select]

#include <Adafruit_NeoPixel.h>
#define PIN 6
#define NUM_LEDS 75
// Parameter 1 = number of pixels in strip
// Parameter 2 = pin number (most are valid)
// Parameter 3 = pixel type flags, add together as needed:
//   NEO_KHZ800  800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
//   NEO_KHZ400  400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
//   NEO_GRB     Pixels are wired for GRB bitstream (most NeoPixel products)
//   NEO_RGB     Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_LEDS, PIN, NEO_GRB + NEO_KHZ800);

void setup() {
  strip.begin();
  strip.show(); // Initialize all pixels to 'off'
}

void loop() {
  Fire(55,120,15);
}

void Fire(int Cooling, int Sparking, int SpeedDelay) {
  static byte heat[NUM_LEDS];
  int cooldown;
 
  // Step 1.  Cool down every cell a little
  for( int i = 0; i < NUM_LEDS; i++) {
    cooldown = random(0, ((Cooling * 10) / NUM_LEDS) + 2);
   
    if(cooldown>heat[i]) {
      heat[i]=0;
    } else {
      heat[i]=heat[i]-cooldown;
    }
  }
 
  // Step 2.  Heat from each cell drifts 'up' and diffuses a little
  for( int k= NUM_LEDS - 1; k >= 2; k--) {
    heat[k] = (heat[k - 1] + heat[k - 2] + heat[k - 2]) / 3;
  }
   
  // Step 3.  Randomly ignite new 'sparks' near the bottom
  if( random(255) < Sparking ) {
    int y = random(7);
    heat[y] = heat[y] + random(160,255);
    //heat[y] = random(160,255);
  }

  // Step 4.  Convert heat to LED colors
  for( int j = 0; j < NUM_LEDS; j++) {
    setPixelHeatColor(j, heat[j] );
  }

  showStrip();
  delay(SpeedDelay);
}

void setPixelHeatColor (int Pixel, byte temperature) {
  // Scale 'heat' down from 0-255 to 0-191
  byte t192 = round((temperature/255.0)*191);
 
  // calculate ramp up from
  byte heatramp = t192 & 0x3F; // 0..63
  heatramp <<= 2; // scale up to 0..252
 
  // figure out which third of the spectrum we're in:
  if( t192 > 0x80) {                     // hottest
    setPixel(Pixel, 255, 255, heatramp);
  } else if( t192 > 0x40 ) {             // middle
    setPixel(Pixel, 255, heatramp, 0);
  } else {                               // coolest
    setPixel(Pixel, heatramp, 0, 0);
  }
}

void showStrip() {
 #ifdef ADAFRUIT_NEOPIXEL_H
   // NeoPixel
   strip.show();
 #endif
 #ifndef ADAFRUIT_NEOPIXEL_H
   // FastLED
   FastLED.show();
 #endif
}

void setPixel(int Pixel, byte red, byte green, byte blue) {
 #ifdef ADAFRUIT_NEOPIXEL_H
   // NeoPixel
   strip.setPixelColor(Pixel, strip.Color(red, green, blue));
 #endif
 #ifndef ADAFRUIT_NEOPIXEL_H
   // FastLED
   leds[Pixel].r = red;
   leds[Pixel].g = green;
   leds[Pixel].b = blue;
 #endif
}

void setAll(byte red, byte green, byte blue) {
  for(int i = 0; i < NUM_LEDS; i++ ) {
    setPixel(i, red, green, blue);
  }
  showStrip();
}


PaulRB

Quote
Basically mirror out from the middle of the strip
Do you want it to exactly mirror? Would it be acceptable if the two sides were exactly mirrored (for example if both sides cannot be seen together from any angle)?

If so, here is a simple solution. Cut the strip in half. Reverse one half so that the data inputs of both strips are next to each other. Connect the same data signal to both halves. They will automatically mirror each other. In the code, set NUM_LEDS to half is current value.

PS: +1 Karma for using code tags. Now learn how to post links correctly also, so they can be clicked.

Jaymer

Here's ya go.
Added only 2 lines.
MIRROR = ### at the top

and Lines 53/54

pretty straightforward.
I ran it on no 144 pixels - looks neat

jaymer...

Code: [Select]

#include <Adafruit_NeoPixel.h>
#define PIN 3
#define NUM_LEDS 74   // set to an even number
#define MIRROR 37     // set to 1/2 of the LED count
// Parameter 1 = number of pixels in strip
// Parameter 2 = pin number (most are valid)
// Parameter 3 = pixel type flags, add together as needed:
//   NEO_KHZ800  800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
//   NEO_KHZ400  400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
//   NEO_GRB     Pixels are wired for GRB bitstream (most NeoPixel products)
//   NEO_RGB     Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_LEDS, PIN, NEO_GRB + NEO_KHZ800);

void setup() {
  strip.begin();
  strip.show(); // Initialize all pixels to 'off'
}

void loop() {
  Fire(55,120,15,1);
}

void Fire(int Cooling, int Sparking, int SpeedDelay, int Mirror) {
  static byte heat[NUM_LEDS];
  int cooldown;
 
  // Step 1.  Cool down every cell a little
  for( int i = 0; i < NUM_LEDS; i++) {
    cooldown = random(0, ((Cooling * 10) / NUM_LEDS) + 2);
   
    if(cooldown>heat[i]) {
      heat[i]=0;
    } else {
      heat[i]=heat[i]-cooldown;
    }
  }
 
  // Step 2.  Heat from each cell drifts 'up' and diffuses a little
  for( int k= NUM_LEDS - 1; k >= 2; k--) {
    heat[k] = (heat[k - 1] + heat[k - 2] + heat[k - 2]) / 3;
  }
   
  // Step 3.  Randomly ignite new 'sparks' near the bottom
  if( random(255) < Sparking ) {
    int y = random(7);
    heat[y] = heat[y] + random(160,255);
    //heat[y] = random(160,255);
  }

  // Step 4.  Convert heat to LED colors
  for( int j = 0; j < NUM_LEDS - MIRROR; j++) {
    setPixelHeatColor(j+MIRROR, heat[j] );
    setPixelHeatColor(MIRROR-j, heat[j] );
    }

  showStrip();
  delay(SpeedDelay);
}

void setPixelHeatColor (int Pixel, byte temperature) {
  // Scale 'heat' down from 0-255 to 0-191
  byte t192 = round((temperature/255.0)*191);
 
  // calculate ramp up from
  byte heatramp = t192 & 0x3F; // 0..63
  heatramp <<= 2; // scale up to 0..252
 
  // figure out which third of the spectrum we're in:
  if( t192 > 0x80) {                     // hottest
    setPixel(Pixel, 255, 255, heatramp);
  } else if( t192 > 0x40 ) {             // middle
    setPixel(Pixel, 255, heatramp, 0);
  } else {                               // coolest
    setPixel(Pixel, heatramp, 0, 0);
  }
}

void showStrip() {
 #ifdef ADAFRUIT_NEOPIXEL_H
   // NeoPixel
   strip.show();
 #endif
 #ifndef ADAFRUIT_NEOPIXEL_H
   // FastLED
   FastLED.show();
 #endif
}

void setPixel(int Pixel, byte red, byte green, byte blue) {
 #ifdef ADAFRUIT_NEOPIXEL_H
   // NeoPixel
   strip.setPixelColor(Pixel, strip.Color(red, green, blue));
 #endif
 #ifndef ADAFRUIT_NEOPIXEL_H
   // FastLED
   leds[Pixel].r = red;
   leds[Pixel].g = green;
   leds[Pixel].b = blue;
 #endif
}

void setAll(byte red, byte green, byte blue) {
  for(int i = 0; i < NUM_LEDS; i++ ) {
    setPixel(i, red, green, blue);
  }
  showStrip();
}

damadscientist

Hello PaulRB, I thought of the same thing but then all other effects would have to be written to be halves :-).

Jaymer,

You are most awesome sir! Exactly what I was looking for. Is there any documentation or tutorials out there that may have had this or that is good for noobs to review?

A video of the results is below:

https://youtu.be/Dj9sLt2-2qc

I changed 55 to 115 as it looked better on the ring

void loop() {
  Fire(115,120,15,1);

PaulRB

Hello PaulRB, I thought of the same thing but then all other effects would have to be written to be halves :-).
You wasted my time by not mentioning those other patterns before, or the options you had already considered and rejected. Next time you ask.for help on the forum, please be more considerate of others.

Jaymer

You wasted my time by not mentioning those other patterns before, or the options you had already considered and rejected. Next time you ask.for help on the forum, please be more considerate of others.
a big LOL

Jaymer

Jaymer,

You are most awesome sir! Exactly what I was looking for. Is there any documentation or tutorials out there that may have had this or that is good for noobs to review?

A video of the results is below:

https://youtu.be/Dj9sLt2-2qc
Bad Ass my man!!!

There's no tutorials that are gonna teach you that - its just math/logic/programming.  Had nothing to do with LEDs.   Hope people enjoy what you made!  I'm gonna go watch that video again!!!

<------ PS _ just to made Paul more mad - how about clicking the
<------ Karma [Add] button over on the left.

PaulRB

You deserve +1 Karma for helping the OP. And I'm going to give you it despite the adolescent comment. But you have to understand you were lucky this time. I'm lucky like that sometimes too. But none of us are psychics. More often than not, when you guess what a forum newbie wants or needs but lacks the experience to properly or fully explain, you will be wrong, like I was this time. If you don't point this out to them, they don't always figure it out for themselves and won't learn from their mistakes. The skill is "putting yourself in someone else's shoes".

Jaymer

<snip> But you have to understand you were lucky this time. <snip>
The skill is "putting yourself in someone else's shoes".
"I was lucky" - LOL
No, I just read what the guy asked and answered him.  There was no luck... is WAS skill.
Instead of badgering him, I decided I'd invest some time (was only 10-15 minutes, but still, whatever it was going to take I was going to do) and copied his code, figured it out and replied. 

SOME people on here remind me of this...
When I was at University, I was blown away that some professors thought they were God's gift to the U & students.  In a basic freshman class (can't remember if it was Soc 301 or Phil 301), they made a freshman do a term paper, AND 3 debates and something else off the chain - like they felt they needed to "make their mark" on these freshmen.  Always resented that.  These posters don't need "SOME people" to be their dad or teach them a life lesson or to be a hall minotaur (sic).  But thats just me!

PaulRB

Quote
These posters don't need "SOME people" to be their dad or teach them a life lesson or to be a hall minotaur
They need someone to do their coding for them. And they got it. Welcome to the Arduino forum.

damadscientist

Hey guys,

I greatly appreciate all the assistance that was provided and apologize if I was not clear enough on what I was trying to accomplish.

To add a more wow factor to this code, I wanted to be able to change the color so that it could be a blue flame. It would be even better if I could somehow incorporate the Bluefruit color picker app so that I could change the color of the flame on the fly.

I have the following posting below asking for that assistance but I am not 100% sure I understand what they were asking as I could not put it all together. Also the post is somehow locked and I cannot reply


https://forums.adafruit.com/viewtopic.php?f=47&t=152209

damadscientist

This has been answered here:

https://forums.adafruit.com/viewtopic.php?f=47&t=152228

Still trying to figure out the color picker portion though

Go Up