This may be more of a general Arduino programming question,

This may be more of a general Arduino programming question, but since I’m applying it directly to a FastLED project, it’s just as well asked here. How would I go about creating a function that can run over itself without overwriting itself?

To put it simpler, I want to build a sketch that creates a firework that picks a random color and fires off in opposite directions (with a tail) from a random point. I’m partial to the comet sketch from the OpenLab Neopixel playground. The trick is that I want more than one to fire off at at time, with their own colors and tails.

So, is the best way to do this to create two separate functions and time them to overlap (somehow, cuz I’m at a loss here) or as one functions to fire off every x-second so they overlap but without overwriting the previous set?

Write one function and call it in a loop. The last one called will “overwrite” a given LED if a prior function call has already written a value to it.

How would I go about doing that though? Do I add the function to the loop statement, do a time comparison and do it again?

I would declare the pertinent variables, put the function in a “Forever Loop” While(True)
call the function then throw in a random time delay at the bottom of the loop.

If you want you can randomize or just increment the call parameters to get a “psudo random” fireworks display.

correct me if I’m wrong, but if I call the function a second time while the first is still running, won’t it kill off the original variables, replacing them with its own? Or will they remain in their locally stored function and increase (for example, position) integers without interference?

Inside the forever loop the draw me a fireworks function will run to completion. You will likely have another loop inside your draw me a fireworks function that will itterat enough times to draw a full fireworks anamation. And then return to the forever loop. Where you can call a random time delay. After the time delay expires you will be back at the top of your forever loop where you can edit the call parameters for your fireworks function. Rinse and repeat.

@JP_Roy That is majorly cool, but I’m not working with a grid, just a single, 300 pixel strip. This is more or less the look I’m going for, but with more than one at a time:

@Jimmy_Edwards Here’s what I’ve got running so far - https://gist.github.com/anonymous/30d02c2bf5231b823c57ae0334f221da

I set things up so there’s never more than 3 fireworks being displayed at a time, to prevent overloading, by doing a check and having the function call itself if it’s halfway done. What I’m seeing though is that when the function calls itself, the previous iteration isn’t allowed to finish, it just starts fresh.

I think I may have misunderstood your forever loop though.

@Christopher_Kirkman1 Did not realise you were working on a single strand. Still… have a close look at the structures and functions in my sketch, in my humble opinion, that should provide a sound base to create the one-dimension animation with multiple objects that you are looking for.

I can try to answer more specific questions about that sketch if you get lost in it. That would not surprise me as I am by no means a professional programmer.

your void loop() is your “Forever Loop” and it does call your fireworks function.

You can declare a 2nd start position
“int LEDPosition2” with a random start position. And then use LEDPosition2 in the

if((LEDPosition2+i)<stripLength)showLED(LEDPosition2+i, mainHue, 255, intensity);

if((LEDPosition2-i)>0) showLED(LEDPosition2-i, mainHue, 255, intensity);

The showLED() is what actually turns on the LEDs so call it again with the position of your 2nd fireworks “LEDPosition2”

I can see how that would work, but not as a true overlap. The function would then just fire off one, then the second, wait till the second one completes and then start over again. My goal is to make it a seamless animation, if that’s even possible.

I think @JP_Roy 's code does this on a grander scale because as one set of blasts finish, another is fired from below, but I’m still trying to get my head around how, separating out the matrix-specific instruction.

I’m not at home till later so I can’t test it myself, but if inside my loop I create a time check that calls the function every two second or whatever, will any previous functions be allowed to finish their runs or am I gonna run into it resetting itself each time?

I’m not sure. You are just going to have to “Hack on it” , but that is half the fun. Each function should run to completion without a problem. You call “ShowLED()” more than once in your fireworks(). I think you are referring to “Reentrant” functions as in a function calling itself. That is indeed something to avoid in a “Procedural language” unless you have designed the function to actually be reentrant from the beginning. It is much more common in Object Oriented Languages like C++ and C# but that is something that I can’t help you with.

I’m about ready to give up the ghost. I can fire off two at the same time, and they display mostly fine, but there’s a weird delay at the end of the second one’s run and the LEDs freeze in place for a second before the function fires again. I suppose it’s possible I’m overthinking it by trying to hack up someone else’s code but even when I try something as simple as staggering two pixels down the same chain with one function, the second instance overrides the first.

@Christopher_Kirkman1 Nah… don’t give up! What (I think) you are trying to do is definitely feasible. However if you request help, you will need to provide the code you currently have and ideally a bit more detail as to what ‘animation’ you are expecting from it. Maybe even a short video showing the current behaviour !?

Sometimes just spending extra time to clearly describe the wanted behaviour for someone else is sufficient to stir up our own analysis of the problem and may help with your own solution. I am willing to help you !

Okay, let me start by describing what I want to see.

From a random point on a 1x300 strip, I want two points of light, each followed by a dimmed tail, to travel down the strip in opposite directions. At some point, let’s say half way before those points disappear off the edges of the strip, I want another 2 points of light to fire off, without interrupting the first two or its tails, repeating seamlessly. Since I’m better with After Effects than I am with Arduino, I’ve attached a GIF that approximates what I want.

The codepen link I attached up the thread some creates the basic “shell” (in javascript) which I can get going pretty easily, but it’s when I try and introduce a second shell is when things go all wonky. The myriad of challenges I’ve hit are due to function instances either waiting to complete before moving onto another (instead of overlapping), the second function interrupting and overriding the previous instance of the function and/or otherwise random freezing of the program because of bad math or similar.

The more I mull about it, the more I’m starting to think that this requires OOP which is way above my knowledge threshold.
missing/deleted image from Google+

I realized that last GIF was hard to see, so try this:
missing/deleted image from Google+

@Christopher_Kirkman1 Yes it is better GIF. Is this exactly what you are looking for though ?

I see 2 shells ( a red and a green) that basically alternate there does seem to be some overlap between the complete fading of one before the other is launched.

Yeah, the animation is just a sample that loops. I want to code the strip in such a way that each new shell overlaps the last and uses a new random color and start position. Even if it’s shooting off a new shell every 3 seconds or so is fine as long as it doesn’t kill the previous one off OR completely wait for the previous one to finish its run before starting the next. Does that make sense?