Hello wonderful people. Looking for some suggestion on how to implement an idea.

gplus
(Sergeant Seven) #1

Hello wonderful people.
Looking for some suggestion on how to implement an idea.
I have a WS2812B that flickers like a candle with a random value at a random interval.

the code so far as follows in my void Flicker()

unsigned long now = millis(); //get current time
if ((now - lastFlicker) > interval) { //Execute only after set interval.
lastFlicker = now; //Update the time it last flickered to now.
val = map(random(200), 0, 200, 200, 250); //generate a random value between 0 and 200 and map it so it is between 200 and 250 then send to the HSV value for brightness at the end of the loop.
interval = random(1000) + 125; //Randomize the interval for the next loop.
}

val is part of HSV(hue,sat,val) at the end of the loop to update the LED values after calculating them in whatever function is being called by the finite state machine. much of the code is proprietary so I can not post all of it, just the snippets pertaining to my issue.

I need to figure out a way to make this so it fades at a randomized speed between the random values and still maintains the random amount of time spent on each value once it has faded. Right now it works great but it changes in sharp steps between the random brightness values. I need this to be done in non blocking code as well. Any recommendations would be wonderful. thanks!

(Sergeant Seven) #2

I think I managed to figure this out on my own, this is what I came up with. It needs a little more fine tuning but it works well so far.

void flicker() { //Function Flicker; Randomly changes the brightness between 180 and 255 at random intervals between 0ms and 50ms.
unsigned long now = millis(); //get current time
if ((now - lastFlicker) > interval) { //Execute after set interval.
lastFlicker = now; //Update time since last flicker
interval = random(50); //Randomize the interval.
if (val < flickerAmount){ //Incriment the value until it reaches the set flicker amount.
val ++;
}
else if (val > flickerAmount){ //or dincriment if it is higher then flicker amount
val --;
}
else if (val == flickerAmount){ //Only randomize the flicker amount when the value has reached the previous amount.
flickerAmount = map(random(200), 0, 200, 200, 250); //generate a random value between 180 and 225.
}
}
}

(Stefan Petrick) #3

Use the noise - it´s designed to produce nice gradients between different values.
missing/deleted image from Google+

(Sergeant Seven) #4

@Stefan_Petrick do you have an example of using noise to control the brightness value in HSV? I think it would probably work great. so far what I came up with works well too. I made some other adjustments after my last post that seemed to help it look more realistic.

(Dustin G) #5

@Sergeant_Seven Would you be willing to post your code? I’m looking for various options for creating a candle like effect and wanted to try yours out. Thanks!